nim-markdown

repository·master·Indexed 41 days ago

https://github.com/soasme/nim-markdown

A Markdown parser for the Nim programming language that transforms Markdown text into HTML. It supports a wide range of block and inline elements as of version 0.8.8, provides a CLI binary, and allows for custom parsing rules and Abstract Syntax Tree (AST) manipulation.

Tokens
1.3K
Snippets
5
Records
6
Agent score
50%

What's inside nim-markdown

  1. Operate on the Abstract Syntax Tree (AST)

    master

    The markdown function allows you to manipulate the resulting Abstract Syntax Tree (AST) instead of just receiving a rendered string.

    1. Initialize a custom root: Create a Document() object and pass it to the markdown function using the root= argument.
    2. Modify the tree: Once markdown(input, root=root) is called, the root object is populated with the parsed nodes. You can manually append new nodes (like Paragraph, Em, or Text) to the tree using appendChild().
    3. Render the modified tree: Use the render(root) function to convert your modified AST into an HTML string.
    let root = Document()
    discard markdown("# Hello World\nTest.", root=root)
    
    # Manually add nodes to the AST
    let p = Paragraph(loose: true)
    let em = Em()
    let text = Text(doc: "emphasis text.")
    em.appendChild(text)
    p.appendChild(em)
    root.appendChild(p)
    
    # Render the modified tree
    echo render(root)
  2. Install nim-markdown via nimble

    master

    You can install nim-markdown using the Nim package manager, nimble. You can choose to install the latest version, a specific stable version range, or the latest development version from the head of the repository.

    # Install the latest version
    $ nimble install markdown
    
    # Install a specific stable version range
    $ nimble install markdown@">= 0.8.0"
    
    # Install the latest development version
    $ nimble install markdown@#head
  3. Customize parsing with custom block or inline parsers

    master

    You can extend Nim-markdown by implementing new parsing rules. To do this, you must:

    1. Define a new parser type that inherits from Parser.
    2. Define a new token type that inherits from Block (for block-level parsing) or Inline (for inline-level parsing).
    3. Register the parser by inserting it into MarkdownConfig.blockParsers or MarkdownConfig.inlineParsers. To ensure it takes precedence, use insert(parser, 0) to place it at the beginning of the parser list.
    4. Implement the parse method on your parser to define how it identifies tokens in the source string.
    5. Implement the $ (render) method on your token to define how it should be converted to HTML.

    Note: APIs are subject to change before v1.0.0.

    import markdown, strutils, sequtils, strscans, system, os
    
    # 1. Define Parser
    type IncludeParser = ref object of Parser
    
    # 2. Define Token
    type IncludeToken = ref object of Block
        path: string
    
    # 3. Register Parser
    var c = initCommonmarkConfig()
    c.blockParsers.insert(IncludeParser(), 0)
    
    # 4. Implement parse
    method parse(parser: IncludeParser, doc: string, start: int): ParseResult {.locks: "unknown".} = 
      # ... implementation logic ...
      ParseResult(token: IncludeToken(path: path), pos: idx)
    
    # 5. Implement render
    method `$`(token: IncludeToken): string = 
      markdown(token.path.readFile, c)
    
    # Usage
    let md = "#include \"hello.md\""
    echo(markdown(md, c))
  4. Use nim-markdown as a library

    master

    To use nim-markdown within your Nim project, import the markdown module and call the markdown function. This function takes a Markdown string as input and returns the transformed HTML string.

    # 1. import pkg.
    import markdown
    
    # 2. transform md to html.
    let html = markdown("# Hello World\nHappy writing Markdown document!")
    
    # 3. do something :)
    echo(html)
  5. Supported Markdown features

    master

    As of version 0.8.8, nim-markdown supports the following features:

    Block Elements:

    • Thematic Break
    • Heading
    • Indented code block
    • Fence code block
    • Block Quote
    • Ordered/Unordered List
    • Nested lists
    • Raw HTML block
    • Table
    • Paragraph

    Inline Elements:

    • Ref Link
    • Inline Link
    • Auto link
    • Image Link
    • Emphasis
    • Double Emphasis
    • Strikethrough
    • Link Break
    • Inline Code
    • Inline HTML
    • Escape

    Planned/Roadmap Features:

    • GFM (GitHub Flavored Markdown) support
    • Writing extensions
    • Footnotes
    • Auto link (as an extension)