MDExNative.Comrak (MDExNative v0.1.3)

Copy Markdown View Source

Markdown parsing and rendering powered by the Rust comrak crate.

This package provides Elixir bindings for MDExNative.Comrak. It returns rendered HTML/XML directly and keeps the native NIF behind the public API.

Options mirror Rust comrak::Options and can be passed as keyword lists. Nested :extension, :parse, and :render options are validated in Elixir before crossing the NIF boundary.

Examples

iex> MDExNative.Comrak.markdown_to_html("# Hello")
"<h1>Hello</h1>\n"

iex> MDExNative.Comrak.markdown_to_html("- [x] done", extension: [tasklist: true])
"<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> done</li>\n</ul>\n"

iex> MDExNative.Comrak.anchorize("Hello World")
"hello-world"

Summary

Types

Heading anchor generated from text.

Parsed MDExNative.Comrak AST node.

Parsed fenced code block info string.

Comrak Extension options.

Rendered HTML.

Markdown source text.

Comrak Options accepted by markdown_to_html/2 and markdown_to_xml/2, plus the :syntax_highlight convenience option.

Comrak Parse options.

Comrak Render options.

Rendered CommonMark XML.

Functions

Converts text to a heading anchor.

Converts a generic MDExNative.Comrak document to CommonMark.

Converts a generic MDExNative.Comrak document to HTML.

Converts a generic MDExNative.Comrak document to XML.

Converts Markdown to HTML.

Converts Markdown to XML.

Parses a fenced code block info string into generic parts.

Parses Markdown into a generic MDExNative.Comrak AST.

Types

anchor()

@type anchor() :: String.t()

Heading anchor generated from text.

ast_node()

@type ast_node() :: struct()

Parsed MDExNative.Comrak AST node.

code_fence_info()

@type code_fence_info() :: %{
  language: String.t(),
  metadata: String.t(),
  attributes: %{required(String.t()) => String.t() | true}
}

Parsed fenced code block info string.

extension_options()

@type extension_options() :: keyword()

Comrak Extension options.

html()

@type html() :: String.t()

Rendered HTML.

markdown()

@type markdown() :: String.t()

Markdown source text.

options()

@type options() :: keyword()

Comrak Options accepted by markdown_to_html/2 and markdown_to_xml/2, plus the :syntax_highlight convenience option.

parse_options()

@type parse_options() :: keyword()

Comrak Parse options.

render_options()

@type render_options() :: keyword()

Comrak Render options.

xml()

@type xml() :: String.t()

Rendered CommonMark XML.

Functions

anchorize(text)

@spec anchorize(String.t()) :: anchor()

Converts text to a heading anchor.

Examples

iex> MDExNative.Comrak.anchorize("Hello World")
"hello-world"

document_to_commonmark(document, options \\ [])

@spec document_to_commonmark(MDExNative.Comrak.Document.t(), options()) :: markdown()

Converts a generic MDExNative.Comrak document to CommonMark.

document_to_html(document, options \\ [])

@spec document_to_html(MDExNative.Comrak.Document.t(), options()) :: html()

Converts a generic MDExNative.Comrak document to HTML.

document_to_xml(document, options \\ [])

@spec document_to_xml(MDExNative.Comrak.Document.t(), options()) :: xml()

Converts a generic MDExNative.Comrak document to XML.

markdown_to_html(markdown, options \\ [])

@spec markdown_to_html(markdown(), options()) :: html()

Converts Markdown to HTML.

Options

Pass Comrak options as a keyword list matching Rust comrak::Options, with any of these top-level keys:

  • :extension - mapper to Comrak's Extension options, for example tasklist: true, table: true, autolink: true, or header_id_prefix: "prefix-".
  • :parse - mapper to Comrak's Parse options, for example smart: true.
  • :render - mapper to Comrak's Render options, for example unsafe: true, hardbreaks: true, or sourcepos: true.
  • :syntax_highlight - syntax highlighting options with an :engine and engine-specific :opts. Currently :lumis is the only supported engine, and its options are passed to the native Rust LumisAdapter for fenced code blocks. See the Lumis.formatter/0 type for formatter options.

Examples

iex> MDExNative.Comrak.markdown_to_html("**bold**")
"<p><strong>bold</strong></p>\n"

iex> MDExNative.Comrak.markdown_to_html("- [x] done", extension: [tasklist: true])
"<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> done</li>\n</ul>\n"

iex> html = MDExNative.Comrak.markdown_to_html("```elixir\nIO.puts(\"Hello\")\n```", syntax_highlight: [engine: :lumis, opts: [formatter: {:html_inline, theme: "github_light", pre_class: "code-example"}]])
iex> html =~ ~s(<pre class="lumis code-example")
true

markdown_to_xml(markdown, options \\ [])

@spec markdown_to_xml(markdown(), options()) :: xml()

Converts Markdown to XML.

Examples

iex> MDExNative.Comrak.markdown_to_xml("# Hello")
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <heading level=\"1\">\n    <text xml:space=\"preserve\">Hello</text>\n  </heading>\n</document>\n"

iex> MDExNative.Comrak.markdown_to_xml("# Hello", render: [sourcepos: true])
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document sourcepos=\"1:1-1:7\" xmlns=\"http://commonmark.org/xml/1.0\">\n  <heading sourcepos=\"1:1-1:7\" level=\"1\">\n    <text sourcepos=\"1:3-1:7\" xml:space=\"preserve\">Hello</text>\n  </heading>\n</document>\n"

parse_code_fence_info(info)

@spec parse_code_fence_info(String.t() | nil) :: code_fence_info()

Parses a fenced code block info string into generic parts.

The first word is returned as the language, the remaining text is preserved as metadata, and shell-like tokens in the metadata are exposed as attributes.

Examples

iex> MDExNative.Comrak.parse_code_fence_info(~s(elixir pre_class="demo" highlight_lines=2 include_highlights))
%{
  language: "elixir",
  metadata: ~s(pre_class="demo" highlight_lines=2 include_highlights),
  attributes: %{
    "pre_class" => "demo",
    "highlight_lines" => "2",
    "include_highlights" => true
  }
}

iex> MDExNative.Comrak.parse_code_fence_info("")
%{language: "", metadata: "", attributes: %{}}

parse_document(markdown, options \\ [])

@spec parse_document(markdown(), options()) :: MDExNative.Comrak.Document.t()

Parses Markdown into a generic MDExNative.Comrak AST.

Examples

iex> MDExNative.Comrak.parse_document("# Hello")
%MDExNative.Comrak.Document{
  nodes: [
    %MDExNative.Comrak.Heading{
      nodes: [
        %MDExNative.Comrak.Text{literal: "Hello", sourcepos: %MDExNative.Comrak.Sourcepos{start: {1, 3}, end: {1, 7}}}
      ],
      level: 1,
      setext: false,
      sourcepos: %MDExNative.Comrak.Sourcepos{start: {1, 1}, end: {1, 7}}
    }
  ],
  sourcepos: %MDExNative.Comrak.Sourcepos{start: {1, 1}, end: {1, 7}}
}