Skip to content

feat(plugins): add @docubook/plugin-og-image for per-page social cards - #493

Merged
gitfromwildan merged 4 commits into
mainfrom
feat/plugin-og-image
Nov 6, 2026
Merged

gitfromwildan merged 4 commits into
mainfrom
feat/plugin-og-image

Conversation

@gitfromwildan

@gitfromwildan gitfromwildan commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Summary

New official plugin @docubook/plugin-og-image: every page gets its own 1200×630 social preview card, rendered at build time from its frontmatter. Authors write nothing extra. A page with title and description gets its card; image: in frontmatter is only needed to override it.

card (default) minimal
card layout minimal layout

Colours follow themes.colors, the logo comes from navbar.logo.src, and the section is the page's top-level sidebar group.

How it works

  • transformFrontmatter sets image: ./assets/og/<slug>.<hash>.png on pages without an image. Flame's own SEO code resolves it against meta.baseURL and the docs prefix, so there is exactly one og:image per page. The test suite checks this against flame's buildSeoMeta for the default prefix, a root deployment, a GitHub Pages project site and a custom prefix.
  • injectHead adds og:image:width, og:image:height, og:image:type and og:image:alt. On the landing page it also adds og:image, unless meta.ogImage sets one.
  • onEnd reads every page's frontmatter itself. Since perf(flame): keep the page cache when docu.json lists plugins #491, per-page hooks skip cached pages, but onEnd still sees all of them. It renders only cards that are missing or out of date, deletes cards of removed pages, and records the render hash of each file in .docu/og-image.json. A build where nothing changed writes no images.
  • The file name hash covers only inputs that also invalidate flame's page cache: plugin version, docu.json, page text. Logo, font and theme bytes only trigger a re-render under the same name. Cached HTML therefore never points at a card that was not written.
  • handleRequest renders a page's card on demand in flame dev, at the same URL.
  • image: in frontmatter always wins, og: false opts a page out, and og.title / og.description change only the card text.
  • Every hook plans the card from the frontmatter written in the source file, not from what other plugins hand on. A plugin that rewrites a title in onLoad or transformFrontmatter changes the page but not the card, and the file name the page links to always matches the file onEnd writes. injectHead reuses the card transformFrontmatter linked.

Renderer: Takumi

Cards are rendered with Takumi (@takumi-rs/core 2.19), a native renderer with prebuilt binaries and no browser. On a 4-vCPU Linux sandbox:

Renderer Per card Install
Takumi 2–6 ms 8.5 MB
satori + resvg-js ~45 ms ~31 MB
sharp (SVG text, no layout) ~15 ms ~20 MB
@napi-rs/canvas ~150 ms ~37 MB
  • Takumi's render() never fetches. The logo and fonts are read from disk and passed as bytes, and prepare(), which does fetch, is never called.
  • Page text only lands in Takumi text nodes, never in HTML strings, so frontmatter is drawn as text.
  • The built-in Geist font covers Latin-1 only. Takumi has no glyph-coverage API, so the build draws each non-ASCII character once, compares it with the missing-glyph box, and warns with the page names. A fonts option adds fonts for other scripts.
  • The native module loads on Bun, Node and Deno; Deno produced byte-identical PNGs to Node, so there is no WASM fallback.

Options

layout (card | minimal), logo, siteName, pages (docs, index, home), fonts. Unknown keys, remote logos and paths outside the project stop setup() with a [plugin:og-image] error. A missing or oversized logo is a warning, and cards render without it.

Flame changes (patch changeset)

  • The project template's .gitignore, and the docs site's, ignore .docu/og-image.json.
  • Docs: new Social Images page (getting-started/og-image), the og frontmatter field, a link from meta.ogImage, and a fix to the plugin lifecycle diagram (transformFrontmatter runs after compileMdx, before the SEO tags).

Release plan

The plugin is 0.1.0 and public, with no plugin changeset. npm trusted publishing cannot create a new package, so before merging:

  1. Publish it once by hand from this branch with pnpm --filter @docubook/plugin-og-image publish --access public. Use pnpm so workspace:^ becomes ^2.6.0 (flame) and ^2.2.1 (themes-colors). Add --no-git-checks if pnpm refuses to publish from a non-main branch.
  2. Add the trusted publisher for release.yml on npmjs.com.
  3. Merge.

scripts/check-release-plan.mjs now expects the plugin as a patch on a core major, like the Algolia plugin. The docs site does not use the plugin yet; it takes plugins from npm, so that is a follow-up after the publish.

