Parse line numbers
See original GitHub issueIs it possible to retrieve information about the line number of block-level elements in the AST? We are looking into using Mistune for a project and this would be quite helpful.
E.g., running:
markdown = mistune.create_markdown(renderer=mistune.AstRenderer())
markdown("# Heading 1\n\n## Heading2")
to return something like
[{'type': 'heading',
'children': [{'type': 'text', 'text': 'Heading 1'}],
'level': 1,
'lineno': 0},
{'type': 'heading',
'children': [{'type': 'text', 'text': 'Heading2'}],
'level': 2,
'lineno': 2}]
If it’s not possible currently, is it something that would be doable w/ a PR?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:11 (2 by maintainers)
Top Results From Across the Web
how to track line number and position in line while parsing ...
While parsing some data I need to know where I am now in a file (line number, position in line). For example if...
Read more >Splitting a File at Given Line Numbers | Baeldung on Linux
Learn how to split a file at given line numbers using head, tail, sed, and awk.
Read more >haskell - What is the fastest way to parse line with lots of Ints?
I'm learning Haskell for two years now and I'm still confused, whats the best (fastest) way to read tons of numbers from a...
Read more >Finding definitions from a source file and a line number in ...
Open a file, read it, find the line number. Right. Then, how do you know which functions this line is in? You don't,...
Read more >How to Find Line Number of a Given Word in text file using ...
In this article, we will show you how to get a line number in which the given word is present from a text...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would be using this for a linter like tool. Perhaps a spellchecker or a link checker. In the care of a spellchecker, the tool should skip anything within code blocks or spans, so we can’t just pass the entire document into a spellchecker. In the case of a link checker, we need to find all links (external, internal, absolute, relative). In each case, the easiest way to ensure we are checking the correct content is by running the document through a Markdown parser.
Of course, we could pass the rendered HTML into any number of existing tools, but that will present any errors found with the location in the HTML. However, ideally, the location (line and column) in the Markdown source document would be preferred. That way the user can easily find the error and fix it.
To implement such a tool, I would create my own custom renderer which would step through the AST, checking the content of the various nodes. For any nodes without errors, the renderer would return nothing. However, when an error is encountered, the renderer would report the error, ideally with the line and column.
See the oversimplified pseudo example of a link checker below
Tracking line numbers would definitely not make mistune slower unless the feature was egregiously poorly implemented.