Skip to content

Commit 482ddfd

Browse files
committed
Handle AST to JSON
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent d56ed1d commit 482ddfd

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/marvdown.nim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,31 @@ elif isMainModule:
7575
# show stats if `--bench` flag is set
7676
stdout.writeLine("🔥 Done in " & $benchTime & " sec")
7777

78+
proc jsonCommand(v: Values) =
79+
## Convert Markdown to JSON via CLI
80+
if not v.has("md"):
81+
display("Nothing to parse. Missing `.md` doc")
82+
QuitFailure.quit
83+
84+
let
85+
filePath = absolutePath(normalizedPath($(v.get("md").getPath)))
86+
content = readFile(filePath)
87+
88+
var md = newMarkdown(content)
89+
90+
# output JSON to console
91+
stdout.writeLine(md.toJson())
92+
7893
# Kapsis CLI Application
7994
commands:
8095
html path(`md`), ?filename(`output`),
8196
bool(--optAnchors),
8297
bool(--bench):
8398
## Write a markdown document to HTML
8499

100+
json path(`md`):
101+
## Export the markdown AST as JSON
102+
85103
else:
86104
# Use Marvdown as Nimble library
87105
import std/[htmlparser, xmltree]
@@ -100,3 +118,8 @@ else:
100118
## Convert Markdown content to XML Node
101119
var md = newMarkdown(content)
102120
htmlparser.parseHtml(md.toHtml())
121+
122+
proc getAst*(content: sink string): string =
123+
## Retrieve the Markdown AST as a stringified JSON
124+
var md = newMarkdown(content)
125+
md.toJson()

src/marvdown/parser.nim

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import htmlparser {.all.}
1111
export HtmlTag
1212

1313
import ./lexer, ./ast
14+
import pkg/jsony
1415

1516
type
1617
MarkdownParser* = object
@@ -199,14 +200,6 @@ proc parseCheckboxItem(md: var Markdown): MarkdownNode =
199200
for n in md.parseInline(itemText):
200201
result.children.items.add(n)
201202
md.advance()
202-
203-
# If there's text after the checkbox, parse it as inline
204-
# echo md.parser.curr
205-
# if md.parser.curr.kind == mtkText and md.parser.curr.token.len > 0:
206-
# echo md.parser.curr.token
207-
# for n in md.parseInline(md.parser.curr.token.strip()):
208-
# result.children.items.add(n)
209-
# md.advance()
210203

211204
proc parseEmphasis(md: var Markdown): MarkdownNode =
212205
# Parse emphasis text and add to current paragraph
@@ -692,6 +685,10 @@ proc toHtml*(md: var Markdown): string =
692685
if md.opts.showFootnotes and md.footnotesHtml.len > 0:
693686
add result, "<hr><div class=\"footnotes\">" & md.footnotesHtml & "</div>"
694687

688+
proc toJson*(md: Markdown): string =
689+
## Convert the parsed Markdown AST to JSON
690+
jsony.toJson(md.ast)
691+
695692
proc getSelectors*(md: Markdown): OrderedTableRef[string, string] =
696693
## Get the headline selectors (anchors) from the parsed Markdown
697694
md.selectors

0 commit comments

Comments
 (0)