Skip to content

MDX Messages

Palamedes treats .mdx as a first-class source format. The native extractor and the Vite compiler consume the same Ferromark source-ranged AST, so a message extracted by pmds extract is the same message rendered by the compiled module.

Page outline

Setup

MDX compilation uses Vite 8's Rolldown pipeline, which accepts the generated JSX module type. Install the framework package and use the Palamedes plugin before the framework Vite plugin:

import react from "@vitejs/plugin-react";
import { palamedes } from "@palamedes/vite-plugin";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [palamedes(), react()],
});

For React, Palamedes marks generated .mdx modules as JSX automatically.

For Solid 2, set framework: "solid" on the plugin and opt .mdx into @solidjs/vite-plugin explicitly:

import solid from "@solidjs/vite-plugin";
import { palamedes } from "@palamedes/vite-plugin";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [palamedes({ framework: "solid" }), solid({ extensions: [".mdx"] })],
});

The plugin compiles for React unless told otherwise, so this option is required for Solid. Framework selection controls only the rich-component contract; runtime lookups stay hook-free. Extraction produces identical messages for both frameworks, so the catalog config has no reason to know the application runtime.

Do not add React's JSX module type for Solid. Rolldown would otherwise lower the module with React's automatic runtime before Solid's Babel preset sees it.

Palamedes and @mdx-js/rollup both claim .mdx modules. Do not enable both for the same files; Palamedes runs as a pre-transform and intentionally owns the full MDX compilation path.

Run normal catalog extraction after adding an MDX file:

pmds extract

Directory include patterns discover .mdx alongside JavaScript and TypeScript.

Translation Units

Palamedes derives messages from rendered semantics rather than raw Markdown tokens:

  • each heading, paragraph, list item, blockquote paragraph, and table cell is its own message
  • inline emphasis, links, and JSX become rich placeholders
  • MDX expressions become named value placeholders
  • image alt text is extracted separately
  • configured static JSX attributes and frontmatter scalar fields are extracted separately

For example:

# Hello {account.name}

Read the **guide** or visit [configuration](/docs/configuration).

extracts messages equivalent to:

Hello {name}
Read the <0>guide</0> or visit <1>configuration</1>.

The generated module preserves account.name as the runtime expression behind the name placeholder. A repeated bare identifier such as {count} reuses one placeholder; calls, property reads, and other expressions get one placeholder per occurrence ({name}, {name_1}) and are evaluated in source order, as in JavaScript macros. URLs, image sources, fenced code blocks, and inline code are rendered but are not sent to translators. Fenced code keeps a language-* class derived from its info string.

Configuration

mdx:
  framework: react
  translatable-attributes: [alt, title, aria-label]
  front-matter-fields: [title, description]
  trans-module: "@palamedes/react/compiled"
  runtime-module: "@palamedes/runtime"
  ignore-directive: palamedes-ignore
FieldDefaultPurpose
frameworkreactGenerates React or Solid rich-component bindings.
translatable-attributes[alt]Static JSX attributes extracted as standalone messages.
front-matter-fields[]Scalar frontmatter values extracted as standalone messages.
trans-moduleframework /compiled pathModule exporting parser-free Trans.
runtime-module@palamedes/runtimeAdvanced override for the module exporting getI18n.
ignore-directivepalamedes-ignoreMarker used for an explicit per-unit opt-out.

TypeScript configs use the camelCase equivalents: translatableAttributes, frontMatterFields, transModule, runtimeModule, and ignoreDirective.

translatable-attributes replaces the default list rather than extending it. Include alt explicitly when adding fields such as title or aria-label. href and src remain structural attributes and are never extracted. In Vite, the plugin's framework option selects the component contract for compiled MDX; mdx.framework overrides it per config. Runtime access remains hook-free. The plugin's runtimeModule option applies to macros only — use mdx.runtime-module to point MDX at a different custom runtime explicitly.

Generated MDX message lookups pass only the compiled message ID by default. Set includeSourceMetadata: true on palamedes() to also pass the authored source text as diagnostic metadata; a missing compiled message still throws. MdxOptions accepts the same setting in data config as include-source-metadata, but the Vite plugin overwrites it with its own setting, so configure the plugin option when using Vite.

The Vite plugin's top-level framework: "none" option drops the plugin's own MDX framework selection; it does not disable MDX compilation. The compiler then falls back to its default and still emits the React component contract. A project that wants no MDX component contract at all sets mdx: false. The data-config override mdx.framework selects between "react" and "solid" and accepts no other value; @palamedes/remix does not expose the Vite plugin option.

Translated frontmatter remains explicit. The compiler exports the original scalar object as frontmatter; when configured fields are present it also exports getTranslatedFrontmatter(), which resolves those fields through the active i18n instance. Collection-valued frontmatter is omitted rather than being stringified as a misleading scalar.

Ignoring Content

Put the configured marker in a comment immediately before a semantic unit:

{/* palamedes-ignore */}

This paragraph stays in the source language.

The content still renders, but no catalog message is created for that unit. The directive covers the whole unit, including image alt text and configured JSX attributes. JSX comments are the canonical MDX form; HTML comments are also recognized and are omitted from rendered content.

Diagnostics, Maps, and HMR

Malformed MDX produces structured, source-ranged diagnostics from Ferromark. Footnotes currently produce an UnsupportedFootnote diagnostic instead of silently compiling incorrect links. The Vite adapter reports diagnostic locations as build errors and returns the native v3 source map for valid modules. It watches the Palamedes config; catalog modules remain owned by the normal PO loader and configured runtime.

Generated modules currently use the built-in HTML element mapping. They do not yet implement MDXProvider or a components prop, and extracted messages do not set context. Use imported JSX components directly when a page needs custom rendering.

Verified Example

examples/vite-mdx is a complete three-page React/Vite handbook with linked MDX modules, a shared English/German catalog, imported JSX components, expressions, translated attributes, image alt text, code blocks, and a document-level locale switch. It is part of pnpm verify:examples.

Architecture Boundary

Ferromark owns Markdown/MDX recognition, JavaScript syntax validation, and the source-ranged AST. crates/palamedes owns translation-unit selection, rich/value placeholder construction, message identity, diagnostics conversion, and JSX module lowering. The native CLI and typed N-API binding both call that core workflow. @palamedes/vite-plugin is intentionally a thin host adapter: it selects .mdx modules, forwards configuration, registers watch files, and returns the native code and source map.

This boundary prevents extraction and compilation from developing separate Markdown heuristics.

Parser Contract

The native adapter uses Ferromark 3.7.0 with its jsx feature and both mdx and mdx_compatible parser options. Structured image-alt analysis is enabled for source provenance. The adapter consumes the public arena AST. Its MIT license, Rust requirement, and OXC version are compatible with the workspace. See the upstream v2 migration guide and v3 upgrade notes.

MDX ships with Palamedes v2. Translation units follow the current parsed semantics. MDX diagnostics use the shared SourceRange type with original UTF-8 byte offsets and line/column positions.

  • Image alt text is the complete normalized accessible description. Alt text authored as Code `sample` produces the message Code sample, including words authored as inline code. Its source reference points to the original bracket contents using Ferromark's structured alt metadata, including inside lists, quotes, and reference-style images.
  • Multiple paragraphs inside JSX stay separate translation units inside their authored container. They are never concatenated across a blank line.
  • Callout markers such as [!NOTE] remain structural; the body is translatable. Ferromark's public callout metadata owns marker recognition and body selection, including escaped or entity-encoded markers.
  • GFM strikethrough follows the parser, including single-tilde ~text~. Optional Ferromark writing extensions remain disabled.
  • Code fences retain MDX-looking source verbatim as code. Escaped and decoded text is not entity-decoded a second time.

Parser failures return typed diagnostics such as InvalidExpression, InvalidOpeningTag, InvalidModule, MismatchedClosingTag, and UnexpectedClosingTag. Tag mismatches include the complete offending tag range and a related opening-tag range in the original source. Nesting limits still produce NestingTooDeep; unknown future MDX categories fall back to InvalidMdx. The adapter collects up to 16 independent errors. Recovery is limited to complete, standalone unmatched closing tags at the root block boundary; ambiguous, container-local, and JavaScript failures stop collection. Invalid JavaScript in expressions, attributes, and import/export blocks fails analysis; TypeScript declarations belong in imported TypeScript modules. Failed analysis returns no messages, compiled IDs, JSX, or source map. Recognized footnotes still return UnsupportedFootnote; unresolved footnote-like text remains literal.

Ferromark #563, #564, and #565 track richer image provenance, diagnostic details, and public callout metadata. All three shipped in Ferromark 3.6.0 and are used by the native adapter.

Edit this page on GitHubLast updated: November 10, 2026