soasme / nim-markdown

A Beautiful Markdown Parser in the Nim World.

Home Page:https://www.soasme.com/nim-markdown/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Document an example usage for parsing markdown into an AST

hoijui opened this issue · comments

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

commented

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

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.

commented

@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.

@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 ...

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

commented

@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.

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?

commented

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