Markup.Markdown (Markup.Markdown v0.1.0)

Copy Markdown View Source

CommonMark and GitHub Flavored Markdown for Markup, parsed and rendered by comrak through mdex_native.

Markup.to_html!("# Hello", language: :markdown)
#=> "<h1>Hello</h1>\n"

doc = Markup.parse_document!("# Hello", language: :markdown)
doc[:heading]
#=> [%Markup.Markdown.Heading{level: 1, ...}]

Markup.convert!(doc, :markdown)
#=> "# Hello\n"

It renders to :html, :xml (CommonMark XML) and :markdown (CommonMark).

Nodes are the Markup.Markdown.* structs, one per MDExNative.Comrak.* struct with the same fields (Markup.Markdown.Heading, Markup.Markdown.Text, ...). Bare-atom selectors resolve under Markup.Markdown (:code_block is Markup.Markdown.CodeBlock).

Options

  • :extension (keyword/0) - Enable extensions. See comrak's ExtensionOptions for more info and examples. The default value is [].

    • :strikethrough (boolean/0) - Enables the strikethrough extension from the GFM spec. The default value is false.

    • :tagfilter (boolean/0) - Enables the tagfilter extension from the GFM spec. The default value is false.

    • :table (boolean/0) - Enables the table extension from the GFM spec. The default value is false.

    • :grid_table (boolean/0) - Enables Pandoc-style grid tables, whose cells hold blocks and can span columns and rows. See Markup.Markdown.GridTable. The default value is false.

    • :autolink (boolean/0) - Enables the autolink extension from the GFM spec. The default value is false.

    • :tasklist (boolean/0) - Enables the task list extension from the GFM spec. The default value is false.

    • :superscript (boolean/0) - Enables the superscript Comrak extension. The default value is false.

    • :header_id_prefix - Enables the header IDs Comrak extension, with the given ID prefix. The default value is nil.

    • :header_id_prefix_in_href (boolean/0) - When enabled alongside header_id_prefix, the prefix is also applied to the href anchor in the generated link. The default value is false.

    • :footnotes (boolean/0) - Enables the footnotes extension per cmark-gfm The default value is false.

    • :inline_footnotes (boolean/0) - Enables inline footnotes with ^[footnote content] syntax The default value is false.

    • :description_lists (boolean/0) - Enables the description lists extension. The default value is false.

    • :front_matter_delimiter - Enables the front matter extension. The default value is nil.

    • :multiline_block_quotes (boolean/0) - Enables the multiline block quotes extension. The default value is false.

    • :alerts (boolean/0) - Enables GitHub style alerts. The default value is false.

    • :ofm_callouts (boolean/0) - Enables Obsidian callouts with arbitrary types, metadata, Markdown titles, and fold markers. Takes precedence over alerts; folding behavior requires application CSS/JavaScript. The default value is false.

    • :ofm_comments (boolean/0) - Enables Obsidian %% comments. Like Obsidian, rendered output (HTML, Slack, and Delta) omits them; Markdown output and the document tree (AST, JSON, and XML) keep them. The default value is false.

    • :ofm_block_ids (boolean/0) - Enables Obsidian block identifiers: a trailing ^name becomes a Markup.Markdown.BlockId, and HTML renders it as id="^name" on the block it ends. The default value is false.

    • :math_dollars (boolean/0) - Enables math using dollar syntax. The default value is false.

    • :math_dollars_block (boolean/0) - With :math_dollars, parses lines between $$ fences as a display math block, Markup.Markdown.MathBlock, instead of a paragraph of inline math. The default value is false.

    • :math_latex (boolean/0) - Enables math using LaTeX-style (...) and [...] delimiters. The default value is false.

    • :math_code (boolean/0) - Enables the math code extension from the GFM spec. The default value is false.

    • :shortcodes (boolean/0) - Phrases wrapped inside of ':' blocks will be replaced with emojis. The default value is false.

    • :wikilinks_title_after_pipe (boolean/0) - Enables wikilinks using title after pipe syntax. The default value is false.

    • :wikilinks_title_before_pipe (boolean/0) - Enables wikilinks using title before pipe syntax. The default value is false.

    • :wikilinks_ignore_embeds (boolean/0) - Leaves ![[url]] embeds as literal text while still parsing ordinary wikilinks. Requires one of the wikilink title options. The default value is false.

    • :underline (boolean/0) - Enables underlines using double underscores. The default value is false.

    • :subscript (boolean/0) - Enables subscript text using single tildes. The default value is false.

    • :spoiler (boolean/0) - Enables spoilers using double vertical bars. The default value is false.

    • :greentext (boolean/0) - Requires at least one space after a > character to generate a blockquote, and restarts blockquote nesting across unique lines of input. The default value is false.

    • :subtext (boolean/0) - Enables Discord-style subtext using curly braces with hyphens: {-text-}. The default value is false.

    • :highlight (boolean/0) - Enables the highlight extension using double equals ==highlighted text== (wraps text in <mark> tags). The default value is false.

    • :insert (boolean/0) - Enables the insert extension using double plus ++inserted text++ (wraps text in <ins> tags). The default value is false.

    • :image_url_rewriter - Wraps embedded image URLs using a string template.

      Example:

      Given this image ![alt text](http://unsafe.com/image.png) and this rewriter:

      image_url_rewriter: "https://example.com?url={@url}"

      Renders <p><img src="https://example.com?url=http://unsafe.com/image.png" alt="alt text" /></p>

      Notes:

      • Assign @url is always passed to the template.
      • Function callback is not supported, only string templates. Transform the Document AST for more complex cases.

      The default value is nil.

    • :link_url_rewriter - Wraps link URLs using a string template.

      Example:

      Given this link [my link](http://unsafe.example.com/bad) and this rewriter:

      link_url_rewriter: "https://safe.example.com/norefer?url={@url}"

      Renders <p><a href="https://safe.example.com/norefer?url=http://unsafe.example.com/bad">my link</a></p>

      Notes:

      • Assign @url is always passed to the template.
      • Function callback is not supported, only string templates. Transform the Document AST for more complex cases.

      The default value is nil.

    • :cjk_friendly_emphasis (boolean/0) - Recognizes many emphasis that appear in CJK contexts but are not recognized by plain CommonMark. The default value is false.

    • :header_attributes (boolean/0) - Enables attributes on headings. The default value is false.

    • :fenced_code_attributes (boolean/0) - Enables attributes on fenced code blocks. The default value is false.

    • :inline_code_attributes (boolean/0) - Enables attributes on inline code spans. The default value is true.

    • :link_attributes (boolean/0) - Enables attributes on links and images. The default value is false.

    • :phoenix_heex (boolean/0) - Enables Phoenix HEEx components and expressions. The default value is false.

    • :block_directive (boolean/0) - Enables the container block directive extension. Uses :::info syntax to create <div class="info"> blocks. The default value is false.

  • :parse (keyword/0) - Configure parsing behavior. See comrak's ParseOptions for more info and examples. The default value is [].

    • :smart (boolean/0) - Punctuation (quotes, full-stops and hyphens) are converted into 'smart' punctuation. The default value is false.

    • :default_info_string - The default info string for fenced code blocks. The default value is nil.

    • :relaxed_tasklist_matching (boolean/0) - Whether or not a simple x or X is used for tasklist or any other symbol is allowed. The default value is false.

    • :relaxed_autolinks (boolean/0) - Relax parsing of autolinks, allow links to be detected inside brackets and allow all url schemes. It is intended to allow a very specific type of autolink detection, such as [this http://and.com that] or {http://foo.com}, on a best can basis. The default value is true.

    • :ignore_setext (boolean/0) - Ignore setext headings in input. The default value is false.

    • :tasklist_in_table (boolean/0) - Parse a tasklist item if it's the only content of a table cell. The default value is false.

    • :leave_footnote_definitions (boolean/0) - Leave footnote definitions inline instead of moving them to the end of the document. The default value is false.

    • :escaped_char_spans (boolean/0) - Wrap escaped characters in a <span> to allow any post-processing to recognize them. The default value is false.

    • :sourcepos_chars (boolean/0) - Convert byte-based columns in Sourcepos to character-based columns, so that multi-byte UTF-8 characters occupy one column instead of multiple. The default value is false.

  • :render (keyword/0) - Configure rendering behavior. See comrak's RenderOptions for more info and examples. The default value is [].

    • :hardbreaks (boolean/0) - Soft line breaks in the input translate into hard line breaks in the output. The default value is false.

    • :github_pre_lang (boolean/0) - GitHub-style <pre lang="xyz"> is used for fenced code blocks with info tags. The default value is false.

    • :full_info_string (boolean/0) - Enable full info strings for code blocks. The default value is false.

    • :width (integer/0) - The wrap column when outputting CommonMark. The default value is 0.

    • :unsafe (boolean/0) - Allow rendering of raw HTML and potentially dangerous links. The default value is false.

    • :escape (boolean/0) - Escape raw HTML instead of clobbering it. The default value is false.

    • :list_style - Set the type of bullet list marker to use. Either one of :dash, :plus, or :star. The default value is :dash.

    • :sourcepos (boolean/0) - Include source position attributes in HTML and XML output. The default value is false.

    • :attributes (boolean/0) - With :unsafe, render the attributes parsed by :header_attributes, :fenced_code_attributes, :inline_code_attributes, and :link_attributes on headings, code blocks (on the <pre>), code spans, links, and images. Without it, :unsafe renders them on links, images, and highlighted code spans only. Attributes can run script like raw HTML can, so sanitize the output of untrusted input. The default value is false.

    • :escaped_char_spans (boolean/0) - Wrap escaped characters in a <span> to allow any post-processing to recognize them. The default value is false.

    • :ignore_empty_links (boolean/0) - Ignore empty links in input. The default value is false.

    • :gfm_quirks (boolean/0) - Enables GFM quirks in HTML output which break CommonMark compatibility. The default value is false.

    • :prefer_fenced (boolean/0) - Prefer fenced code blocks when outputting CommonMark. The default value is false.

    • :figure_with_caption (boolean/0) - Render the image as a figure element with the title as its caption. The default value is false.

    • :tasklist_classes (boolean/0) - Add classes to the output of the tasklist extension. This allows tasklists to be styled. The default value is false.

    • :ol_width (integer/0) - Render ordered list with a minimum marker width. Having a width lower than 3 doesn't do anything. The default value is 1.

    • :experimental_minimize_commonmark (boolean/0) - Minimise escapes used in CommonMark output (-t commonmark) by removing each individually and seeing if the resulting document roundtrips. Brute-force and expensive, but produces nicer output. Note that the result may not in fact be minimal. The default value is false.

    • :compact_html (boolean/0) - Suppress newlines in pretty-printing The default value is false.

    • :alert_style - Controls whether alerts render using specific or semantic HTML elements. The default value is :specific.

    • :grid_table_widths (boolean/0) - Render a <colgroup> in grid tables with each column's share of the total width. The default value is false.

  • :syntax_highlight - Apply syntax highlighting to code blocks. Defaults to nil (disabled).

    The highlighter is compiled into mdex_native; pick it with config :mdex_native, syntax_highlighter: :lumis (or :syntect).

    The default value is nil.

  • :budget - Limits parsing, using [parse_time: milliseconds, parse_memory: bytes]. A parse is cancelled once it runs longer than :parse_time or holds more than :parse_memory bytes of heap. Each limit defaults to no limit. A value of 0 or nil also means no limit. The default value is nil.

:sanitize (see Markup.Sanitize) defaults to MDEx's allowlists; see default_sanitize_options/0.

Syntax highlighting

The highlighter is compiled into mdex_native, and you pick it in config:

config :mdex_native, syntax_highlighter: :lumis   # or :syntect

Then you turn it on per render with syntax_highlight: [engine: :lumis] (Lumis also needs a lumis_wasm_* parser package per language). Without the config, highlighting returns {:error, %MDExNative.SyntaxHighlightError{}}.

HEEx

Markup.to_heex/2 and ~MU"..."markdownHEEX enable extension: [phoenix_heex: true] and render: [unsafe: true], so HEEx components (<.link>) and expressions ({@name}) pass through. Braces in code are escaped so HEEx reads them as text.

Summary

Functions

The default :sanitize options, from MDEx.Document.default_sanitize_options/0

Functions

default_sanitize_options()

The default :sanitize options, from MDEx.Document.default_sanitize_options/0:

[
  tags: ["a", "abbr", "acronym", "area", "article", "aside", "b", "bdi", "bdo",
   "blockquote", "br", "caption", "center", "cite", "code", "col", "colgroup",
   "data", "dd", "del", "details", "dfn", "div", "dl", "dt", "em", "figcaption",
   "figure", "footer", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup",
   "hr", "i", "img", "ins", "kbd", "li", "map", "mark", "nav", "ol", "p", "pre",
   "q", "rp", "rt", "rtc", "ruby", "s", "samp", "small", "span", "strike",
   "strong", "sub", "summary", "sup", "table", "tbody", "td", "th", "thead",
   "time", "tr", "tt", "u", "ul", "var", "wbr"],
  add_tags: [],
  rm_tags: [],
  clean_content_tags: ["script", "style"],
  add_clean_content_tags: [],
  rm_clean_content_tags: [],
  tag_attributes: %{
    "a" => ["href", "hreflang"],
    "bdo" => ["dir"],
    "blockquote" => ["cite"],
    "code" => ["class", "style", "translate", "tabindex"],
    "col" => ["align", "char", "charoff", "span"],
    "colgroup" => ["align", "char", "charoff", "span"],
    "del" => ["cite", "datetime"],
    "div" => ["class", "style", "data-line"],
    "hr" => ["align", "size", "width"],
    "img" => ["align", "alt", "height", "src", "width"],
    "ins" => ["cite", "datetime"],
    "ol" => ["start"],
    "pre" => ["class", "style"],
    "q" => ["cite"],
    "span" => ["class", "style", "data-line"],
    "table" => ["align", "char", "charoff", "summary"],
    "tbody" => ["align", "char", "charoff"],
    "td" => ["align", "char", "charoff", "colspan", "headers", "rowspan"],
    "tfoot" => ["align", "char", "charoff"],
    "th" => ["align", "char", "charoff", "colspan", "headers", "rowspan",
     "scope"],
    "thead" => ["align", "char", "charoff"],
    "tr" => ["align", "char", "charoff"]
  },
  add_tag_attributes: %{},
  rm_tag_attributes: %{},
  tag_attribute_values: %{},
  add_tag_attribute_values: %{},
  rm_tag_attribute_values: %{},
  set_tag_attribute_values: %{},
  set_tag_attribute_value: %{},
  rm_set_tag_attribute_value: %{},
  generic_attribute_prefixes: [],
  add_generic_attribute_prefixes: [],
  rm_generic_attribute_prefixes: [],
  generic_attributes: ["lang", "title"],
  add_generic_attributes: [],
  rm_generic_attributes: [],
  url_schemes: ["bitcoin", "ftp", "ftps", "geo", "http", "https", "im", "irc",
   "ircs", "magnet", "mailto", "mms", "mx", "news", "nntp", "openpgp4fpr",
   "sip", "sms", "smsto", "ssh", "tel", "url", "webcal", "wtai", "xmpp"],
  add_url_schemes: [],
  rm_url_schemes: [],
  url_relative: :passthrough,
  link_rel: "noopener noreferrer",
  allowed_classes: %{},
  add_allowed_classes: %{},
  rm_allowed_classes: %{},
  strip_comments: true,
  id_prefix: nil
]