Sayfa.Markdown (Sayfa v0.6.0)

Copy Markdown View Source

Wrapper around MDEx for rendering Markdown to HTML.

Provides a consistent interface with {:ok, html} / {:error, reason} tuples and a bang variant that raises on error.

Supports optional syntax highlighting theme selection via the theme parameter.

Code blocks are highlighted for both light and dark color schemes at once using Lumis' :html_multi_themes formatter, so the colors automatically follow the reader's color-scheme via the CSS light-dark() function.

Examples

iex> {:ok, html} = Sayfa.Markdown.render("# Hello")
iex> html =~ "Hello"
true

iex> Sayfa.Markdown.render!("**bold**")
"<p><strong>bold</strong></p>"

Summary

Types

Syntax highlighting theme selection.

Types

theme()

@type theme() :: String.t() | keyword(String.t()) | %{optional(atom()) => String.t()}

Syntax highlighting theme selection.

Either a single theme name applied to both color schemes, or a keyword list (or map) with :light and :dark theme names.

Functions

render(markdown, theme \\ [light: "catppuccin_latte", dark: "catppuccin_mocha"])

@spec render(String.t(), theme()) :: {:ok, String.t()} | {:error, term()}

Renders a Markdown string to HTML.

Headings include anchor IDs for linking (e.g., <h1 id="hello">...</h1>). The optional theme selects the syntax highlighting themes. It accepts either a single theme name (applied to both schemes) or a [light: ..., dark: ...] pair (default: [light: "catppuccin_latte", dark: "catppuccin_mocha"]).

Examples

iex> {:ok, html} = Sayfa.Markdown.render("# Hello")
iex> html =~ ~s(id="hello")
true

iex> Sayfa.Markdown.render("plain text")
{:ok, "<p>plain text</p>"}

render!(markdown, theme \\ [light: "catppuccin_latte", dark: "catppuccin_mocha"])

@spec render!(String.t(), theme()) :: String.t()

Renders a Markdown string to HTML, raising on error.

Examples

iex> html = Sayfa.Markdown.render!("# Hello")
iex> html =~ ~s(id="hello")
true