cmark v0.7.0 Cmark

Compiles Markdown formatted text into HTML or one of the other supported target formats.

Provides:

HTML

XML

Manpage

CommonMark

LaTeX

Summary

Types

A callback function which can return anything

A list of atoms describing the options to use (for details check documentation of any function using options)

Either an options list or a callback function

List of strings

Either a string or a list of strings

Functions

Compiles one or more (list) Markdown documents to CommonMark and returns result

Compiles one or more (list) Markdown documents to CommonMark using provided options and returns result

Compiles one or more (list) Markdown documents to CommonMark using provided options and calls function with result

Compiles a list of Markdown documents to CommonMark and calls function for each item

Compiles a list of Markdown documents to CommonMark using provided options and calls function for each item

Compiles one or more (list) Markdown documents to HTML and returns result

Compiles one or more (list) Markdown documents to HTML using provided options and returns result

Compiles one or more (list) Markdown documents to HTML using provided options and calls function with result

Compiles a list of Markdown documents to HTML and calls function for each item

Compiles a list of Markdown documents to HTML using provided options and calls function for each item

Compiles one or more (list) Markdown documents to LaTeX and returns result

Compiles one or more (list) Markdown documents to LaTeX using provided options and returns result

Compiles one or more (list) Markdown documents to LaTeX using provided options and calls function with result

Compiles a list of Markdown documents to LaTeX and calls function for each item

Compiles a list of Markdown documents to LaTeX using provided options and calls function for each item

Compiles one or more (list) Markdown documents to Manpage and returns result

Compiles one or more (list) Markdown documents to Manpage using provided options and returns result

Compiles one or more (list) Markdown documents to Manpage using provided options and calls function with result

Compiles a list of Markdown documents to Manpage and calls function for each item

Compiles a list of Markdown documents to Manpage using provided options and calls function for each item

Compiles one or more (list) Markdown documents to XML and returns result

Compiles one or more (list) Markdown documents to XML using provided options and returns result

Compiles one or more (list) Markdown documents to XML using provided options and calls function with result

Compiles a list of Markdown documents to XML and calls function for each item

Compiles a list of Markdown documents to XML using provided options and calls function for each item

Types

callback_fun()
callback_fun() :: (function -> any)

A callback function which can return anything

options_list()
options_list() :: [atom]

A list of atoms describing the options to use (for details check documentation of any function using options)

options_or_callback()
options_or_callback() :: options_list | callback_fun

Either an options list or a callback function

string_list()
string_list() :: [String.t]

List of strings

string_or_list()
string_or_list() :: String.t | string_list

Either a string or a list of strings

Functions

to_commonmark(data)
to_commonmark(string_or_list) :: string_or_list

Compiles one or more (list) Markdown documents to CommonMark and returns result.

  • data is either a string or a list

Examples

iex> "test" |> Cmark.to_commonmark
"test\n"

iex> ["test 1", "test 2"] |> Cmark.to_commonmark
["test 1\n", "test 2\n"]
to_commonmark(data, options_or_callback)

Compiles one or more (list) Markdown documents to CommonMark using provided options and returns result.

  • data is either a string or a list
  • options_or_callback is either an option list or a callback function

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> Cmark.to_commonmark(~s(Use option to enable "smart" quotes.), [:smart])
"Use option to enable “smart” quotes.\n"

iex> Cmark.to_commonmark([~s(Something "smart"), ~s(em---dashed)], [:smart])
["Something “smart”\n", "em—dashed\n"]

Compiles one or more (list) Markdown documents to CommonMark and calls function with result.

Examples

iex> callback = fn (result) -> "CommonMark is #{result}" |> String.strip end
iex> Cmark.to_commonmark("test", callback)
"CommonMark is test"

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("%%joiner%%")
iex> end
iex> Cmark.to_commonmark(["list", "test"], callback)
"list%%joiner%%test"
to_commonmark(data, callback, options)

Compiles one or more (list) Markdown documents to CommonMark using provided options and calls function with result.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> {:ok, result} end
iex> Cmark.to_commonmark(~s(Something "smart" and ... ---you get it!), callback, [:smart])
{:ok, "Something “smart” and … —you get it\\!\n"}

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("%%joiner%%")
iex> end
iex> Cmark.to_commonmark(["en-dash --", "ellipsis..."], callback, [:smart])
"en-dash –%%joiner%%ellipsis…"
to_commonmark_each(data, callback)
to_commonmark_each(string_or_list, callback_fun) :: list

Compiles a list of Markdown documents to CommonMark and calls function for each item.

  • data is either a string or a list
  • callback is a callback function

Examples

iex> callback = fn (result) -> "CommonMark is #{result |> String.strip}" end
iex> Cmark.to_commonmark_each(["list", "test"], callback)
["CommonMark is list", "CommonMark is test"]
to_commonmark_each(data, callback, options)
to_commonmark_each(string_or_list, callback_fun, options_list) :: list

Compiles a list of Markdown documents to CommonMark using provided options and calls function for each item.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> "CommonMark is #{result |> String.strip}" end
iex> Cmark.to_commonmark_each(["list --", "test..."], callback, [:smart])
["CommonMark is list –", "CommonMark is test…"]
to_html(data)

Compiles one or more (list) Markdown documents to HTML and returns result.

  • data is either a string or a list

Examples

iex> "test" |> Cmark.to_html
"<p>test</p>\n"

iex> ["test 1", "test 2"] |> Cmark.to_html
["<p>test 1</p>\n", "<p>test 2</p>\n"]
to_html(data, options_or_callback)

Compiles one or more (list) Markdown documents to HTML using provided options and returns result.

  • data is either a string or a list
  • options_or_callback is either an option list or a callback function

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> Cmark.to_html(~s(Use option to enable "smart" quotes.), [:smart])
"<p>Use option to enable “smart” quotes.</p>\n"

iex> Cmark.to_html([~s(Something "smart"), ~s(em---dashed)], [:smart])
["<p>Something “smart”</p>\n", "<p>em—dashed</p>\n"]

Compiles one or more (list) Markdown documents to HTML and calls function with result.

Examples

iex> callback = fn (result) -> "HTML is #{result}" |> String.strip end
iex> Cmark.to_html("test", callback)
"HTML is <p>test</p>"

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("<hr>")
iex> end
iex> Cmark.to_html(["list", "test"], callback)
"<p>list</p><hr><p>test</p>"
to_html(data, callback, options)

Compiles one or more (list) Markdown documents to HTML using provided options and calls function with result.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> {:ok, result} end
iex> Cmark.to_html(~s(Something "smart" and ... ---you get it!), callback, [:smart])
{:ok, "<p>Something “smart” and … —you get it!</p>\n"}

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("<hr>")
iex> end
iex> Cmark.to_html(["en-dash --", "ellipsis..."], callback, [:smart])
"<p>en-dash –</p><hr><p>ellipsis…</p>"
to_html_each(data, callback)
to_html_each(string_or_list, callback_fun) :: list

Compiles a list of Markdown documents to HTML and calls function for each item.

  • data is either a string or a list
  • callback is a callback function

Examples

iex> callback = fn (result) -> "HTML is #{result |> String.strip}" end
iex> Cmark.to_html_each(["list", "test"], callback)
["HTML is <p>list</p>", "HTML is <p>test</p>"]
to_html_each(data, callback, options)
to_html_each(string_or_list, callback_fun, options_list) :: list

Compiles a list of Markdown documents to HTML using provided options and calls function for each item.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> "HTML is #{result |> String.strip}" end
iex> Cmark.to_html_each(["list --", "test..."], callback, [:smart])
["HTML is <p>list –</p>", "HTML is <p>test…</p>"]
to_latex(data)

Compiles one or more (list) Markdown documents to LaTeX and returns result.

  • data is either a string or a list

Examples

iex> "test" |> Cmark.to_latex
"test\n"

iex> ["test 1", "test 2"] |> Cmark.to_latex
["test 1\n", "test 2\n"]
to_latex(data, options_or_callback)

Compiles one or more (list) Markdown documents to LaTeX using provided options and returns result.

  • data is either a string or a list
  • options_or_callback is either an option list or a callback function

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> Cmark.to_latex(~s(Use option to enable "smart" quotes.), [:smart])
"Use option to enable ``smart'' quotes.\n"

iex> Cmark.to_latex([~s(Something "smart"), ~s(em---dashed)], [:smart])
["Something ``smart''\n", "em---dashed\n"]

Compiles one or more (list) Markdown documents to LaTeX and calls function with result.

Examples

iex> callback = fn (result) -> "LaTeX is #{result}" |> String.strip end
iex> Cmark.to_latex("test", callback)
"LaTeX is test"

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("%%joiner%%")
iex> end
iex> Cmark.to_latex(["list", "test"], callback)
"list%%joiner%%test"
to_latex(data, callback, options)

