Packages

CommonMark/GFM Markdown (comrak) for Markup

Current section

Files

Jump to
Raw

README.md

# Markup.Markdown

<div align=center>

[![Hex.pm](https://img.shields.io/hexpm/v/markup_markdown?style=for-the-badge&color=%23714a94)](https://hex.pm/packages/markup_markdown)
[![Hexdocs](https://img.shields.io/badge/hex-docs-714a94?style=for-the-badge&logo=elixir)](https://hexdocs.pm/markup_markdown)

</div>

CommonMark and GitHub Flavored Markdown for [Markup](https://hex.pm/packages/markup),
parsed and rendered by [comrak](https://github.com/kivikakk/comrak) through
[`mdex_native`](https://hex.pm/packages/mdex_native). The document API, plugins, HEEx,
the sigil and streaming come from `markup` (see its docs).

| markup_markdown | mdex_native | comrak |
| --- | --- | --- |
| 0.1.x | 0.3.6 | 1.3.0 |

comrak comes in through `mdex_native` (`{:mdex_native, "~> 0.3.6"}` in `mix.exs`), so
the comrak version is whatever that `mdex_native` release bundles.

## Installation

```elixir
def deps do
  [
    {:markup, "~> 0.1.0"},
    {:markup_markdown, "~> 0.1.0"}
  ]
end
```

The language id is `:markdown`. It renders to `:html`, `:xml` (CommonMark XML) and
`:markdown` (CommonMark).

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

doc = Markup.parse_document!("# Hello *world*", language: :markdown, extension: [strikethrough: true])
Markup.convert!(doc, :markdown)
Markup.convert!(doc, :xml)
```

## Options

`:extension`, `:parse` and `:render` are comrak's options, with the same names and
defaults as [MDEx](https://hexdocs.pm/mdex). `:syntax_highlight` and `:budget` go
straight to `mdex_native`. `sanitize: []` uses MDEx's allowlists
(`Markup.Markdown.default_sanitize_options/0`). See `Markup.Markdown` for the full
list.

## Nodes

There's one `Markup.Markdown.*` struct for each `MDExNative.Comrak.*` struct, with the
same fields, so MDEx plugins port over mostly unchanged. Note the comrak names, like
`Emph` and `LineBreak`; kind selectors such as `:emphasis` and `:hard_break` find them
anyway.

## Syntax highlighting

The highlighter is compiled into `mdex_native`, so you choose it in config:

```elixir
# config/config.exs
config :mdex_native, syntax_highlighter: :lumis   # or :syntect
```

Then turn it on per render:

```elixir
Markup.to_html!(markdown, language: :markdown, syntax_highlight: [engine: :lumis])
Markup.to_html!(markdown, language: :markdown, syntax_highlight: [engine: :syntect, opts: [theme: "InspiredGitHub"]])
```

Lumis also needs a parser package for each language you highlight, such as
`{:lumis_wasm_elixir, "~> 0.26"}`. Without the config, highlighting returns
`{:error, %MDExNative.SyntaxHighlightError{}}`. Code block decorators (`theme`,
`highlight_lines` and the like) aren't supported here; that's a djot and carve thing.

## HEEx

HEEx rendering turns on comrak's `phoenix_heex` extension and `render: [unsafe: true]`
for you.

## Credits

Option schemas and the comrak node converter are ported from
[MDEx](https://github.com/leandrocp/mdex) by Leandro Pereira (MIT).

## License

MIT