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.valueis the code,block.langthe language, andblock.metathe words after it, such assort=-pricein```query sort=-price.highlight(block)renders the block as charon's highlighted code, for a fallback inside your placeholder.- Return
undefinedto 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.