gomarkdown / markdown

markdown parser and HTML renderer for Go
Other
1.37k stars 172 forks source link

Programmatically generate markdown document using ast nodes #247

Closed rockaport closed 2 years ago

rockaport commented 2 years ago

I couldn't readily find out through the documentation/tests, but is it possible to programmatically generate a document using the ast types and render to a file using this library?

I'm thinking of something along the lines of

var document = ast.Document{}
// append/add nodes/headings/text, etc.
markdown.Render(document, md.NewRenderer())

Thanks!

kjk commented 2 years ago

Yes, this will work.

The AST will have to have the right shape, of course. You can use ast.Print to visualize ast for some example markdown documents to get familiar with expected structure.

I haven't done "build ast from scratch" approach but I did ast modifications.

You can see https://github.com/kjk/cheatsheets/blob/main/cheatsheets.go#L37 for example of traversing ast to extract information and https://github.com/kjk/cheatsheets/blob/main/cheatsheets.go#L104 for modifying ast (minor modifications).