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 isfalse.:tagfilter(boolean/0) - Enables the tagfilter extension from the GFM spec. The default value isfalse.:table(boolean/0) - Enables the table extension from the GFM spec. The default value isfalse.:grid_table(boolean/0) - Enables Pandoc-style grid tables, whose cells hold blocks and can span columns and rows. SeeMarkup.Markdown.GridTable. The default value isfalse.:autolink(boolean/0) - Enables the autolink extension from the GFM spec. The default value isfalse.:tasklist(boolean/0) - Enables the task list extension from the GFM spec. The default value isfalse.:superscript(boolean/0) - Enables the superscript Comrak extension. The default value isfalse.:header_id_prefix- Enables the header IDs Comrak extension, with the given ID prefix. The default value isnil.:header_id_prefix_in_href(boolean/0) - When enabled alongsideheader_id_prefix, the prefix is also applied to thehrefanchor in the generated link. The default value isfalse.:footnotes(boolean/0) - Enables the footnotes extension per cmark-gfm The default value isfalse.:inline_footnotes(boolean/0) - Enables inline footnotes with ^[footnote content] syntax The default value isfalse.:description_lists(boolean/0) - Enables the description lists extension. The default value isfalse.:front_matter_delimiter- Enables the front matter extension. The default value isnil.:multiline_block_quotes(boolean/0) - Enables the multiline block quotes extension. The default value isfalse.:alerts(boolean/0) - Enables GitHub style alerts. The default value isfalse.: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 isfalse.: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 isfalse.:ofm_block_ids(boolean/0) - Enables Obsidian block identifiers: a trailing^namebecomes aMarkup.Markdown.BlockId, and HTML renders it asid="^name"on the block it ends. The default value isfalse.:math_dollars(boolean/0) - Enables math using dollar syntax. The default value isfalse.: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 isfalse.:math_latex(boolean/0) - Enables math using LaTeX-style(...)and[...]delimiters. The default value isfalse.:math_code(boolean/0) - Enables the math code extension from the GFM spec. The default value isfalse.:shortcodes(boolean/0) - Phrases wrapped inside of ':' blocks will be replaced with emojis. The default value isfalse.:wikilinks_title_after_pipe(boolean/0) - Enables wikilinks using title after pipe syntax. The default value isfalse.:wikilinks_title_before_pipe(boolean/0) - Enables wikilinks using title before pipe syntax. The default value isfalse.: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 isfalse.:underline(boolean/0) - Enables underlines using double underscores. The default value isfalse.:subscript(boolean/0) - Enables subscript text using single tildes. The default value isfalse.:spoiler(boolean/0) - Enables spoilers using double vertical bars. The default value isfalse.: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 isfalse.:subtext(boolean/0) - Enables Discord-style subtext using curly braces with hyphens: {-text-}. The default value isfalse.:highlight(boolean/0) - Enables the highlight extension using double equals ==highlighted text== (wraps text in <mark> tags). The default value isfalse.:insert(boolean/0) - Enables the insert extension using double plus ++inserted text++ (wraps text in <ins> tags). The default value isfalse.:image_url_rewriter- Wraps embedded image URLs using a string template.Example:
Given this image
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
@urlis 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.- Assign
: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
@urlis 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.- Assign
:cjk_friendly_emphasis(boolean/0) - Recognizes many emphasis that appear in CJK contexts but are not recognized by plain CommonMark. The default value isfalse.:header_attributes(boolean/0) - Enables attributes on headings. The default value isfalse.:fenced_code_attributes(boolean/0) - Enables attributes on fenced code blocks. The default value isfalse.:inline_code_attributes(boolean/0) - Enables attributes on inline code spans. The default value istrue.:link_attributes(boolean/0) - Enables attributes on links and images. The default value isfalse.:phoenix_heex(boolean/0) - Enables Phoenix HEEx components and expressions. The default value isfalse.:block_directive(boolean/0) - Enables the container block directive extension. Uses:::infosyntax to create<div class="info">blocks. The default value isfalse.
: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 isfalse.:default_info_string- The default info string for fenced code blocks. The default value isnil.:relaxed_tasklist_matching(boolean/0) - Whether or not a simplexorXis used for tasklist or any other symbol is allowed. The default value isfalse.: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 istrue.:ignore_setext(boolean/0) - Ignore setext headings in input. The default value isfalse.:tasklist_in_table(boolean/0) - Parse a tasklist item if it's the only content of a table cell. The default value isfalse.:leave_footnote_definitions(boolean/0) - Leave footnote definitions inline instead of moving them to the end of the document. The default value isfalse.:escaped_char_spans(boolean/0) - Wrap escaped characters in a <span> to allow any post-processing to recognize them. The default value isfalse.: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 isfalse.
: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 isfalse.:github_pre_lang(boolean/0) - GitHub-style<pre lang="xyz">is used for fenced code blocks with info tags. The default value isfalse.:full_info_string(boolean/0) - Enable full info strings for code blocks. The default value isfalse.:width(integer/0) - The wrap column when outputting CommonMark. The default value is0.:unsafe(boolean/0) - Allow rendering of raw HTML and potentially dangerous links. The default value isfalse.:escape(boolean/0) - Escape raw HTML instead of clobbering it. The default value isfalse.: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 isfalse.:attributes(boolean/0) - With:unsafe, render the attributes parsed by:header_attributes,:fenced_code_attributes,:inline_code_attributes, and:link_attributeson headings, code blocks (on the<pre>), code spans, links, and images. Without it,:unsaferenders 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 isfalse.:escaped_char_spans(boolean/0) - Wrap escaped characters in a<span>to allow any post-processing to recognize them. The default value isfalse.:ignore_empty_links(boolean/0) - Ignore empty links in input. The default value isfalse.:gfm_quirks(boolean/0) - Enables GFM quirks in HTML output which break CommonMark compatibility. The default value isfalse.:prefer_fenced(boolean/0) - Prefer fenced code blocks when outputting CommonMark. The default value isfalse.:figure_with_caption(boolean/0) - Render the image as a figure element with the title as its caption. The default value isfalse.:tasklist_classes(boolean/0) - Add classes to the output of the tasklist extension. This allows tasklists to be styled. The default value isfalse.: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 is1.: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 isfalse.:compact_html(boolean/0) - Suppress newlines in pretty-printing The default value isfalse.: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 isfalse.
:syntax_highlight- Apply syntax highlighting to code blocks. Defaults tonil(disabled).The highlighter is compiled into
mdex_native; pick it withconfig :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_timeor holds more than:parse_memorybytes of heap. Each limit defaults to no limit. A value of0ornilalso means no limit. The default value isnil.
: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 :syntectThen 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
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
]