Content & Markdown
Posts are written in Markdown, rendered and sanitized on save
Rendering
Every table that stores content keeps two columns. content holds the Markdown and is canonical; content_html holds the rendered, sanitized result. That is the one you render.
import { renderContent, excerpt, slugify } from '@basicbenframework/core/content'
const html = await renderContent('# Title\n\nSome **bold** text.')
const summary = excerpt(markdown, 200) // plain text, cut at a word boundary
const url = slugify('Hello, World!') // "hello-world"Rendering happens on save, not per request — a blog is read far more often than written. The models do it for you: Post.create and Post.update re-render whenever content changes, so the two columns cannot drift apart.
Never render content as HTML. It is Markdown. content_html is the column that has been through the sanitizer.
Why there is no Markdown dependency
The framework has no runtime dependencies, and this is where that argument usually breaks down: surely hand-writing a parser is an XSS risk. It is the reverse. CommonMark requires raw HTML to pass through verbatim — 64 of the spec's 652 cases exist to pin that down — which is exactly why every off-the-shelf parser tells you to sanitize its output. The dependency introduces the hazard rather than removing it.
This parser refuses those 64 cases deliberately. It escapes everything it reads and emits only tags from its own vocabulary, so no input can become a tag. The correctness you give up is measured rather than claimed: it passes 93% of the CommonMark suite excluding the raw-HTML sections, checked on every test run with a floor that fails the build on regression.
Supported syntax
Headings (ATX and setext), emphasis, strong, strikethrough, inline code, fenced and indented code blocks, blockquotes, nested and tight/loose lists, links, reference links, images, autolinks, tables, hard line breaks, backslash escapes and HTML entities. Headings get id anchors automatically.
Sanitizing other HTML
The parser is safe on its own, but imported content is not — a WordPress export, a a hook's output, a field you deliberately opened up to raw HTML.
import { sanitizeHtml } from '@basicbenframework/core/content'
sanitizeHtml(imported) // allowlist, drops the rest
sanitizeHtml(imported, { schemes: ['https'] }) // narrow the URL schemes
sanitizeHtml(imported, { allowed: { p: [], a: ['href'] } })Anything not named is removed. script, style, iframe, svg and math go with their contents; other unknown tags are unwrapped so their text survives. Event handlers and style attributes never pass. URLs are checked after entity decoding, so javascript: is caught.
Extending the pipeline
A listener post-processes rendered HTML through the content.render filter — syntax highlighting, lazy-loaded images, a table of contents.
hooks.on('content.render', (html, { table, id }) =>
html.replace(/<code>/g, '<code class="hljs">')
)The filter runs before sanitization, never after — so no listener can put markup on the page the allowlist has not seen. The practical consequence, and the first thing you will hit writing one: your markup is subject to the allowlist too. <span class> and <code class> pass; <p class> does not, and the attribute will simply be gone. Widen the list rather than moving the filter.
Rerendering
Stored HTML goes stale when the parser changes, the allowlist changes, or a listener that hooks content.render is installed or removed.
basicben content:rerender basicben content:rerender posts # just one table basicben content:rerender --dry-run
It only ever writes content_html, so it is safe to run repeatedly.