Skip to content

Add copy-as-Markdown action to documentation pages#375

Open
Zedoraps wants to merge 4 commits into
mainfrom
feat/copy-page-markdown-llm
Open

Add copy-as-Markdown action to documentation pages#375
Zedoraps wants to merge 4 commits into
mainfrom
feat/copy-page-markdown-llm

Conversation

@Zedoraps

@Zedoraps Zedoraps commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

🔍 Problem

  • Docs pages have no built-in way to grab the Markdown source. Readers feeding pages into an LLM had to scrape HTML or paste from view-source.
  • The plugin's per-page .md route was gated behind LLMS_TXT=true, so any UI exposing those URLs would 404 in normal dev/prod builds.

🛠️ Solution

  • Add a copy-for-LLM button + dropdown ("View page as markdown", "Use in your agent") to every doc page's <h1> row.
  • Split the plugin's llmsTxt and perPageMarkdown gates so the .md twin is always available; the site-wide /llms.txt and /llms-full.txt stay gated.
  • Centralise Markdown twin URL building behind one helper so route param normalisation, the copy URL in PageTitle, and link rewriting in entry-to-markdown agree on */index*.
  • Load changelog topics via import.meta.glob so the per-page route works on fresh checkouts where sidebar-changelog.generated.ts is absent.

💬 Review

  • The route's getStaticPaths now normalises doc.id for the slug (e.g. guides/indexguides). Old /guides/index.md-shaped URLs no longer exist — only matters if anything external bookmarks them.
  • import.meta.glob with eager: true is the deliberate choice over try/catch import(): sitemap-generator.ts consumes topics in a sync context, and the eager glob handles the missing-file case without a runtime check.
  • starlight-site-navigation still uses its own fs+regex loader for the changelog (needs both changelogTopics and changelogTopicPaths); left out of scope.
  • Icons in the new buttons are Lucide originals (copy, chevron-down, file-text, bot). Breadcrumb home/chevron icons were left as-is — pre-existing, hand-inlined to the same style.

@github-actions github-actions Bot added the site Site infrastructure label Jun 4, 2026
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

📦 Preview  ·  View →  ·  🟢 Live

Verified for 02d7a9b  ·  Auto-updates on push

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: be80473302

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/components/PageTitle.astro Outdated
Zedoraps added 3 commits June 4, 2026 13:22
Add a copy-for-LLM button and "View page as markdown" / "Use in
your agent" dropdown to every documentation page. The button copies the
page's Markdown twin to the clipboard; the menu links open the same
content for human or agent consumption.

Decouple the per-page `.md` route from the `LLMS_TXT` env flag via a
new plugin option, so the action works in normal dev and production
builds. The site-wide `/llms.txt` and `/llms-full.txt` routes stay
gated behind the flag.

Add a shared `markdownPathForDocPath` / `markdownUrlForDocPath`
helper so the per-page route, the copy URL in PageTitle, and link
rewriting in entry-to-markdown agree on slug normalization (`*/index`
→ `*`).

Move changelog topics behind an `import.meta.glob` loader so the
per-page route survives fresh checkouts where the generated
`sidebar-changelog.generated.ts` is absent.

Assisted-by: Claude Opus 4.7 (Claude Code)
OpenAPI pages (`/reference/{node,platform}/api/...`) render via
PageTitle but are not part of the `docs` content collection, so the
plugin generates no `.md` route for them. Clicking the copy or view
button would 404.

Gate the action buttons on `docsMap.has(starlightRoute.entry.id)` so
they only appear on pages backed by the collection. Future
integrations that inject pages outside the collection are covered by
the same check.

Assisted-by: Claude Opus 4.7 (Claude Code)
The button JS used to fire a fetch for the page's Markdown twin on
every page load and kept the copy button disabled until the response
arrived. Most readers never use the copy action, so this was one extra
page-sized request per view, and on slow links the action was
unavailable until the prefetch settled.

Flip the SSR-disabled buttons to idle on JS init without fetching. The
existing pointerover and focusin listeners already prefetch on user
intent, and the click handler fetches inline when the cache is empty.

Assisted-by: Claude Opus 4.7 (Claude Code)
@Zedoraps Zedoraps force-pushed the feat/copy-page-markdown-llm branch from b4de13f to 19841e5 Compare June 4, 2026 11:22
Internal links in the generated `.md` were root-relative
(`/foo.md`) while the appended `## Contents` section was absolute
(`https://docs.tenzir.com/foo.md`). The mismatch was awkward and the
relative form was the wrong default: an LLM or agent reading a copied
page has no origin to resolve `/foo.md` against.

Wrap the rehype/remark pipeline in a per-baseUrl factory with a small
cache, and have the link rewriter prepend the site URL. Non-markdown
internal paths (redirects like `/discord`, OpenAPI roots) are
absolutised too without appending `.md`.

Assisted-by: Claude Opus 4.7 (Claude Code)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 02d7a9b2d3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 71 to +72
const cleanPath = path.endsWith("/") ? path.slice(0, -1) : path;
link.properties.href = `${cleanPath}.md${suffix}`;
link.properties.href = `${markdownUrlForDocPath(baseUrl, cleanPath)}${suffix}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Strip the configured base before rewriting Markdown links

When the docs are built with --base (which this repo explicitly supports in astro.config.mjs), rendered internal anchors have already been prefixed by rehypeBaseLinks, e.g. /preview/guides/quickstart/, while baseUrl also resolves to https://docs.tenzir.com/preview. Passing that prefixed path into markdownUrlForDocPath() produces links like https://docs.tenzir.com/preview/preview/guides/quickstart.md, so copied/generated per-page Markdown contains broken internal links for base-path deployments. Strip the configured base from cleanPath before treating it as a doc path.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

site Site infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant