Markdown gives writers a compact way to structure technical content, but rendering it safely requires more than replacing
**Keep the pipeline explicit
A reliable pipeline has three steps: parse Markdown into an AST, sanitize the allowed HTML or AST nodes, then render with components that fit the product. Do not concatenate author text into
dangerouslySetInnerHTMLAllow the elements the article format needs: paragraphs, headings, lists, blockquotes, fenced code, and safe links. For links, reject
javascript:relMake format detection deterministic
Store the content format alongside the content or define a migration rule. Guessing from a few characters creates edge cases: a plain text post may contain angle brackets, while valid Markdown may not contain any obvious markers. When converting legacy records, preview the result and retain the original until the migration is verified.
A realistic rendering check
A heading inside a fenced code block and a heading in the surrounding article look similar in the Markdown source. A parser represents them differently. In CommonMark 0.31.2, the contents of a fenced code block are literal text, and the information string after the opening fence can identify the language. A syntax highlighter can consume that information without interpreting the contents as HTML. CommonMark fenced code blocks
Preserve the original Markdown so an editor can revise it later. Start with a fixture that contains a heading, a literal closing script tag in a code block, a link, and an image with alternative text. Inspect the rendered DOM for a single heading and a readable code block. If the renderer accepts raw HTML as an editorial feature, sanitize the generated HTML through a configured allowlist before insertion. Keep URL handling in the same review. Markdown syntax and permission to render arbitrary HTML are separate decisions. DOMPurify configuration and examples
Worked example
Store this as Markdown source. The renderer should produce a heading and an inert shell-code block, rather than executing the command.
markdown## Installation Run the example locally. ```sh bun install --frozen-lockfile ```




Community
Discussion
Loading comments…