Packages
earmark
1.1.1
1.5.0-pre1
retired
1.5.0-pre
retired
1.4.49
retired
1.4.48
retired
1.4.47
retired
1.4.46
retired
1.4.45
retired
1.4.44
retired
1.4.43
retired
1.4.42
retired
1.4.41
retired
1.4.40
retired
1.4.39
retired
1.4.38
retired
1.4.37
retired
1.4.36
retired
1.4.35
retired
1.4.34
retired
1.4.33
retired
1.4.32
retired
1.4.31
retired
1.4.30
retired
1.4.29
retired
1.4.28
retired
1.4.27
retired
1.4.26
retired
1.4.25
retired
1.4.24
retired
1.4.23
retired
1.4.22
retired
1.4.21
retired
1.4.20
retired
1.4.19
retired
1.4.18
retired
1.4.17
retired
1.4.16
retired
1.4.16-pre2
retired
1.4.16-pre1
retired
1.4.16-pre
retired
1.4.15
retired
1.4.14
retired
1.4.13
retired
1.4.12
retired
1.4.10
retired
1.4.9
retired
1.4.8
retired
1.4.7
retired
1.4.6
retired
1.4.5
retired
1.4.4
retired
1.4.3
retired
1.4.2
retired
1.4.1
retired
1.4.0
retired
1.3.6
retired
1.3.5
retired
1.3.4
retired
1.3.3
retired
1.3.2
retired
1.3.1
retired
1.3.0
retired
1.2.6
retired
1.2.5
retired
1.2.4
retired
1.2.3
retired
1.2.2
retired
1.2.1
retired
1.2.0
retired
1.1.1
retired
1.1.0
retired
1.0.3
retired
1.0.2
retired
1.0.1
retired
1.0.0
retired
0.2.1
retired
0.2.0
retired
0.1.19
retired
0.1.18
retired
0.1.17
retired
0.1.16
retired
0.1.15
retired
0.1.14
retired
0.1.13
retired
0.1.12
retired
0.1.10
retired
0.1.9
retired
0.1.8
retired
0.1.7
retired
0.1.6
retired
0.1.5
retired
0.1.4
retired
0.1.3
retired
0.1.2
retired
0.1.1
retired
0.1.0
retired
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as a command-line tool (run mix escript.build first). Output generation is pluggable.
Retired package: Deprecated - Earmark is no longer maintained. Migrate to a replacement, for example MDEx (https://hex.pm/packages/mdex).
Current section
Files
Jump to
Current section
Files
README.md
# Earmark—A Pure Elixir Markdown Processor[](https://travis-ci.org/pragdave/earmark)[](https://hex.pm/packages/earmark)<!-- moduledoc: Earmark -->## Dependency { :earmark, "> x.y.z" }## Usage### API * `Earmark.as_html` {:ok, html_doc, []} = Earmark.as_html(markdown) {:error, html_doc, error_messages} = Earmark.as_html(markdown) * `Earmark.as_html!` html_doc = Earmark.as_html!(markdown, options) Any error messages are printed to _stderr_.#### Options:#Options can be passed into `as_html` or `as_html!` according to the documentation. html_doc = Earmark.as_html!(markdown) html_doc = Earmark.as_html!(markdown, options)Formats the error_messages returned by `as_html` and adds the filename to each.Then prints them to stderr and just returns the html_doc### Command line $ mix escript.build $ ./earmark file.mdSome options defined in the `Earmark.Options` struct can be specified as command line switches.Use $ ./earmark --helpto find out more, but here is a short example $ ./earmark --smartypants false --code-class-prefix "a- b-" file.mdwill call Earmark.as_html!( ..., %Earmark.Options{smartypants: false, code_class_prefix: "a- b-"})## SupportsStandard [Gruber markdown][gruber].[gruber]: <http://daringfireball.net/projects/markdown/syntax>## Extensions### TablesGithub Flavored Markdown tables are supported State | Abbrev | Capital ----: | :----: | ------- Texas | TX | Austin Maine | ME | AugustaTables may have leading and trailing vertical bars on each line | State | Abbrev | Capital | | ----: | :----: | ------- | | Texas | TX | Austin | | Maine | ME | Augusta |Tables need not have headers, in which case all column alignmentsdefault to left. | Texas | TX | Austin | | Maine | ME | Augusta |Currently we assume there are always spaces around interior verticalbars. It isn't clear what the expectation is.### Adding HTML attributes with the IAL extensionHTML attributes can be added to any block-level element. We usethe Kramdown syntax: add the line `{:` _attrs_ `}` following the block._attrs_ can be one or more of:* `.className`* `#id`* name=value, name="value", or name='value'Malformed attributes are ignored and a warning is issued to stderr.If you need to render IAL-like test verbatim escape it:`{:alpha, 42}`This of course is not necessary in code blocks or text linescontaining an IAL-like string, as in`the returned tuple should be {:error, "I wish you hadn't done that"}`For example: # Warning {: .red} Do not turn off the engine if you are at altitude. {: .boxed #warning spellcheck="true"}## Limitations* Nested block-level HTML is correctly handled only if each HTML tag appears on its own line. So <div> <div> hello </div> </div> will work. However. the following won't <div><div> hello </div></div>* John Gruber's tests contain an ambiguity when it comes to lines that might be the start of a list inside paragraphs. One test says that This is the text * of a paragraph that I wrote is a single paragraph. The "*" is not significant. However, another test has * A list item * an another and expects this to be a nested list. But, in reality, the second could just be the continuation of a paragraph. I've chosen always to use the second interpretation—a line that looks like a list item will always be a list item.* Rendering of block and inline elements. Block or void HTML elements that are at the absolute beginning of a line end the preceding paragraph. Thusly mypara <hr> Becomes <p>mypara</p> <hr> While mypara <hr> will be transformed into <p>mypara <hr></p>## Integration### Syntax HighlightningAll backquoted or fenced code blocks with a language string are rendered with the givenlanguage as a _class_ attribute of the _code_ tag.For example: ```elixir @tag :hello ```will be rendered as <pre><code class="elixir">...If you want to integrate with a syntax highlighter with different conventions you can add more classes by specifying prefixes that will beput before the language string.Prism.js for example needs a class `language-elixir`. In order to achieve that goal you can add `language-`as a `code_class_prefix` to `Earmark.Options`.In the following example we want more than one additional class, so we add more prefixes. Earmark.as_html!(..., %Earmark.Options{code_class_prefix: "lang- language-"})which is rendering <pre><code class="elixir lang-elixir language-elixir">...As for all other options `code_class_prefix` can be passed into the `earmark` executable as follows: earmark --code-class-prefix "language- lang-" ...## Security Please be aware that Markdown is not a secure format. It produces HTML from Markdown and HTML. It is your job to sanitize and or filter the output of `Markdown.html` if you cannot trust the input and are to serve the produced HTML on the Web.## AuthorCopyright © 2014 Dave Thomas, The Pragmatic Programmers@/+pragdave, dave@pragprog.comLicensed under the same terms as Elixir, which is Apache 2.0.<!-- endmoduledoc: Earmark --># Details<!-- doc: Earmark.as_html -->Given a markdown document (as either a list of lines ora string containing newlines), returns a tuple containing either`{:ok, html_doc}`, or `{:error, html_doc, error_messages}`Where `html_doc` is an HTML representation of the markdown document and`error_messages` is a list of strings representing information concerningthe errors that occurred during parsing.The options are a `%Earmark.Options{}` structure:* `renderer`: ModuleName The module used to render the final document. Defaults to `Earmark.HtmlRenderer`* `gfm`: boolean True by default. Turns on Github Flavored Markdown extensions* `breaks`: boolean Only applicable if `gfm` is enabled. Makes all line breaks significant (so every line in the input is a new line in the output.* `smartypants`: boolean Turns on smartypants processing, so quotes become curly, two or three hyphens become en and em dashes, and so on. True by default.So, to format the document in `original` and disable smartypants,you'd call alias Earmark.Options Earmark.as_html(original, %Options{smartypants: false})<!-- enddoc: Earmark.as_html -->## Plugins<!-- moduledoc: Earmark.Plugin -->Plugins are modules that implement a render function. Right now that is `as_html`.### API#### Plugin RegistrationWhen invoking `Earmark.as_html(some_md, options)` we can register plugins inside the `plugins` map, whereeach plugin is a value pointed to by the prefix triggering it.Prefixes are appended to `"$$"` and lines starting by that string will be rendered by the registered plugin.`%Earmark.Options{plugins: %{"" => CommentPlugin}}` would trigger the `CommentPlugin` for each block oflines prefixed by `$$`, while `%Earmark.Options{plugins: %{"cp" => CommentPlugin}}` would do the same forblocks of lines prefixed by `$$cp`.Please see the documentation of `Plugin.define` for a convenience function that helps creating the necessary`Earmark.Options` structs for the usage of plugins.#### Plugin Invocation`as_html` (or other render functions in the future) is invoked with a list of pairs containing the textand line number of the lines in the block. As an example, if our plugin was registered with the default prefixof `""` and the markdown to be converted was: # Plugin output ahead $$ line one $$ $$ line two`as_html` would be invoked as follows: as_html([{"line one", 2}, {"", 3}, {"line two", 4})#### Plugin OutputEarmark's render function will invoke the plugin's render function as explained above. It can then integrate thereturn value of the function into the generated rendering output if it complies to the following criteria.1. It returns a string1. It returns a list of strings1. It returns a pair of lists containing a list of strings and a list of error/warning tuples.Where the tuples are of the form `{:error | :warning, line_number, descriptive_text}`#### A complete example iex> defmodule MyPlug do ...> def as_html(lines) do ...> # to demonstrate the three possible return values ...> case render(lines) do ...> {[line], []} -> line ...> {lines, []} -> lines ...> tuple -> tuple ...> end ...> end ...> ...> defp render(lines) do ...> Enum.map(lines, &render_line/1) |> Enum.partition(&ok?/1) ...> end ...> ...> defp render_line({"", _}), do: "<hr/>" ...> defp render_line({"line one", _}), do: "<p>first line</p>\n" ...> defp render_line({line, lnb}), do: {:error, lnb, line} ...> ...> defp ok?({_, _, _}), do: false ...> defp ok?(_), do: true ...> end ...> ...> lines = [ ...> "# Plugin Ahead", ...> "$$ line one", ...> "$$", ...> "$$ line two", ...> ] ...> Earmark.as_html(lines, Earmark.Plugin.define(MyPlug)) {:error, "<h1>Plugin Ahead</h1>\n<p>first line</p>\n<hr/>", [{ :error, 4, "line two"}]}#### Plugins, reusing EarmarkAs long as you avoid endless recursion there is absolutely no problem to call `Earmark.as_html` in your plugin, consider the followingexample in which the plugin will parse markdown and render html verbatim (which uis stupid, that is what Earmark already does for you,but just to demonstrate the possibilities): iex> defmodule Again do ...> def as_html(lines) do ...> text_lines = Enum.map(lines, fn {str, _} -> str end) ...> {_, html, errors} = Earmark.as_html(text_lines) ...> { Enum.join([html | text_lines]), errors } ...> end ...> end ...> lines = [ ...> "$$a * one", ...> "$$a * two", ...> ] ...> Earmark.as_html(lines, Earmark.Plugin.define({Again, "a"})) {:ok, "<ul>\n<li>one\n</li>\n<li>two\n</li>\n</ul>\n* one* two", []}<!-- endmoduledoc: Earmark.Plugin -->## AuthorCopyright © 2014 Dave Thomas, The Pragmatic Programmers@/+pragdave, dave@pragprog.com# LICENSESame as Elixir, which is Apache License v2.0. Please refer to [LICENSE](LICENSE) for details.