Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parse line numbers

See original GitHub issue

Is 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

waylancommented, May 22, 2020

What is the usage of line numbers in your case?

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

class LinkChecker(BaseRenderer):
    NAME = 'linkchecker'
    IS_TREE = False

    def text(self, text):
        return ''

    def link(self, link, pos, children=None, title=None):
        result = validate(link):
        if result:
            return f'ERROR at {pos.line}:{pos.col} {link} --> {result.code}\n'
         return ''
ddevaultcommented, Sep 26, 2022

Tracking line numbers would definitely not make mistune slower unless the feature was egregiously poorly implemented.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found