@molecule/app-markdown-react-markdown

Provider bond · markdown · App (browser) · v2.0.0 · Apache-2.0

Markdown provider for @molecule/app-markdown backed by react-markdown and remark-gfm

npm install @molecule/app-markdown-react-markdown

npm · Source on GitHub · Implements @molecule/app-markdown

How it works

@molecule/app-markdown-react-markdown is a provider bond on the app (browser) side: it implements the markdown core interface (@molecule/app-markdown) with a concrete vendor or library behind it.

Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.

import { provider } from '@molecule/app-markdown-react-markdown'
import { requireProvider, setProvider } from '@molecule/app-markdown'

setProvider(provider) // once, at app startup (bonds.ts)

const { html, toc } = requireProvider().render('| a | b |\n| - | - |\n| 1 | 2 |')
// html === '<table>…</table>'  (a real GFM table)

Works with: @molecule/app-i18n, @molecule/app-markdown

Live demos

Running apps built with Molecule that install this package — open the app or study the template.

Reference

Markdown provider for @molecule/app-markdown, backed by the real react-markdown 10 library plus remark-gfm.

The core contract returns an HTML STRING (render(): { html: string; toc }), but react-markdown renders React elements — so this bond renders that element tree to a static string with renderToStaticMarkup (react-dom/server). You still get real CommonMark parsing and full GitHub Flavored Markdown (tables, strikethrough, task lists, autolinks, footnotes) via remark-gfm.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { provider } from '@molecule/app-markdown-react-markdown'
import { requireProvider, setProvider } from '@molecule/app-markdown'

setProvider(provider) // once, at app startup (bonds.ts)

const { html, toc } = requireProvider().render('| a | b |\n| - | - |\n| 1 | 2 |')
// html === '<table>…</table>'  (a real GFM table)

Type

provider

Installation

npm install @molecule/app-markdown-react-markdown @molecule/app-i18n @molecule/app-markdown react react-dom react-markdown rehype-raw remark-gfm
npm install -D @types/react @types/react-dom

API

Interfaces

ReactMarkdownConfig

Provider-specific configuration for createProvider. These are the defaults baked into a provider instance; per-call MarkdownOptions (from @molecule/app-markdown) override the ones they share (sanitize, gfm, components).

interface ReactMarkdownConfig {
  /**
   * Whether to sanitize link/image URLs by default (the XSS URL gate) and keep
   * raw HTML inert. When `true` (default) a markdown link or image whose URL
   * `isRefusedMarkdownUrl` refuses renders as its text, and raw HTML in the
   * source is escaped to inert text. When `false` every URL is kept except a
   * `javascript:` or `vbscript:` one on a markdown link or image, and raw HTML
   * in the source is rendered as elements (`rehype-raw`) — reserve it for
   * trusted, app-authored content. Defaults to `true`.
   */
  sanitize?: boolean

  /**
   * Whether to enable GitHub Flavored Markdown (the `remark-gfm` plugin —
   * tables, strikethrough, task lists, autolinks, footnotes) by default.
   * Defaults to `true`.
   */
  gfm?: boolean

  /**
   * Extra remark plugins to run, appended after `remark-gfm` and the (optional)
   * hard-break transform. Use for additional markdown-AST extensions.
   */
  remarkPlugins?: MarkdownPluginList

  /**
   * Rehype plugins to run on the HTML AST (after `remark-rehype`), appended
   * after the built-in steps (heading ids and the table of contents, unsafe
   * URL replacement, task-list and footnote markup, `elementAttributes`, and
   * `rehype-raw` when `sanitize` is off). This is where a real syntax
   * highlighter (e.g. `rehype-highlight`) is wired in.
   */
  rehypePlugins?: MarkdownPluginList

  /**
   * Restrict the output to only these HTML tag names. Mutually exclusive with
   * `ReactMarkdownConfig.disallowedElements` — if both are set,
   * `allowedElements` wins.
   */
  allowedElements?: readonly string[]

  /** Drop these HTML tag names from the output (e.g. `['img']`). */
  disallowedElements?: readonly string[]

  /**
   * Custom component overrides keyed by tag name (react-markdown's
   * `Components`). Merged with any per-call `options.components`, which win.
   *
   * The result is a static HTML string, so an override keeps only its rendered
   * markup — no click handler, state, or context. Use the provider-neutral
   * `elementAttributes` option to add attributes instead.
   */
  components?: Components
}

Types

MarkdownPluginList

A list of remark or rehype plugins — react-markdown's PluggableList (a plugin function, a [plugin, options] tuple, or a preset). Re-derived from react-markdown's own Options so no extra dependency on unified is needed.

