nim-markdown icon indicating copy to clipboard operation
nim-markdown copied to clipboard

Document an example usage for parsing markdown into an AST

Open hoijui opened this issue 3 years ago • 9 comments

... instead of converting to markdown. (in the README)

hoijui avatar Jul 01 '21 18:07 hoijui

@hoijui Does this suit your need? The given example shows you how to get an AST using prod markdown() and manipulate such an AST.

soasme avatar Jul 01 '21 23:07 soasme

ahh nice, thank you! How would I get to this site, e.g. from the README or somewhere else?

It would be nice if it would also show, how to traverse the AST, and for my personal use, I would want to scrape out only the link and image targets, plus the same two things out of raw HTML elements (which I would parse with an other library, I guess).

I need this for an Open Source tool that does quality checking on documentation written in markdown.

hoijui avatar Jul 02 '21 14:07 hoijui

@hoijui the link is missing on README or docs site. The traversing is also missing. I can add them later.

In short, to get the children nodes of a node, just simply call .children on a node, which is a DoublyLinkedList[Token] object. To traverse the AST tree, you will need to write some recursive code keep calling .children.

soasme avatar Jul 03 '21 03:07 soasme

@hoijui the link is missing on README or docs site. The traversing is also missing. I can add them later.

thank you, that would be great! :-)

In short, to get the children nodes of a node, just simply call .children on a node, which is a DoublyLinkedList[Token] object. To traverse the AST tree, you will need to write some recursive code keep calling .children.

... That is as far as I got. though all I get then is objects of type Token. I do not understand how I would find out what kind of token it is, as type(token) always returns just Token, nothing more specific. though before going to bed this night, I though.. the render() function must do it somehow, let's have a look ...

hoijui avatar Jul 03 '21 05:07 hoijui

.. aha.. nope, it doesn't :/

hoijui avatar Jul 03 '21 05:07 hoijui

@hoijui Say you want to check if a token is a paragraph, you can use Nim of keyword: if token of Paragraph: ....

Actually, Nim-markdown render function is one of the examples of traversing the AST.

soasme avatar Jul 03 '21 05:07 soasme

ahhh.... cool, thank you! so it can be done with of or through "function overloading" (as with the $ operator you linked to). great! With this, got it working now. :-)

... an other issue now though.. the token.pos seems to be 0, almost always. do I have to set a config var to get real values there?

hoijui avatar Jul 03 '21 07:07 hoijui

Currently, nim-markdown generates an Abstract Syntax Tree, not Parse Tree. Unfortunately, that means the position information is lost.

soasme avatar Jul 12 '21 00:07 soasme