Charon
Docs menu

Custom fences

A fence renderer turns the code fence of one language into your own HTML. Give it to createRenderer, then render through the result instead of renderToHtml:

import { createRenderer } from '@memomori/charon'

const renderer = createRenderer({
  fences: {
    chart: (block, { highlight }) =>
      `<div class="chart" data-spec="${btoa(block.value)}">${highlight(block)}</div>`,
    mermaid: null,
  },
})

const html = await renderer.render(source)

The renderer #

A renderer takes the block and a set of tools, and returns HTML:

  • block.value is the code, block.lang the language, and block.meta the words after it, such as sort=-price in ```query sort=-price.
  • highlight(block) renders the block as charon's highlighted code, for a fallback inside your placeholder.
  • Return undefined to hand the block back to the default rendering.
  • Return '' to drop the block.

Built-in fences #

null turns off a built-in fence, so it renders as plain highlighted code:

Fence Renders as
mermaid a .mermaid diagram placeholder
geojson a .geojson-map map placeholder
csv a table
tsv a table

defaultFences holds all four, to wrap or reuse one.

Pass it on #

Each renderer builds its own markdown-it instance, so its fences never reach renderToHtml. renderer.markdown() returns that instance.

Hand the renderer to every step that renders Markdown, so examples and embedded notes use your fences too:

await renderStaticPage(source, { renderer, examples: true })
await fillEmbeds(html, folder, { renderer })

A docs site built with Vite takes fences directly. The charon build CLI has no way to load code, so it uses the built-in fences.

Last updated