Compiles one or more (list) Markdown documents to LaTeX using provided options and calls function with result.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> {:ok, result} end
iex> Cmark.to_latex(~s(Something "smart" and ... ---you get it!), callback, [:smart])
{:ok, "Something ``smart'' and \\ldots{} ---you get it!\n"}

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("%%joiner%%")
iex> end
iex> Cmark.to_latex(["en-dash --", "ellipsis..."], callback, [:smart])
"en-dash --%%joiner%%ellipsis\\ldots{}"
to_latex_each(data, callback)
to_latex_each(string_or_list, callback_fun) :: list

Compiles a list of Markdown documents to LaTeX and calls function for each item.

  • data is either a string or a list
  • callback is a callback function

Examples

iex> callback = fn (result) -> "LaTeX is #{result |> String.strip}" end
iex> Cmark.to_latex_each(["list", "test"], callback)
["LaTeX is list", "LaTeX is test"]
to_latex_each(data, callback, options)
to_latex_each(string_or_list, callback_fun, options_list) :: list

Compiles a list of Markdown documents to LaTeX using provided options and calls function for each item.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> "LaTeX is #{result |> String.strip}" end
iex> Cmark.to_latex_each(["list --", "test..."], callback, [:smart])
["LaTeX is list --", "LaTeX is test\\ldots{}"]
to_man(data)

Compiles one or more (list) Markdown documents to Manpage and returns result.

  • data is either a string or a list

Examples

iex> "test" |> Cmark.to_man
".PP\ntest\n"

iex> ["test 1", "test 2"] |> Cmark.to_man
[".PP\ntest 1\n", ".PP\ntest 2\n"]
to_man(data, options_or_callback)

Compiles one or more (list) Markdown documents to Manpage using provided options and returns result.

  • data is either a string or a list
  • options_or_callback is either an option list or a callback function

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> Cmark.to_man(~s(Use option to enable "smart" quotes.), [:smart])
".PP\nUse option to enable \\[lq]smart\\[rq] quotes.\n"

iex> Cmark.to_man([~s(Something "smart"), ~s(em---dashed)], [:smart])
[".PP\nSomething \\[lq]smart\\[rq]\n", ".PP\nem\\[em]dashed\n"]

Compiles one or more (list) Markdown documents to Manpage and calls function with result.

Examples

iex> callback = fn (result) -> "Manpage is #{result}" |> String.strip end
iex> Cmark.to_man("test", callback)
"Manpage is .PP\ntest"

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("%%joiner%%")
iex> end
iex> Cmark.to_man(["list", "test"], callback)
".PP\nlist%%joiner%%.PP\ntest"
to_man(data, callback, options)

Compiles one or more (list) Markdown documents to Manpage using provided options and calls function with result.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> {:ok, result} end
iex> Cmark.to_man(~s(Something "smart" and ... ---you get it!), callback, [:smart])
{:ok, ".PP\nSomething \\[lq]smart\\[rq] and … \\[em]you get it!\n"}

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("%%joiner%%")
iex> end
iex> Cmark.to_man(["en-dash --", "ellipsis..."], callback, [:smart])
".PP\nen\\-dash \\[en]%%joiner%%.PP\nellipsis…"
to_man_each(data, callback)
to_man_each(string_or_list, callback_fun) :: list

Compiles a list of Markdown documents to Manpage and calls function for each item.

  • data is either a string or a list
  • callback is a callback function

Examples

iex> callback = fn (result) -> "Manpage is #{result |> String.strip}" end
iex> Cmark.to_man_each(["list", "test"], callback)
["Manpage is .PP\nlist", "Manpage is .PP\ntest"]
to_man_each(data, callback, options)
to_man_each(string_or_list, callback_fun, options_list) :: list

Compiles a list of Markdown documents to Manpage using provided options and calls function for each item.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> "Manpage is #{result |> String.strip}" end
iex> Cmark.to_man_each(["list --", "test..."], callback, [:smart])
["Manpage is .PP\nlist \\[en]", "Manpage is .PP\ntest…"]
to_xml(data)

Compiles one or more (list) Markdown documents to XML and returns result.

  • data is either a string or a list

Examples

iex> "test" |> Cmark.to_xml
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>test</text>\n  </paragraph>
</document>\n"

iex> ["test 1", "test 2"] |> Cmark.to_xml
["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>test 1</text>\n  </paragraph>
</document>\n",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>test 2</text>\n  </paragraph>
</document>\n"]
to_xml(data, options_or_callback)

Compiles one or more (list) Markdown documents to XML using provided options and returns result.

  • data is either a string or a list
  • options_or_callback is either an option list or a callback function

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> Cmark.to_xml(~s(Use option to enable "smart" quotes.), [:smart])
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>Use option to enable </text>
    <text>“</text>\n    <text>smart</text>\n    <text>”</text>\n    <text> quotes</text>\n    <text>.</text>
  </paragraph>\n</document>\n"

iex> Cmark.to_xml([~s(Something "smart"), ~s(em---dashed)], [:smart])
["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>Something </text>
    <text>“</text>\n    <text>smart</text>\n    <text>”</text>\n  </paragraph>\n</document>\n",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>em</text>
    <text>—</text>\n    <text>dashed</text>\n  </paragraph>\n</document>\n"]

Compiles one or more (list) Markdown documents to XML and calls function with result.

Examples

iex> callback = fn (result) -> "XML is #{result}" |> String.strip end
iex> Cmark.to_xml("test", callback)
"XML is <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>test</text>\n  </paragraph>
</document>"

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("<joiner>")
iex> end
iex> Cmark.to_xml(["list", "test"], callback)
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>list</text>\n  </paragraph>
</document><joiner><?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>test</text>\n  </paragraph>
</document>"
to_xml(data, callback, options)

Compiles one or more (list) Markdown documents to XML using provided options and calls function with result.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> {:ok, result} end
iex> Cmark.to_xml(~s(Something "smart" and ... ---you get it!), callback, [:smart])
{:ok, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>Something </text>
    <text>“</text>\n    <text>smart</text>\n    <text>”</text>\n    <text> and </text>\n    <text>…</text>
    <text> </text>\n    <text>—</text>\n    <text>you get it</text>\n    <text>!</text>\n  </paragraph>
</document>\n"}

iex> callback = fn (results) ->
iex>   Enum.map(results, &String.strip/1) |> Enum.join("<joiner>")
iex> end
iex> Cmark.to_xml(["en-dash --", "ellipsis..."], callback, [:smart])
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>en</text>\n    <text>-</text>
    <text>dash </text>\n    <text>–</text>\n  </paragraph>
</document><joiner><?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>ellipsis</text>\n    <text>…</text>
  </paragraph>\n</document>"
to_xml_each(data, callback)
to_xml_each(string_or_list, callback_fun) :: list

Compiles a list of Markdown documents to XML and calls function for each item.

  • data is either a string or a list
  • callback is a callback function

Examples

iex> callback = fn (result) -> "XML is #{result |> String.strip}" end
iex> Cmark.to_xml_each(["list", "test"], callback)
["XML is <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>list</text>\n  </paragraph>
</document>",
"XML is <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>test</text>\n  </paragraph>
</document>"]
to_xml_each(data, callback, options)
to_xml_each(string_or_list, callback_fun, options_list) :: list

Compiles a list of Markdown documents to XML using provided options and calls function for each item.

  • data is either a string or a list
  • callback is a callback function
  • options is an option list

Available options are:

  • :sourcepos Include a data-sourcepos attribute on all block elements.
  • :hardbreaks Render softbreak elements as hard line breaks.
  • :nobreaks Render softbreak elements as spaces.
  • :normalize Normalize tree by consolidating adjacent text nodes.
  • :smart Convert straight quotes to curly, --- to em dashes, -- to en dashes.
  • :validate_utf8 Validate UTF-8 in the input before parsing, replacing illegal sequences with the replacement character U+FFFD.
  • :safe Suppress raw HTML and unsafe links (javascript:, vbscript:, file:, and data:, except for image/png, image/gif, image/jpeg, or image/webp mime types). Raw HTML is replaced by a placeholder HTML comment. Unsafe links are replaced by empty strings.

Examples

iex> callback = fn (result) -> "XML is #{result |> String.strip}" end
iex> Cmark.to_xml_each(["list --", "test..."], callback, [:smart])
["XML is <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>list </text>\n    <text>–</text>
  </paragraph>\n</document>",
"XML is <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">
<document xmlns=\"http://commonmark.org/xml/1.0\">\n  <paragraph>\n    <text>test</text>\n    <text>…</text>
  </paragraph>\n</document>"]