Verification

  • Plugin: 137 tests, including golden images for both layouts (Linux x64 only, pixel tolerance), base-path parity with flame's own functions, and publish/prune/manifest cases. tsc, oxlint and Prettier pass.
  • turbo lint typecheck test for flame and both plugins: 14 of 14 tasks pass. check-release-plan.mjs: 6 of 6 scenarios. The pre-push hook passes.
  • flame build of the project template with the built plugin, on Bun, Node and Deno 2.9.6: 5 cards on a cold build, 0 rendered and 5 unchanged on a warm one. Every og:image in the HTML points at a file in the output; a page with its own image keeps it, and og: false falls back to meta.ogImage.
  • A copy of this docs site (28 pages, with the Algolia plugin): 29 cards in 315 ms cold, 9 ms warm.
  • flame dev: a card URL returns image/png, a stale hash returns the page's current card, an unknown slug returns 404, and other assets/ requests still reach flame.
  • A template build with a second plugin that rewrites titles in onLoad and transformFrontmatter: every og:image in the HTML points at a file in the output.
  • CodeQL flagged three /\/+$/ patterns as polynomial on long runs of /; a linear trimSlashes() replaces them.
  • pnpm pack: the tarball holds dist, README.md and LICENSE, with peer @docubook/flame ^2.6.0.

A 1200×630 OpenGraph card for every page, rendered at build time from
its frontmatter with Takumi (@takumi-rs/core). Authors write nothing
extra: transformFrontmatter points frontmatter.image at the page's card,
and flame's SEO code turns it into an absolute og:image.

- injectHead adds og:image:width/height/type/alt, and og:image on the
  landing page when meta.ogImage does not set one
- onEnd reads every page's frontmatter, so pages served from flame's
  page cache still get their card; it renders only cards whose inputs
  changed, deletes cards of removed pages, and records the result in
  .docu/og-image.json
- the file name hash covers only inputs flame's page cache also sees,
  so cached HTML never points at a missing card
- layouts are Takumi node trees; page text only lands in text nodes
- handleRequest renders cards on demand in flame dev
- the build warns about characters no loaded font can draw

Version 0.1.0, published by hand before merge: npm trusted publishing
cannot create a new package. check-release-plan expects the plugin as
a patch on a core major, like the Algolia plugin.
- new Social Images page (getting-started/og-image) in the sidebar after
  Plugins: install, layouts, per-page image and og overrides, options,
  fonts for other scripts, caching, and flame dev
- frontmatter.mdx documents the og field and that image is optional with
  the plugin; configuration.mdx links it from meta.ogImage
- plugins.mdx lifecycle: transformFrontmatter runs after compileMdx and
  before the SEO tags, not before compileMdx
- the docs site and the project template ignore .docu/og-image.json
  (flame patch changeset for the template)
@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Preview Nov 6, 2026 1:06pm UTC

Comment thread plugins/og-image/src/site.ts Fixed
Comment thread plugins/og-image/src/site.ts Fixed
Comment thread plugins/og-image/src/site.ts Fixed
@gitfromwildan gitfromwildan self-assigned this Nov 6, 2026

@pullfrog pullfrog 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.

ℹ️ One plugin-interaction bug can break og:image in production. Everything else checks out.

Reviewed changes

Initial review (mode: standard) of the new @docubook/plugin-og-image package and the flame docs and .gitignore changes that come with it. Overall: well-tested and carefully matched to flame. Base-path rules, docsIndexWritable, the onEnd page list, pluginsStamp() cache invalidation and the root-index exclusion all match flame's code. Top priorities: (1) the card name must come from one frontmatter source, (2) the CodeQL check is red.

  • Plugin hooks: transformFrontmatter points image at ./assets/og/<slug>.<hash>.png. injectHead adds size, type and alt tags, plus an absolute og:image on the landing page. onEnd renders missing or stale cards. handleRequest serves cards in flame dev.
  • Incremental publishing: publish.ts keeps a manifest in .docu/og-image.json. It re-renders only cards whose render hash changed or whose file is missing, and deletes only files listed in the previous manifest. Deletion is guarded by isInside(cardDir, …).
  • Renderer: Takumi native module, loaded lazily. The logo and fonts are passed as bytes, never fetched. Missing glyphs are detected by comparing each character with the notdef box.
  • Options and site mirroring: strict option validation, and logo and font paths must stay inside the project. site.ts mirrors flame's normalizeBasePath / canonicalBasePath / resolveDeployPath, and the tests pin it to flame's own functions and buildSeoMeta.
  • Flame docs and release plan: new Social Images page, the og frontmatter field, a lifecycle diagram fix, the template's .gitignore, and a check-release-plan.mjs scenario for the plugin's flame peer.

ℹ️ The CodeQL check fails on regexes copied from flame

The CodeQL check is red with 3 "polynomial regular expression" alerts at plugins/og-image/src/site.ts lines 62, 112 and 136. These are the same /^\/+|\/+$/g and /\/+$/ patterns flame already uses in base-path.ts and seo.ts. The input is docu.json, which the site author writes, so there is no real ReDoS exposure. The alerts still need to be dismissed, or the check stays red. Rewriting the patterns would also clear it, but they should stay identical to flame's.

Technical details
# CodeQL js/polynomial-redos alerts on mirrored base-path regexes

