ply-markdown.h
Markdown Parser
The Markdown parser converts Markdown text into a tree of block and span objects. All functions and types in this
module are defined in the ply::markdown namespace.
Parser
Parser supports incremental parsing. Create one with createParser(), pass input lines to parseLine(), then call
flush() after the last line. Top level blocks are returned as they become available.
Creation and streaming
Owned<Parser> createParser(const ParseOptions& options)Creates a stateful parser using the selected extensions. Retain the returned
Owned<Parser>while streaming input. Available options:bool backslashEscapesRecognizes backslash escapes before ASCII punctuation. bool characterReferencesDecodes named and numeric character references. bool codeSpansRecognizes inline code delimited by backticks. bool emphasisRecognizes emphasis delimiters. bool strongEmphasisRecognizes strong-emphasis delimiters. bool inlineLinksRecognizes links with inline destinations. bool referenceLinksResolves full, collapsed and shortcut reference links. bool inlineImagesRecognizes images with inline destinations. bool referenceImagesResolves full, collapsed and shortcut reference images. bool autolinksRecognizes URI and email autolinks enclosed in angle brackets. bool inlineHTMLRecognizes CommonMark inline HTML. bool softLineBreaksProduces soft-break spans at ordinary line boundaries. bool hardLineBreaksRecognizes hard breaks created by spaces or a backslash. bool blockQuotesRecognizes block quote markers. bool orderedListsRecognizes ordered list markers. bool unorderedListsRecognizes unordered list markers. bool indentedCodeBlocksRecognizes code blocks created by indentation. bool fencedCodeBlocksRecognizes backtick- and tilde-fenced code blocks. bool htmlBlocksRecognizes CommonMark HTML blocks. bool atxHeadingsRecognizes headings beginning with #markers.bool setextHeadingsRecognizes headings followed by =or-underlines.bool thematicBreaksRecognizes thematic breaks. bool linkReferenceDefinitionsCollects and removes link reference definitions. GitHub Flavored Markdown extensions are disabled by default. Use
ParseOptions::githubFlavored()to obtain an object with all element types enabled.bool tablesEnables pipe tables. bool taskListItemsRecognizes task markers at the start of list items. bool strikethroughEnables text delimited by pairs of tildes. bool extendedAutolinksRecognizes URL and email autolinks without angle brackets. bool tagFilterEscapes the opening characters of the raw HTML tags disallowed by. Parser* duplicate(Parser* parser)Makes an independent deep copy, including any unfinished block. Copying an
Owned<Parser>uses this function, so a parser snapshot can be retained while either copy continues parsing.void destroy(Parser* parser)Destroys a parser created by
createParser().Owned<Parser>normally performs this automatically.Owned<Block> parseLine(Parser* parser, StringView line)Consumes one input line and returns a completed top-level block when available. Call it repeatedly in source order.
Owned<Block> flush(Parser* parser)Finishes the current top-level block and returns it. Call it once all lines have been supplied.
Array<Owned<Span>> parseInlineSpans(StringView markdown, const ParseOptions& options)Parses text directly as paragraph-style inline content without recognizing block constructs. Link reference definitions are not collected by this entry point, so reference links require document parsing.
String convertInlineToHtml(StringView src, const ParseOptions& parseOptions, const HTMLOptions& htmlOptions)Parses paragraph-style inline content and returns its rendered HTML without adding block markup. Parsing extensions are selected with
ParseOptions, whileHTMLOptionscontrols rendering and link transformation.Array<Owned<Block>> parse(StringView markdown, const ParseOptions& options)Parses a complete string and returns its top-level blocks in document order. This is the whole-document equivalent of streaming the lines through a parser configured with the same options and then calling
flush().String convertToHtml(StringView src, const ParseOptions& options)Parses a complete Markdown string and returns its HTML. This is the direct convenience API; parsing extensions are selected with
ParseOptions.void convertToHtml(Stream* outs, const Block* block, const HTMLOptions& options)Renders an already-parsed block and its descendants to a stream. Use this overload to inspect or modify the AST, or to render blocks incrementally.
HTMLOptionscontrols rendering rather than Markdown syntax.bool childAnchorsWrites a child anchor span for a heading with an id, instead of putting theidon the heading. Default is false.Functor<String(StringView)> filterLinksTransforms each link or image destination before XML escaping and output