type MarkdownPluginList = NonNullable<Options['remarkPlugins']>

Functions

createProvider(config)

Creates a react-markdown provider instance.

function createProvider(config?: ReactMarkdownConfig): MarkdownProvider
  • config — Optional provider configuration (defaults for every render).

Returns: A configured MarkdownProvider.

Constants

provider

Default react-markdown provider instance (GFM + URL sanitization on).

const provider: MarkdownProvider

Core Interface

Implements @molecule/app-markdown interface.

Bond Wiring

Setup function to register this provider with the core interface:

import { setProvider } from '@molecule/app-markdown'
import { provider } from '@molecule/app-markdown-react-markdown'

export function setupMarkdownReactMarkdown(): void {
  setProvider(provider)
}

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-i18n ^1.2.2
  • @molecule/app-markdown ^1.1.0
  • react ^18.0.0 || ^19.0.0
  • react-dom ^18.0.0 || ^19.0.0

Runtime Dependencies

  • @molecule/app-i18n

  • @molecule/app-markdown

  • react

  • react-dom

  • react-markdown

  • rehype-raw

  • remark-gfm

  • The DOM matches every other @molecule/app-markdown provider. The conformance cases in the shared kit define it: headings get a unique id from createHeadingSlugger(), and toc lists exactly the rendered headings in order (parsed from the tree, so a # line in a code fence is not a heading and a setext heading is), [] when there are none. A link or image whose URL isSafeMarkdownUrl refuses renders as its link text or alt text — no element. Task lists render as <li><input type="checkbox" disabled [checked]> text</li> with no class names, and GFM footnotes render the labelled data-footnotes section the cases define (section and back-link labels go through t()).

  • URL policy. sanitize (default on) applies the shared isSafeMarkdownUrl allow-list (http, https, mailto, tel, irc, ircs, xmpp, relative URLs and fragments) to every markdown link and image. With sanitize: false every URL is kept except a javascript: or vbscript: one on a markdown link or image, which no provider ever emits.

  • Raw HTML. With sanitize on (the default) raw HTML in the source (<script>, <b>, an onerror attribute) is escaped to inert text. With sanitize: false it renders as elements via rehype-raw — trusted, app-authored markup that gets none of this bond's shaping: a raw heading gets no id and no toc entry, a raw element gets no elementAttributes, and a raw URL is kept as written.

  • Options: gfm, sanitize, breaks (soft breaks → <br>), linkTarget (_blank also adds rel="noopener noreferrer"), elementAttributes (extra attributes on every element of a kind, refused when they name href, src, id or an event handler), and the deprecated components (custom tag overrides). A fenced code block carries a language-* class (highlighter-ready); pass a rehype highlighter (e.g. rehype-highlight) via config.rehypePlugins to colorize.

  • A components override keeps only its static markup — no click handler, state, or context — because the result is an HTML string. Use elementAttributes instead.

  • ReactMarkdownConfig additionally exposes remarkPlugins, rehypePlugins, allowedElements, and disallowedElements for provider-level customization at createProvider time. A config's own rehypePlugins run after this bond's.

  • Wire it with setProvider() from @molecule/app-markdown or bond('markdown', provider) from @molecule/app-bond — both route through the shared registry; requireProvider() throws until one has run.

E2E Tests

Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual screens/flows, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip:

  • Common markdown renders as real HTML wherever the app shows it (chat reply, comment, note, README preview): #..###### become <h1>..<h6>, **bold** a <strong>, *italic* an <em>, -/1. lists become <ul>/<ol> of <li>, [text](url) an <a> anchor, and a fenced code block a <pre><code> — not the raw */backticks shown as literal text.
  • GFM extras render when enabled (gfm, default on): a pipe table | a | b | becomes a real <table> with header + rows, and ![alt](src) an <img>.
  • The output tracks the source: editing the markdown updates the preview live — change a heading's text and its <h#> updates; add a list item and a new <li> appears. No stale or one-shot render.
  • SECURITY (XSS) — user- or model-supplied markdown is sanitized (sanitize, default on, the XSS gate): markdown containing a raw <script>alert(1)</script>, an <img src=x onerror=alert(1)>, or a [click](javascript:alert(1)) link must NOT execute and must NOT produce active markup — the script is stripped, the onerror is gone, and the link's href is neutralized. Verify in the browser: no alert fires, and DevTools shows no <script>, onerror, or javascript: in the rendered DOM. Never render untrusted content with sanitize: false.
  • Malformed markdown degrades gracefully — an unclosed **, a broken [link](, or a half-open code fence renders as readable text and never throws, blanks the view, or breaks the surrounding page.