Syntax highlighting

Copy Markdown View Source

Syntax highlighting can be enabled with the :syntax_highlight option, and it's disabled by default.

MDExNative supports two engines:

It's disable by default, fenced code blocks still render as code blocks keeping the code content unchanged, and the language name is added on the <pre> class.

Lumis

Configure MDExNative before compiling dependencies:

config :mdex_native, syntax_highlighter: :lumis

Then pass syntax_highlight when rendering:

markdown = """
```rust
fn main() {
    println!("Hello from Lumis");
}
```
"""

html = MDExNative.Comrak.markdown_to_html(markdown,
  syntax_highlight: [
    engine: :lumis,
    opts: [
      formatter: {:html_inline, theme: "catppuccin_macchiato"}
    ]
  ]
)

Lumis formatters and options are documented in Lumis.

Syntect

Configure MDExNative before compiling dependencies:

config :mdex_native, syntax_highlighter: :syntect

Then pass a Syntect theme:

markdown = """
```rust
fn main() {
    println!("Hello from Syntect");
}
```
"""

html = MDExNative.Comrak.markdown_to_html(markdown,
  syntax_highlight: [
    engine: :syntect,
    opts: [theme: "Catppuccin Macchiato"]
  ]
)

Syntect theme names come from two-face.

Artifact size

Bundle size depends on the selected highlighter:

ConfigCompressed artifact size
syntax_highlighter: :lumis15 MB
syntax_highlighter: :syntect3 MB
syntax_highlighter: nil-

Legacy CPUs

Modern CPU features are enabled by default. If your environment has an older CPU, use legacy artifacts:

config :mdex_native, use_legacy_artifacts: true