Skip to content

feat(site): add Copy page and Open in buttons; inline partials in Markdown sources - #1982

Merged
jth-nw merged 1 commit into
devfrom
james/md-copy-and-open-in
Dec 1, 2026
Merged

jth-nw merged 1 commit into
devfrom
james/md-copy-and-open-in

Conversation

@jth-nw

@jth-nw jth-nw commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1934. Evaluated the FlyNumber docusaurus-markdown-source-plugin and kept our /md-source/ implementation, adding the parts of that plugin worth having.

  • Copy page button copies the page's Markdown.
  • Open in… menu opens ChatGPT, Claude, Gemini or Perplexity with a prompt pointing at the page. ChatGPT and Claude get the Markdown URL (they fetch it live); Gemini and Perplexity get the page URL (they answer from their search index).
  • Partials are inlined. import X from '…/_partials/x.mdx' with a bare <X /> is replaced by the partial's text, nested partials included, with a depth limit and a self-import guard. Code fences are left alone. Today that is 33 imports across Identity Manager pages.
  • Doc variables are replaced in the published Markdown, as the build does for the rendered page (the Access Analyzer 26.1 {{Token}} pages).
  • Frontmatter is kept, so description, keywords and title stay at the top of each file. Partial frontmatter is dropped.
  • The dev server renders on the fly through its middleware, so the Markdown tab matches the build. Path traversal and non-served files (CLAUDE.md, _partials, drafts) return 404.
  • Each page's view is tied to its permalink, so following a link starts on the Page tab (no extra fetch).

Addresses both non-blocking notes from the review on #1934.

Notes

  • setupMiddlewares replaces Docusaurus's own dev-server middleware list, so the plugin re-adds its source-map middleware through an internal path (@docusaurus/core/lib/commands/utils/legacy/evalSourceMapMiddleware). If a Docusaurus upgrade moves it, the load falls back to skipping it; only the error overlay's source lookup is affected.
  • Training pages that pull content from src/training/* components (94 files) are not resolved. Their text lives in JS, not Markdown.
  • "Open in…" sends the site's public URL to the assistant, so it only works on a reachable host (not localhost or a private preview).

Test plan

  • node --test for the renderer (partials, nesting, fences, loops, missing files, variables), the provider links, the helper, robots, llms.txt, blob metadata and SEO (137 pass)
  • Dev server: partial inlined, no import lines, traversal and CLAUDE.md return 404
  • DOCS_PRODUCT=identitymanager build: partial inlined and description kept in build/md-source/…, buttons render, no empty directories
  • Check the buttons and the Open in menu in a browser (layout, keyboard, dark mode)
  • On the dev site, confirm Open in ChatGPT/Claude can read a /md-source/ file

🤖 Generated with Claude Code

…kdown sources

Publish rendered Markdown at /md-source/: inline _partials imports, replace doc
variables, and keep frontmatter so the description stays. Add Copy page and an
Open in menu (ChatGPT, Claude, Gemini, Perplexity) next to the Page/Markdown tabs.
Keep each view tied to its page so following a link starts on the Page tab.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code Review

I found no blocking issues. The build pipeline, routing, and dev-server changes look correct. I checked these against @docusaurus/core 3.10.2:

  • setupMiddlewares from configureWebpack replaces Docusaurus's default through webpack-merge, so re-adding evalSourceMapMiddleware is needed. The deep require path exists in 3.10.2. If a later version moves it, the try/catch falls back cleanly.
  • postBuild now walks the same set of files as before (isServedSource still skips _partials, copied KB, and generated API pages) and writes the rendered output. It isn't a KB copy script change, and the KB still comes from docs/kb.
  • Path traversal in the dev middleware is blocked by the relative(...).startsWith('..') check on POSIX.
  • The new tests are wired into test:scripts.

Some smaller issues worth considering:

  1. Copy page can fail in Safari on the first click (src/theme/DocItem/Content/index.js, CopyButton). navigator.clipboard.writeText runs only after await fetchMarkdown(url). When the Markdown isn't cached yet, the network wait uses up Safari's user-gesture window and it rejects the write, so the button shows "Copy failed". A second click works, because the text is cached by then. Fix: in Safari, pass a promise to navigator.clipboard.write([new ClipboardItem({'text/plain': fetchMarkdown(url).then(t => new Blob([t], {type: 'text/plain'}))})]) and fall back to writeText elsewhere. Another option is to prefetch the Markdown when the bar mounts or on hover.

  2. Module-level cache can serve stale Markdown in dev. The Map lives for the whole tab session. With npm run start, after a writer edits a page, the Markdown tab and Copy page keep the old text until a full reload. That contradicts the plugin's "Nothing is stale" comment. You could skip the cache when process.env.NODE_ENV === 'development', or accept the behavior and update the comment.

  3. Indented partials lose their indentation (src/utils/renderMarkdownSource.js, pass 2). The regex accepts leading whitespace before <X />, for example inside a list item, but the inlined partial is inserted with no indentation. A multi-line partial then breaks the list nesting in the published /md-source/ file. You could prefix each inlined line with the matched leading whitespace.

  4. Nit: on Windows, path.relative returns an absolute path when the target is on another drive. In that case the rel.startsWith('..') guard in the dev middleware doesn't fire. It's dev-only and low risk, but || isAbsolute(rel) would close it. The variables test also builds paths with new URL(...).pathname, which breaks on Windows and on paths with spaces. fileURLToPath would fix both.

  5. Nit: FENCE toggles on any of ``` or ~~~ without matching the opening marker or its length. So a ~~~ line inside a ``` block, or a 4-backtick fence that contains a 3-backtick fence, flips the state. Partial imports in those cases could be dropped or kept wrongly. This is rare in these docs.

The workflows, secrets, products.js, and sidebars are unchanged.

@jth-nw
jth-nw merged commit a875c36 into dev Dec 1, 2026
13 checks passed
@jth-nw
jth-nw deleted the james/md-copy-and-open-in branch December 1, 2026 15:52
@FlyNumber

Copy link
Copy Markdown

Glad to see our plug-in was of use to you. We're open to any suggestions and feedback.

This branch was previously deployed

1 inactive deployment
development — 2ef043f6 Deployed Dec 1, 2026 by jth-nw via build-and-deploy #5526
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