Skip to main content

Elixir

When you want explicit error handling

Use highlight/2 when invalid user input, formatter options, or language detection should stay in the normal control flow.

case Lumis.highlight(source, formatter: {:html_inline, language: "elixir"}) do
{:ok, html} -> html
{:error, reason} -> handle_error(reason)
end

highlight/2 returns {:ok, output} or {:error, reason}.

When you want the highlighted output directly

Use highlight!/2 when failures should raise immediately, such as in scripts, trusted internal code, or examples.

html = Lumis.highlight!(source, formatter: {:html_inline, language: "elixir"})

highlight!/2 returns the highlighted output or raises.

Language selection

Elixir accepts several forms:

Lumis.highlight!(code, formatter: {:html_inline, language: "elixir"})
Lumis.highlight!(code, formatter: {:html_inline, language: ".ex"})
Lumis.highlight!(code, formatter: {:html_inline, language: "lib/my_app.ex"})
Lumis.highlight!(code)

Omitting language from the formatter enables auto-detection.

Discover supported languages and themes

Lumis.available_languages()
Lumis.available_themes()
Lumis.Theme.get("github_light")

See Lumis and Lumis.Theme on HexDocs.

Phoenix and LiveView

Highlighted HTML must be rendered as raw HTML in templates.

code = Lumis.highlight!(source, formatter: {:html_inline, language: "elixir"})
{Phoenix.HTML.raw(@code)}

For linked CSS output, also mount a theme stylesheet through Plug.Static as shown in CSS Theme Files.

Light/dark mode

See Light/Dark Mode for three approaches to theme switching (CSS light-dark(), media queries, and manual toggle).

Custom themes

{:ok, theme} = Lumis.Theme.from_file("/path/to/theme.json")
{:ok, theme} = Lumis.Theme.from_json(theme_json)

Then pass the resulting theme struct into the formatter options.

Validation

# validate options before passing to highlight
Lumis.validate_options!(formatter: {:html_inline, language: "elixir", theme: "dracula"})
# inspect defaults
Lumis.default_options()

Use validate_options!/1 when your app exposes formatter options to users (e.g., config files, admin panels). It raises on invalid values.