-
Notifications
You must be signed in to change notification settings - Fork 44
fix: serve markdown via routing middleware for Accept: text/markdown #508
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,59 @@ | ||
| /** | ||
| * Vercel Routing Middleware for developers.plane.so — markdown content negotiation. | ||
| * | ||
| * Requests that prefer `text/markdown` over `text/html` are rewritten to the page's | ||
| * markdown source (`/foo` → `/foo.md`, `/` → `/index.md`); buildEnd() in | ||
| * docs/.vitepress/config.mts copies those files into dist/. Every other request | ||
| * falls through to the HTML page. Both variants carry `Vary: Accept` so | ||
| * downstream caches keep them apart (https://acceptmarkdown.com). | ||
| * | ||
| * This lives in middleware rather than a `rewrites` entry in vercel.json | ||
| * because vercel.json rewrites are only evaluated after the filesystem: with | ||
| * cleanUrls the static foo.html always matched first, so the rewrite never | ||
| * ran and agents got HTML. Middleware runs before the filesystem and before | ||
| * the CDN cache, and the rewritten path gives the markdown variant its own | ||
| * cache key. | ||
| * | ||
| * Mirror of apps/docs/middleware.ts — keep the two in sync. | ||
| */ | ||
| import { next, rewrite } from "@vercel/functions"; | ||
| import Negotiator from "negotiator"; | ||
|
|
||
| export const config = { | ||
| // Page URLs only. Skip the Vite asset dir and known static-file extensions | ||
| // (.md, sitemap.xml, llms.txt, images, fonts, ...). Listed | ||
| // explicitly instead of "contains a dot" because some page slugs contain | ||
| // version numbers. | ||
| matcher: [ | ||
| "/((?!assets/|.*\\.(?:md|html|xml|txt|json|js|mjs|css|map|png|jpe?g|gif|svg|webp|avif|ico|woff2?|ttf|otf|pdf|zip)$).*)", | ||
| ], | ||
| }; | ||
|
|
||
| const VARY_ACCEPT = { Vary: "Accept" }; | ||
| const HTML = "text/html; charset=utf-8"; | ||
| const MARKDOWN = "text/markdown; charset=utf-8"; | ||
| const REPRESENTATIONS = [HTML, MARKDOWN]; | ||
|
|
||
| function prefersMarkdown(accept: string | null): boolean { | ||
| try { | ||
| const negotiator = new Negotiator({ headers: { accept: accept ?? undefined } }); | ||
| const explicitlyAcceptsMarkdown = negotiator | ||
| .mediaTypes() | ||
| .some((mediaType) => mediaType.toLowerCase() === "text/markdown"); | ||
| return explicitlyAcceptsMarkdown && negotiator.mediaType(REPRESENTATIONS) === MARKDOWN; | ||
| } catch { | ||
| // Fall back to HTML when the header cannot be parsed. | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export default function middleware(request: Request): Response { | ||
| if (!prefersMarkdown(request.headers.get("accept"))) { | ||
| return next({ headers: VARY_ACCEPT }); | ||
| } | ||
|
|
||
| const url = new URL(request.url); | ||
| const path = url.pathname.replace(/\/+$/, ""); | ||
| url.pathname = path === "" ? "/index.md" : `${path}.md`; | ||
| return rewrite(url, { headers: VARY_ACCEPT }); | ||
| } | ||
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 |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "include": ["docs/.vitepress/**/*.ts", "docs/.vitepress/**/*.mts", "docs/.vitepress/**/*.vue"], | ||
| "include": [ | ||
| "middleware.ts", | ||
| "docs/.vitepress/**/*.ts", | ||
| "docs/.vitepress/**/*.mts", | ||
| "docs/.vitepress/**/*.vue" | ||
| ], | ||
| "exclude": ["docs/.vitepress/cache", "docs/.vitepress/dist", "docs/.vitepress/.temp"] | ||
| } |
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
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,59 @@ | ||
| /** | ||
| * Vercel Routing Middleware for docs.plane.so — markdown content negotiation. | ||
| * | ||
| * Requests that prefer `text/markdown` over `text/html` are rewritten to the page's | ||
| * markdown source (`/foo` → `/foo.md`, `/` → `/index.md`); buildEnd() in | ||
| * docs/.vitepress/config.ts copies those files into dist/. Every other request | ||
| * falls through to the HTML page. Both variants carry `Vary: Accept` so | ||
| * downstream caches keep them apart (https://acceptmarkdown.com). | ||
| * | ||
| * This lives in middleware rather than a `rewrites` entry in vercel.json | ||
| * because vercel.json rewrites are only evaluated after the filesystem: with | ||
| * cleanUrls the static foo.html always matched first, so the rewrite never | ||
| * ran and agents got HTML. Middleware runs before the filesystem and before | ||
| * the CDN cache, and the rewritten path gives the markdown variant its own | ||
| * cache key. | ||
| * | ||
| * Mirror of apps/developer-docs/middleware.ts — keep the two in sync. | ||
| */ | ||
| import { next, rewrite } from "@vercel/functions"; | ||
| import Negotiator from "negotiator"; | ||
|
|
||
| export const config = { | ||
| // Page URLs only. Skip the Vite asset dir and known static-file extensions | ||
| // (.md, sitemap.xml, llms.txt, images, fonts, ...). Listed | ||
| // explicitly instead of "contains a dot" because some page slugs contain | ||
| // version numbers. | ||
| matcher: [ | ||
| "/((?!assets/|.*\\.(?:md|html|xml|txt|json|js|mjs|css|map|png|jpe?g|gif|svg|webp|avif|ico|woff2?|ttf|otf|pdf|zip)$).*)", | ||
| ], | ||
| }; | ||
|
|
||
| const VARY_ACCEPT = { Vary: "Accept" }; | ||
| const HTML = "text/html; charset=utf-8"; | ||
| const MARKDOWN = "text/markdown; charset=utf-8"; | ||
| const REPRESENTATIONS = [HTML, MARKDOWN]; | ||
|
|
||
| function prefersMarkdown(accept: string | null): boolean { | ||
| try { | ||
| const negotiator = new Negotiator({ headers: { accept: accept ?? undefined } }); | ||
| const explicitlyAcceptsMarkdown = negotiator | ||
| .mediaTypes() | ||
| .some((mediaType) => mediaType.toLowerCase() === "text/markdown"); | ||
| return explicitlyAcceptsMarkdown && negotiator.mediaType(REPRESENTATIONS) === MARKDOWN; | ||
| } catch { | ||
| // Fall back to HTML when the header cannot be parsed. | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export default function middleware(request: Request): Response { | ||
| if (!prefersMarkdown(request.headers.get("accept"))) { | ||
| return next({ headers: VARY_ACCEPT }); | ||
| } | ||
|
|
||
| const url = new URL(request.url); | ||
| const path = url.pathname.replace(/\/+$/, ""); | ||
| url.pathname = path === "" ? "/index.md" : `${path}.md`; | ||
| return rewrite(url, { headers: VARY_ACCEPT }); | ||
| } |
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 |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "include": ["docs/.vitepress/**/*.ts", "docs/.vitepress/**/*.mts", "docs/.vitepress/**/*.vue"], | ||
| "include": [ | ||
| "middleware.ts", | ||
| "docs/.vitepress/**/*.ts", | ||
| "docs/.vitepress/**/*.mts", | ||
| "docs/.vitepress/**/*.vue" | ||
| ], | ||
| "exclude": ["docs/.vitepress/cache", "docs/.vitepress/dist", "docs/.vitepress/.temp"] | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Exclude explicit-extension paths before rewriting.
Both matchers can route unlisted extensions or extension paths with trailing slashes into Markdown negotiation.
apps/developer-docs/middleware.ts#L21-L29: Complete the extension exclusion and handle optional trailing slashes.apps/docs/middleware.ts#L21-L29: Apply the same matcher fix.📍 Affects 2 files
apps/developer-docs/middleware.ts#L21-L29(this comment)apps/docs/middleware.ts#L21-L29🤖 Prompt for AI Agents