Skip to content

docs: add KaTeX Portable Text recipe#1885

Open
giffeler wants to merge 1 commit into
emdash-cms:mainfrom
giffeler:codex/docs-katex-portable-text
Open

docs: add KaTeX Portable Text recipe#1885
giffeler wants to merge 1 commit into
emdash-cms:mainfrom
giffeler:codex/docs-katex-portable-text

Conversation

@giffeler

@giffeler giffeler commented Jul 9, 2026

Copy link
Copy Markdown

What does this PR do?

Adds a small documentation recipe for rendering LaTeX math in EmDash Portable Text content with KaTeX on Astro 7.

The page explains why Astro Markdown plugins do not process CMS Portable Text fields directly, shows the Astro 7 markdown.processor: unified(...) setup for Markdown/MDX files, and demonstrates a modest site-level PortableText wrapper for display math blocks.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added a changeset (if this PR changes a published package) — n/a, docs-only
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/...

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: OpenAI Codex / GPT-5

Screenshots / test output

Docs-only verification run:

pnpm exec prettier --write docs/src/content/docs/guides/katex.mdx
pnpm --filter docs build
pnpm lint:quick

pnpm --filter docs build completed successfully and prerendered /guides/katex/.
pnpm lint:quick completed with zero diagnostics.

@changeset-bot

changeset-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 250ee45

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added area/docs size/M review/needs-review No maintainer or bot review yet labels Jul 9, 2026
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recipe itself is a reasonable, additive docs page: wrapping EmDash's PortableText to render KaTeX display math is a useful site-level technique and the core approach is sound. However, the Astro 7 Markdown-configuration snippet is unverified and inconsistent with what the repo's lockfile shows about Astro 7, and two of the TypeScript snippets won't typecheck as written. I also found a minor docs-style issue.

What I checked:

  • Read the new docs/src/content/docs/guides/katex.mdx and the sidebar change in docs/astro.config.mjs.
  • Verified emdash/ui exports PortableText and PortableTextBlock (from astro-portabletext), but the root emdash package exports a different, generic PortableTextBlock.
  • Checked the lockfile: Astro 7.0.0 resolves to @astrojs/markdown-satteri, not @astrojs/markdown-remark, and the standard unified package takes no options object of { remarkPlugins, rehypePlugins }.
  • Confirmed the docs build only validates Markdown/MDX; it does not typecheck or execute the embedded snippets.

Headline conclusion: the Portable Text wrapper parts look correct, but please fix the Astro 7 Markdown-config section and the TypeScript types before this recipe goes live.

Comment thread docs/src/content/docs/guides/katex.mdx Outdated
Comment on lines +36 to +52
Astro 7 uses a Markdown processor API. If you need math in Markdown or MDX files,
configure `unified()` instead of the deprecated `markdown.remarkPlugins` and
`markdown.rehypePlugins` options:

```js title="astro.config.mjs"
import { unified } from "@astrojs/markdown-remark";
import rehypeKatex from "rehype-katex";
import remarkMath from "remark-math";
import { defineConfig } from "astro/config";

export default defineConfig({
markdown: {
processor: unified({
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
}),
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[needs fixing] This section tells readers to configure Astro 7's Markdown engine with import { unified } from "@astrojs/markdown-remark" and processor: unified({ remarkPlugins: [remarkMath], rehypePlugins: [rehypeKatex] }). That is unverified and appears inconsistent with the project's own lockfile: astro@7.0.0 resolves to @astrojs/markdown-satteri, not @astrojs/markdown-remark. The standard unified package also does not accept an options object of { remarkPlugins, rehypePlugins }; that shape belongs to createMarkdownProcessor-style helpers, while markdown.processor expects a processor function/maker. Finally, markdown.remarkPlugins/rehypePlugins are not deprecated in the Astro 6.1.3 that this repo and EmDash target. Because the docs build only validates Markdown syntax, this snippet is not typechecked or executed. Please either confirm the snippet against Astro 7's actual markdown.processor API or replace it with a safe, version-appropriate pointer to the Astro docs.

Comment thread docs/src/content/docs/guides/katex.mdx Outdated
renderer for normal content and only intercepts formulas.

```ts title="src/components/math-portable-text.ts"
import type { PortableTextBlock } from "emdash";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[needs fixing] The root emdash package exports a generic PortableTextBlock with an index signature ([key: string]: unknown), so block.children is typed as unknown. The code on lines 114–117 then calls .filter(...).map(...).join(...) on it, which will not typecheck. Import the typed PortableTextBlock (and PortableTextSpan) from emdash/ui instead, since that re-exports astro-portabletext's types.

Suggested change
import type { PortableTextBlock } from "emdash";
import type { PortableTextBlock, PortableTextSpan } from "emdash/ui";

Comment thread docs/src/content/docs/guides/katex.mdx Outdated
Comment on lines +114 to +117
block.children
?.filter((child) => child._type === "span")
.map((child) => child.text)
.join("") ?? "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[needs fixing] Even with the correct PortableTextBlock import, .filter((child) => child._type === "span") does not narrow the array element type, so .map((child) => child.text) is not guaranteed to be valid for every element in the union. Add a type predicate or cast the children to PortableTextSpan[].

Suggested change
block.children
?.filter((child) => child._type === "span")
.map((child) => child.text)
.join("") ?? "";
const text =
((block.children ?? []) as PortableTextSpan[])
.filter((child) => child._type === "span")
.map((child) => child.text)
.join("") ?? "";

in rich text fields.
</Aside>

## Install KaTeX

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] Per the Documentation Style Guide, every code block should be introduced by a full, standalone sentence; don't lead with only a bare heading. Add a sentence before the install command, e.g. "Install KaTeX in your Astro project."

@giffeler giffeler force-pushed the codex/docs-katex-portable-text branch from 775503d to cfcefff Compare July 9, 2026 04:27
@github-actions github-actions Bot added review/needs-rereview Author pushed changes since the last review cla: signed and removed review/needs-review No maintainer or bot review yet cla: needed labels Jul 9, 2026
@giffeler giffeler force-pushed the codex/docs-katex-portable-text branch from cfcefff to 250ee45 Compare July 9, 2026 09:49
@github-actions github-actions Bot added size/L and removed size/M labels Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs cla: signed review/needs-rereview Author pushed changes since the last review size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant