-
Notifications
You must be signed in to change notification settings - Fork 41
feat: serve markdown to agents via Accept content negotiation #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sriramveeraghanta
wants to merge
2
commits into
master
Choose a base branch
from
feat/markdown-for-agents
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** @format */ | ||
|
|
||
| import { next, rewrite } from "@vercel/functions"; | ||
|
|
||
| // Markdown content negotiation for agents. | ||
| // | ||
| // VitePress emits an HTML page for every doc, and buildEnd() in | ||
| // .vitepress/config.ts copies each source `.md` twin into dist/ alongside it | ||
| // (e.g. /core-concepts/issues/overview -> /core-concepts/issues/overview.md). | ||
| // | ||
| // A `vercel.json` rewrite can't serve those `.md` files on | ||
| // `Accept: text/markdown` because vercel.json rewrites run *after* the | ||
| // filesystem, and every clean URL already resolves to an existing `.html` file, | ||
| // so the rewrite is never reached. Routing Middleware runs *before* the | ||
| // filesystem, so it can transparently serve the `.md` twin at the same URL. | ||
| // | ||
| // HTML stays the default for browsers. Vercel's CDN already includes the | ||
| // `Accept` request header in its cache key, so the HTML and markdown variants | ||
| // of the same URL are cached as separate entries — no `Vary` header needed. | ||
| export const config = { | ||
| // Page routes only: skip built assets, fonts, and any path that already has a | ||
| // file extension (`.md`, `.txt`, `.xml`, `.js`, `.css`, images, …). | ||
| matcher: ["/", "/((?!assets/|fonts/|.*\\.).*)"], | ||
| }; | ||
|
|
||
| // True only when the client *explicitly* accepts `text/markdown` with a | ||
| // non-zero quality value. Per RFC 7231 the type/subtype and the `q` parameter | ||
| // are case-insensitive, and `q=0` means "not acceptable". Wildcards (`*/*`, | ||
| // `text/*`) are intentionally ignored so browsers — which never list | ||
| // `text/markdown` — keep getting HTML. | ||
| function acceptsMarkdown(accept: string): boolean { | ||
| for (const range of accept.split(",")) { | ||
| const params = range.trim().split(";"); | ||
| if (params[0].trim().toLowerCase() !== "text/markdown") continue; | ||
| let q = 1; | ||
| for (const param of params.slice(1)) { | ||
| const [key, value] = param.split("="); | ||
| if (key.trim().toLowerCase() === "q") { | ||
| const parsed = Number.parseFloat(value ?? ""); | ||
| if (!Number.isNaN(parsed)) q = parsed; | ||
| } | ||
| } | ||
| if (q > 0) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| export default function middleware(request: Request): Response { | ||
| const accept = request.headers.get("accept") || ""; | ||
| if (!acceptsMarkdown(accept)) { | ||
| return next(); | ||
| } | ||
|
|
||
| const url = new URL(request.url); | ||
| let pathname = url.pathname; | ||
|
|
||
| // Map the clean URL to its emitted `.md` twin. | ||
| if (pathname === "/" || pathname === "") { | ||
| pathname = "/index"; | ||
| } else if (pathname.endsWith("/")) { | ||
| pathname = pathname.slice(0, -1); | ||
| } | ||
| if (!pathname.endsWith(".md")) { | ||
| pathname = `${pathname}.md`; | ||
| } | ||
|
|
||
| url.pathname = pathname; | ||
| return rewrite(url); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.