Charon
Docs menu

Render in your app

import { renderToHtml } from '@memomori/charon'

const html = await renderToHtml(source)

renderToHtml returns an HTML string. It runs in Node, a worker or the browser, and needs no DOM. It drops frontmatter and %%comments%%.

Show it #

Wrap the HTML in an element with the charon class, and import the styles:

<article class="charon">…</article>
@import '@memomori/charon/styles.css';

The parser keeps raw HTML. Sanitize the output before you show text from users:

import DOMPurify from 'dompurify'

article.innerHTML = DOMPurify.sanitize(await renderToHtml(source))

See Styles for dark mode and colors.

Fill the placeholders #

The parser knows nothing about your files, so it leaves these for you:

Output Meaning
a.wiki-link[data-link-id] a [[link]]; the raw target sits in data-link-id
.embed-container[data-file-id] an ![[embed]]
img[data-blob-path] an image with a relative path, with src removed
.block-anchor[data-block-id] a ^block-id
.mermaid[data-content] a mermaid fence, base64 UTF-8
.geojson-map[data-content] a geojson fence, base64 UTF-8

Add wiki-link-broken or wiki-link-ambiguous to a link your app fails to resolve. Links, embeds and tags shows each syntax.

To fill them all at build time instead, with no app, use renderStaticPage.

Scroll sync #

const html = await renderToHtml(source, { sourceLines: true })

sourceLines stamps data-source-line-start and data-source-line-end on each block. Lines count from 0 and include the frontmatter, so a preview can follow an editor.

Headings #

import { extractToc, slugify } from '@memomori/charon'

extractToc('## Fares\n\n## Hours')
slugify('Set up')

extractToc lists a note's headings with their level, text, line and the id the renderer gives them. slugify turns text into an id, as headings get.

Plugins and tokens #

renderToHtml shares one markdown-it instance. To add a plugin or read tokens, make your own:

import { createMarkdown } from '@memomori/charon'

const md = await createMarkdown()
md.use(myPlugin)
const tokens = md.parse(source, {})

createMarkdown returns a new instance each time, so your changes stay yours. It parses the text as is; run it through preprocessSource first to get charon's wikilinks, embeds, block ids and comments, as renderToHtml does.

Errors #

Charon throws CharonError for a mistake in your input or setup, such as a missing optional package or an embed that climbs out of its folder with ...

Last updated