## Affected sites
- plugins/og-image/src/site.ts:62 — `path.replace(/^\/+|\/+$/g, "")` (mirror of flame base-path.ts:32)
- plugins/og-image/src/site.ts:112 — `.replace(/\/+$/, "")` on `meta.baseURL` (mirror of flame seo.ts:39)
- plugins/og-image/src/site.ts:136 — `r.href?.replace(/^\/+|\/+$/g, "")` on `routes[].href`

## Required outcome
- The CodeQL check is green before merge, or the alerts are dismissed with a reason (input comes from the project's own docu.json).

## Open questions for the human
- Dismiss the alerts ("won't fix: trusted config input"), or trim slashes without a regex here and in flame together so the two copies stay identical?

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using claude-opus-5-5 | 𝕏

Comment thread plugins/og-image/src/index.ts Resolved
CodeQL flagged the `/\/+$/` patterns in site.ts (base path, baseURL and
route hrefs from docu.json) as polynomial on long runs of `/`. A linear
trimSlashes() replaces them; the base-path parity tests against flame
still pass, and a new test runs 100,000 slashes through it.

@pullfrog pullfrog 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.

ℹ️ No new issues in this commit. The CodeQL check is green again. The earlier og:image file-name thread on index.ts:251 is still open.

Reviewed changes

Incremental review (mode: standard) of 065bfca, the one commit since the last review at 6dd2d2f. It is a fix for the CodeQL alerts.

  • Linear slash trimming: Added trimSlashes(value, endOnly) to site.ts. It replaces the /^\/+|\/+$/g and /\/+$/ regexes in normalizeBasePath, domainOf, resolveSite and sectionFor. The output is the same as before, including all-slash input ("///" → ""). The site.test.ts cases that compare against flame's own normalizeBasePath / buildSeoMeta still cover this. sectionFor now also skips a non-string href, where it used to throw.
  • Tests: Added cases for both trim modes and a 100k-slash input. The check runs in microseconds, so the 100 ms limit leaves plenty of room.

The open MEDIUM finding was not touched by this commit, so its thread stays open. transformFrontmatter / injectHead plan the card from the frontmatter after the plugin chain has run, while onEnd plans it from the raw source. Overall: ready once that thread is resolved.

Pullfrog  | Fix it ➔ | View workflow run | Using claude-opus-5-5 | 𝕏

transformFrontmatter planned the card from the frontmatter it received,
after onLoad and earlier plugins' transformFrontmatter, while onEnd and
flame dev planned from the source file. A plugin that changed a title
gave the page an og:image file name that onEnd never wrote, so it 404'd
in production; flame dev hid it by serving a stale hash.

- transformFrontmatter reads the source file synchronously (PluginBuilder
  types the hook as synchronous) and plans from it, as onEnd does; an
  image set by the author or an earlier plugin still wins
- injectHead reuses the card transformFrontmatter linked for the slug, so
  a later plugin changing the title keeps the size and alt tags, and one
  replacing the image drops them
- README and the Social Images page say the card uses the written title

Four new plugin tests fail on the previous code. A flame build of the
template with a plugin that rewrites titles in onLoad and
transformFrontmatter writes every file its pages link to.

@pullfrog pullfrog 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.

✅ No new issues found. The open og:image file-name finding is fixed, and all checks are green.

Reviewed changes

Incremental review (mode: standard) of ef3dce1, the one commit since the last review at 065bfca. Overall: READY. No findings are left open.

  • One frontmatter source for every hook: transformFrontmatter now plans the card from readFrontmatterSync(site.docsDir, ctx.slug), which is the same source-file read that onEnd and handleRequest use. The file name in frontmatter.image therefore always matches the file onEnd writes. The read is synchronous because PluginBuilder.transformFrontmatter is typed => Record<string, unknown> | void (plugin.ts:206). An image or og: false on the incoming frontmatter still makes the plugin skip the page, and so does the same field in the source.
  • injectHead reuses the linked card: a linked map keyed by slug replaces re-planning from ctx.frontmatter. I checked the hook order in flame. build.ts / build.impl.ts run runTransformFrontmatterChain before collectHead for each page. In dev, the builder lives for the whole process, and docsCache is module-scoped (server-routes.ts:92), so a cached page still finds its map entry. A later plugin that changes the title keeps the tags; one that replaces image drops them.
  • Shared parser: Moved gray-matter parsing into parseFrontmatter, which readFrontmatter and readFrontmatterSync both use.
  • Tests and docs: Added four plugin.test.ts cases, including the one the earlier review described (Setup · Acme from the chain vs. Setup in the source). The README and the Social Images page now explain that the card shows the title as written in the file, and that og.title changes it.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

@gitfromwildan
gitfromwildan merged commit 22774fb into main Nov 6, 2026
15 checks passed
@gitfromwildan
gitfromwildan deleted the feat/plugin-og-image branch November 6, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants