Atom Tweaks v0.1.0+c1f5afa AtomTweaks.Markdown View Source

A structure that represents a chunk of Markdown text in memory.

For the database type, see AtomTweaks.Ecto.Markdown instead.

The structure contains both the raw Markdown text and, potentially, the rendered html. Upon a request to render the structure using either to_html/1 or to_iodata/1, the html field is given preference and returned unchanged, if available. If the html value is nil, then the contents of the text field are rendered using AtomTweaksWeb.MarkdownEngine.render/1 and returned.

This type requires special handling in forms because Phoenix's form builder functions call Phoenix.HTML.html_escape/1 on all field values, which returns the html field on this type. But what we want when we show an AtomTweaks.Markdown value in a form is the text field.

Link to this section Summary

Types

An AtomTweaks.Markdown struct or a string of Markdown text.

t()

Functions

Renders the supplied Markdown as HTML.

Renders a chunk of Markdown to its iodata representation.

Link to this section Types

Specs

markdown() :: %AtomTweaks.Markdown{html: term(), text: term()} | String.t()

An AtomTweaks.Markdown struct or a string of Markdown text.

Specs

t() :: %AtomTweaks.Markdown{html: nil | String.t(), text: String.t()}

Link to this section Functions

Specs

to_html(markdown()) :: binary()

Renders the supplied Markdown as HTML.

Examples

Render Markdown from a string:

iex> AtomTweaks.Markdown.to_html("# Foo")
"<h1>Foo</h1>
"

Render Markdown from an unrendered Markdown struct:

iex> AtomTweaks.Markdown.to_html(%AtomTweaks.Markdown{text: "# Foo"})
"<h1>Foo</h1>
"

Passes already rendered Markdown through unchanged:

iex> AtomTweaks.Markdown.to_html(%AtomTweaks.Markdown{html: "<p>foo</p>"})
"<p>foo</p>"

Returns an empty string for anything that isn't a string or a Markdown struct:

iex> AtomTweaks.Markdown.to_html(5)
""

Specs

to_iodata(markdown()) :: iodata()

Renders a chunk of Markdown to its iodata representation.