Powered by AppSignal & Oban Pro

~MD Sigil

examples/sigil_md.livemd

~MD Sigil

Mix.install([
  {:mdex, ">= 0.11.2"},
  {:kino, "~> 0.16"}
])

Intro

The ~MD sigil provides a convenient way to write Markdown directly in your Elixir code. It supports multiple output formats and can be customized with options passed to use MDEx.

More info at MDEx.Sigil

Examples

With no options, the sigil uses sensible defaults with most extensions enabled:

defmodule DefaultExample do
  use MDEx

  def render do
    ~MD"""
    This is a **bold** statement with ~strikethrough~ and ^superscript^.

    - [x] Task completed
    - [ ] Task pending

    ```elixir
    def greet(name), do: "Hello, #{name}!"
    ```
    """HTML
  end
end

DefaultExample.render()
|> Kino.HTML.new()

You can pass options to use MDEx to customize the sigil behavior. Options are merged with the defaults:

defmodule CustomOptsExample do
  use MDEx,
    extension: [strikethrough: false],
    syntax_highlight: [formatter: {:html_inline, theme: "gruvbox_light"}]

  def render do
    ~MD"""
    Notice ~this~ is not rendered as strikethrough because we disabled it.

    ```elixir
    defmodule Fibonacci do
      def fib(0), do: 0
      def fib(1), do: 1
      def fib(n), do: fib(n - 1) + fib(n - 2)
    end
    ```
    """HTML
  end
end

CustomOptsExample.render()
|> Kino.HTML.new()