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

[feature] API to operate on parsed markdown AST

timotheecour opened this issue · comments

pretty much the same motivation as described in http://pandoc.org/filters.html

How would you modify your regular expression to handle these cases? It would be hairy, to say the least. What we need is a real parser. Well, pandoc has a real markdown parser, the library function readMarkdown. This transforms markdown text to an abstract syntax tree (AST) that represents the document structure. Why not manipulate the AST directly in a short Haskell script, then convert the result back to markdown using writeMarkdown?

Just want to open a discussion on whether this could be supported, how would API look like etc.

use cases

enabling application writers to use nim-markdown API to write these:

  • writing linters for markdown files
  • listing urls / links
  • transform markdown file in custom ways

related

basically this is related to exposing a more modular API, as in pandoc, even if input/output is limited to markdown/html ; in particular input:markdown => output:markdown would be a natural extension (ie, markdown transformers)

Pandoc has a modular design: it consists of a set of readers, which parse text in a given format and produce a native representation of the document (an abstract syntax tree or AST), and a set of writers, which convert this native representation into a target format

using pandoc -f markdown... -t markdown... can have surprisingly useful applications. As a demo, this file is generated by
...

@soasme weird: I'm seeing an answer your write in gmail (subscribed to this thread) but it's not appearing in github... maybe you deleted it?

commented

@timotheecour yes. my earlier response was syntax incorrect. I'll need revisit and submit a new solution.

commented

The master#HEAD branch now supports operating markdown AST after the parsing and rendering is done. Example:

import markdown

let root = Document()
discard markdown("# Hello World\nTest.", root=root)
let p = Paragraph(loose: true)
let em = Em()
let text = Text(doc: "Copyright © 2019 Ju.")
em.appendChild(text)
p.appendChild(em)
root.appendChild(p)
echo(root.render)
# Output:
# <h1>Hello World</h1>
# <p>Test.</p>
# <p><em>emphasis text.</em></p>

I'll clean code, add docs and make a new release next week.