@@ -11,7 +11,7 @@ import htmlparser {.all.}
1111export HtmlTag
1212
1313import ./ lexer, ./ ast
14- import pkg/ jsony
14+ import pkg/ [ jsony, nyml]
1515
1616type
1717 MarkdownParser * = object
3232 # # Internal: Counter for generating unique selectors
3333 ast* : seq [MarkdownNode ]
3434 # # The abstract syntax tree (AST) of the parsed markdown document
35+ headerYaml: JsonNode
36+ # # Parsed YAML front matter as JsonNode
3537 footnotes: OrderedTableRef [string , MarkdownNode ]
3638 # # Footnote definitions parsed from the document
3739 footnotesHtml* : string
@@ -655,6 +657,28 @@ proc parseMarkdown(md: var Markdown, currentParagraph: var MarkdownNode) =
655657 closeCurrentParagraph ()
656658 let bqNode = md.parseBlockquote ()
657659 md.ast.add (bqNode)
660+ of mtkHorizontalRule:
661+ closeCurrentParagraph ()
662+ md.ast.add (MarkdownNode (
663+ kind: mdkHorizontalRule,
664+ line: curr.line,
665+ wsno: curr.wsno
666+ ))
667+ md.advance ()
668+ of mtkDocument:
669+ try :
670+ # Parse YAML front matter
671+ # TODO test YAML parsing (https://github.com/openpeeps/nyml)
672+ md.headerYaml = fromYaml (curr.token, JsonNode )
673+ except YAMLException as e:
674+ # On error, add a text node with the error message
675+ md.ast.add (MarkdownNode (
676+ kind: mdkText,
677+ text: curr.token, # invalid YAML, just add as text
678+ line: curr.line,
679+ wsno: curr.wsno
680+ ))
681+ md.advance ()
658682 of mtkFootnoteRef:
659683 withCurrentParagraph do :
660684 let id = curr.attrs.get ()[0 ]
@@ -673,7 +697,6 @@ proc parseMarkdown(md: var Markdown, currentParagraph: var MarkdownNode) =
673697 md.advance ()
674698 else :
675699 closeCurrentParagraph ()
676- md.advance ()
677700
678701proc newMarkdown * (content: sink string ,
679702 opts: MarkdownOptions = defaultOptions): Markdown =
@@ -712,6 +735,10 @@ proc hasSelectors*(md: Markdown): bool =
712735 # # Check if there are any headline selectors (anchors) in the parsed Markdown
713736 md.selectors != nil and md.selectors.len > 0
714737
738+ proc getHeader * (md: Markdown ): JsonNode =
739+ # # Get the parsed YAML front matter from the Markdown
740+ md.headerYaml
741+
715742proc getFootnotes * (md: Markdown ): OrderedTableRef [string , MarkdownNode ] =
716743 # # Get the footnote definitions from the parsed Markdown
717744 md.footnotes
@@ -867,5 +894,7 @@ proc renderNode(md: var Markdown, node: MarkdownNode): string =
867894 fnContent
868895 )
869896 )
897+ of mdkHorizontalRule: result = " <hr>"
870898 else :
871- discard
899+ echo node.kind
900+ echo " Warning: Unhandled MarkdownNode kind in renderNode"
0 commit comments