accept-md-runtime 4.0.2 → 4.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,8 +2,8 @@
2
2
  * Generated middleware and route handler templates.
3
3
  */
4
4
  export declare const MIDDLEWARE_TEMPLATE = "// Generated by accept-md. Do not edit the markdown block by hand.\nimport { NextResponse } from 'next/server';\n\nconst MARKDOWN_ACCEPT = new RegExp('\\\\btext/markdown\\\\b', 'i');\nconst EXCLUDED_PREFIXES = ['/api/', '/_next/'];\nconst MARKDOWN_HANDLER_PATH = '/api/accept-md';\n\n/** @param {import('next/server').NextRequest} request */\nexport function middleware(request) {\n const pathname = request.nextUrl.pathname;\n const accept = (request.headers.get('accept') || '').toLowerCase();\n if (!MARKDOWN_ACCEPT.test(accept)) return NextResponse.next();\n if (EXCLUDED_PREFIXES.some((p) => pathname.startsWith(p))) return NextResponse.next();\n\n const url = request.nextUrl.clone();\n url.pathname = MARKDOWN_HANDLER_PATH;\n url.searchParams.set('path', pathname);\n // Let Vercel/Next.js forward all original request metadata (auth, cookies, protection)\n // and only use the query parameter to communicate the original pathname.\n return NextResponse.rewrite(url);\n}\n";
5
- export declare const APP_ROUTE_HANDLER_TEMPLATE = "// Generated by accept-md. Do not edit the markdown block by hand.\nimport { NextResponse } from 'next/server';\nimport { getMarkdownForPath, loadConfig } from 'accept-md-runtime';\n\nconst cache = new Map();\nconst HANDLER_PATH = '/api/accept-md';\n\n/** @param {import('next/server').NextRequest} request */\nexport async function GET(request) {\n const pathFromHeader = request.headers.get('x-accept-md-path');\n const pathFromQuery = request.nextUrl.searchParams.get('path');\n const pathname = request.nextUrl.pathname;\n // Never use the handler path itself - always prefer header, then query, then pathname (if not handler), then default to '/'\n let path = pathFromHeader;\n if (!path || path.trim() === '') {\n path = pathFromQuery && pathFromQuery.trim() !== '' ? pathFromQuery : null;\n }\n // If pathname starts with /api/accept-md, extract the original path from it\n // This handles next.config rewrites that use /api/accept-md/:path* pattern\n if (!path && pathname.startsWith(HANDLER_PATH + '/')) {\n path = pathname.slice(HANDLER_PATH.length);\n // Handle root path case: /api/accept-md/ becomes /\n if (path === '') {\n path = '/';\n }\n }\n if (!path) {\n path = pathname !== HANDLER_PATH ? pathname : null;\n }\n if (!path || path === HANDLER_PATH) {\n path = '/';\n }\n // Ensure path starts with /\n if (!path.startsWith('/')) {\n path = '/' + path;\n }\n // Exclude /api and /_next paths - return 404 for these\n if (path.startsWith('/api/') || path.startsWith('/_next/')) {\n return NextResponse.json({ error: 'Not found' }, { status: 404 });\n }\n const config = loadConfig(process.cwd());\n // Construct baseUrl reliably: prefer config, then use request origin, fall back to localhost\n let baseUrl = config.baseUrl;\n if (!baseUrl) {\n baseUrl = request.nextUrl.origin || 'http://localhost:' + (process.env.PORT || 3000);\n }\n try {\n const markdown = await getMarkdownForPath({\n pathname: path,\n baseUrl,\n config,\n cache: config.cache !== false ? cache : undefined,\n headers: request.headers,\n });\n return new NextResponse(markdown, {\n headers: {\n 'Content-Type': 'text/markdown; charset=utf-8',\n 'Cache-Control': config.cache ? 'public, s-maxage=60, stale-while-revalidate' : 'no-store',\n },\n });\n } catch (err) {\n return NextResponse.json(\n { error: err instanceof Error ? err.message : 'Markdown generation failed' },\n { status: 500 }\n );\n }\n}\n";
6
- export declare const PAGES_API_HANDLER_TEMPLATE = "// Generated by accept-md. Do not edit the markdown block by hand.\nimport { getMarkdownForPath, loadConfig } from 'accept-md-runtime';\n\nconst cache = new Map();\n\n/** @param {import('next').NextApiRequest} req @param {import('next').NextApiResponse} res */\nexport default async function handler(req, res) {\n if (req.method !== 'GET') {\n res.setHeader('Allow', 'GET');\n return res.status(405).end();\n }\n const pathFromHeader = req.headers['x-accept-md-path'];\n const pathFromQuery = Array.isArray(req.query.path) ? req.query.path[0] : req.query.path;\n const pathRaw = (pathFromHeader || pathFromQuery) || '/';\n let path = typeof pathRaw === 'string' ? pathRaw : (pathRaw[0] || '/');\n // Ensure path starts with /\n if (!path.startsWith('/')) {\n path = '/' + path;\n }\n // Exclude /api and /_next paths - return 404 for these\n if (path.startsWith('/api/') || path.startsWith('/_next/')) {\n return res.status(404).json({ error: 'Not found' });\n }\n const config = loadConfig(process.cwd());\n // Construct baseUrl reliably on Vercel: use host header with protocol, fall back to origin/referer, then VERCEL_URL, then localhost\n let baseUrl = config.baseUrl;\n if (!baseUrl) {\n const host = req.headers.host;\n if (host) {\n const protocol = req.headers['x-forwarded-proto'] || (process.env.VERCEL_URL ? 'https' : 'http');\n baseUrl = protocol + '://' + host;\n } else {\n const originOrReferer = (req.headers.origin || req.headers.referer || '').replace(/\\\\/?$/, '');\n if (originOrReferer) {\n baseUrl = originOrReferer;\n } else if (process.env.VERCEL_URL) {\n baseUrl = process.env.VERCEL_URL.startsWith('http') ? process.env.VERCEL_URL : 'https://' + process.env.VERCEL_URL;\n } else {\n baseUrl = 'http://localhost:' + (process.env.PORT || 3000);\n }\n }\n }\n // Convert req.headers to Headers for forwarding (e.g., for Vercel deployment protection)\n const headers = new Headers();\n for (const [key, value] of Object.entries(req.headers)) {\n if (value) {\n headers.set(key, Array.isArray(value) ? value[0] : value);\n }\n }\n try {\n const markdown = await getMarkdownForPath({\n pathname: path,\n baseUrl,\n config,\n cache: config.cache !== false ? cache : undefined,\n headers,\n });\n res.setHeader('Content-Type', 'text/markdown; charset=utf-8');\n if (config.cache) {\n res.setHeader('Cache-Control', 'public, s-maxage=60, stale-while-revalidate');\n }\n res.status(200).send(markdown);\n } catch (err) {\n res.status(500).json({\n error: err instanceof Error ? err.message : 'Markdown generation failed',\n });\n }\n}\n";
5
+ export declare const APP_ROUTE_HANDLER_TEMPLATE = "// Generated by accept-md. Do not edit the markdown block by hand.\nimport { NextResponse } from 'next/server';\nimport { getMarkdownForPath, loadConfig } from 'accept-md-runtime';\n\nconst cache = new Map();\nconst HANDLER_PATH = '/api/accept-md';\n\n/** @param {import('next/server').NextRequest} request */\nexport async function GET(request) {\n const pathFromHeader = request.headers.get('x-accept-md-path');\n const pathFromQuery = request.nextUrl.searchParams.get('path');\n const pathname = request.nextUrl.pathname;\n // Never use the handler path itself - always prefer header, then query, then pathname (if not handler), then default to '/'\n let path = pathFromHeader;\n if (!path || path.trim() === '') {\n path = pathFromQuery && pathFromQuery.trim() !== '' ? pathFromQuery : null;\n }\n // If pathname starts with /api/accept-md, extract the original path from it\n // This handles next.config rewrites that use /api/accept-md/:path* pattern\n if (!path && pathname.startsWith(HANDLER_PATH + '/')) {\n path = pathname.slice(HANDLER_PATH.length);\n // Handle root path case: /api/accept-md/ becomes /\n if (path === '') {\n path = '/';\n }\n }\n if (!path) {\n path = pathname !== HANDLER_PATH ? pathname : null;\n }\n if (!path || path === HANDLER_PATH) {\n path = '/';\n }\n // Ensure path starts with /\n if (!path.startsWith('/')) {\n path = '/' + path;\n }\n // Exclude /api and /_next paths - return 404 for these\n if (path.startsWith('/api/') || path.startsWith('/_next/')) {\n return NextResponse.json({ error: 'Not found' }, { status: 404 });\n }\n const config = loadConfig(process.cwd());\n // Construct baseUrl reliably: prefer config, then use request origin, fall back to localhost\n let baseUrl = config.baseUrl;\n if (!baseUrl) {\n baseUrl = request.nextUrl.origin || 'http://localhost:' + (process.env.PORT || 3000);\n }\n // Forward headers but avoid sending markdown Accept header to the upstream page fetch\n const headers = new Headers(request.headers);\n headers.delete('accept');\n try {\n const markdown = await getMarkdownForPath({\n pathname: path,\n baseUrl,\n config,\n cache: config.cache !== false ? cache : undefined,\n headers,\n });\n return new NextResponse(markdown, {\n headers: {\n 'Content-Type': 'text/markdown; charset=utf-8',\n 'Cache-Control': config.cache ? 'public, s-maxage=60, stale-while-revalidate' : 'no-store',\n },\n });\n } catch (err) {\n return NextResponse.json(\n { error: err instanceof Error ? err.message : 'Markdown generation failed' },\n { status: 500 }\n );\n }\n}\n";
6
+ export declare const PAGES_API_HANDLER_TEMPLATE = "// Generated by accept-md. Do not edit the markdown block by hand.\nimport { getMarkdownForPath, loadConfig } from 'accept-md-runtime';\n\nconst cache = new Map();\n\n/** @param {import('next').NextApiRequest} req @param {import('next').NextApiResponse} res */\nexport default async function handler(req, res) {\n if (req.method !== 'GET') {\n res.setHeader('Allow', 'GET');\n return res.status(405).end();\n }\n const pathFromHeader = req.headers['x-accept-md-path'];\n const pathFromQuery = Array.isArray(req.query.path) ? req.query.path[0] : req.query.path;\n const pathRaw = (pathFromHeader || pathFromQuery) || '/';\n // Handle placeholder values like \":path*\" that can appear from Next.js rewrite configs\n let path;\n if (typeof pathRaw === 'string') {\n path = (pathRaw === '' || pathRaw.includes(':path')) ? '/' : pathRaw;\n } else {\n path = pathRaw[0] || '/';\n }\n // Ensure path starts with /\n if (!path.startsWith('/')) {\n path = '/' + path;\n }\n // Exclude /api and /_next paths - return 404 for these\n if (path.startsWith('/api/') || path.startsWith('/_next/')) {\n return res.status(404).json({ error: 'Not found' });\n }\n const config = loadConfig(process.cwd());\n // Construct baseUrl reliably on Vercel: use host header with protocol, fall back to origin/referer, then VERCEL_URL, then localhost\n let baseUrl = config.baseUrl;\n if (!baseUrl) {\n const host = req.headers.host;\n if (host) {\n const protocol = req.headers['x-forwarded-proto'] || (process.env.VERCEL_URL ? 'https' : 'http');\n baseUrl = protocol + '://' + host;\n } else {\n const originOrReferer = (req.headers.origin || req.headers.referer || '').replace(/\\\\/?$/, '');\n if (originOrReferer) {\n baseUrl = originOrReferer;\n } else if (process.env.VERCEL_URL) {\n baseUrl = process.env.VERCEL_URL.startsWith('http') ? process.env.VERCEL_URL : 'https://' + process.env.VERCEL_URL;\n } else {\n baseUrl = 'http://localhost:' + (process.env.PORT || 3000);\n }\n }\n }\n // Convert req.headers to Headers for forwarding (e.g., for Vercel deployment protection)\n const headers = new Headers();\n for (const [key, value] of Object.entries(req.headers)) {\n if (!value) continue;\n // Do not forward markdown Accept header to the upstream page fetch\n if (key.toLowerCase() === 'accept') continue;\n headers.set(key, Array.isArray(value) ? value[0] : value);\n }\n try {\n const markdown = await getMarkdownForPath({\n pathname: path,\n baseUrl,\n config,\n cache: config.cache !== false ? cache : undefined,\n headers,\n });\n res.setHeader('Content-Type', 'text/markdown; charset=utf-8');\n if (config.cache) {\n res.setHeader('Cache-Control', 'public, s-maxage=60, stale-while-revalidate');\n }\n res.status(200).send(markdown);\n } catch (err) {\n res.status(500).json({\n error: err instanceof Error ? err.message : 'Markdown generation failed',\n });\n }\n}\n";
7
7
  /**
8
8
  * Returns the rewrite configuration object for next.config.js/ts
9
9
  * This is the preferred method over middleware (Next.js is moving away from middleware).
@@ -1 +1 @@
1
- {"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,mBAAmB,k+BAqB/B,CAAC;AAEF,eAAO,MAAM,0BAA0B,8+EAmEtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,6qFAoEtC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB;;;;;;;;EAYnC"}
1
+ {"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,mBAAmB,k+BAqB/B,CAAC;AAEF,eAAO,MAAM,0BAA0B,qoFAsEtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,y+FA2EtC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB;;;;;;;;EAYnC"}
package/dist/templates.js CHANGED
@@ -69,13 +69,16 @@ export async function GET(request) {
69
69
  if (!baseUrl) {
70
70
  baseUrl = request.nextUrl.origin || 'http://localhost:' + (process.env.PORT || 3000);
71
71
  }
72
+ // Forward headers but avoid sending markdown Accept header to the upstream page fetch
73
+ const headers = new Headers(request.headers);
74
+ headers.delete('accept');
72
75
  try {
73
76
  const markdown = await getMarkdownForPath({
74
77
  pathname: path,
75
78
  baseUrl,
76
79
  config,
77
80
  cache: config.cache !== false ? cache : undefined,
78
- headers: request.headers,
81
+ headers,
79
82
  });
80
83
  return new NextResponse(markdown, {
81
84
  headers: {
@@ -105,7 +108,13 @@ export default async function handler(req, res) {
105
108
  const pathFromHeader = req.headers['x-accept-md-path'];
106
109
  const pathFromQuery = Array.isArray(req.query.path) ? req.query.path[0] : req.query.path;
107
110
  const pathRaw = (pathFromHeader || pathFromQuery) || '/';
108
- let path = typeof pathRaw === 'string' ? pathRaw : (pathRaw[0] || '/');
111
+ // Handle placeholder values like ":path*" that can appear from Next.js rewrite configs
112
+ let path;
113
+ if (typeof pathRaw === 'string') {
114
+ path = (pathRaw === '' || pathRaw.includes(':path')) ? '/' : pathRaw;
115
+ } else {
116
+ path = pathRaw[0] || '/';
117
+ }
109
118
  // Ensure path starts with /
110
119
  if (!path.startsWith('/')) {
111
120
  path = '/' + path;
@@ -136,9 +145,10 @@ export default async function handler(req, res) {
136
145
  // Convert req.headers to Headers for forwarding (e.g., for Vercel deployment protection)
137
146
  const headers = new Headers();
138
147
  for (const [key, value] of Object.entries(req.headers)) {
139
- if (value) {
140
- headers.set(key, Array.isArray(value) ? value[0] : value);
141
- }
148
+ if (!value) continue;
149
+ // Do not forward markdown Accept header to the upstream page fetch
150
+ if (key.toLowerCase() === 'accept') continue;
151
+ headers.set(key, Array.isArray(value) ? value[0] : value);
142
152
  }
143
153
  try {
144
154
  const markdown = await getMarkdownForPath({
@@ -167,7 +177,7 @@ export default async function handler(req, res) {
167
177
  export function getNextConfigRewrite() {
168
178
  return {
169
179
  source: '/:path*',
170
- destination: '/api/accept-md?path=:path*',
180
+ destination: '/api/accept-md/:path*',
171
181
  has: [
172
182
  {
173
183
  type: 'header',
@@ -1 +1 @@
1
- {"version":3,"file":"templates.js","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,mBAAmB,GAAG;;;;;;;;;;;;;;;;;;;;;CAqBlC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmEzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoEzC,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO;QACL,MAAM,EAAE,SAAS;QACjB,WAAW,EAAE,4BAA4B;QACzC,GAAG,EAAE;YACH;gBACE,IAAI,EAAE,QAAQ;gBACd,GAAG,EAAE,QAAQ;gBACb,KAAK,EAAE,uBAAuB;aAC/B;SACF;KACF,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"templates.js","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,mBAAmB,GAAG;;;;;;;;;;;;;;;;;;;;;CAqBlC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsEzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2EzC,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO;QACL,MAAM,EAAE,SAAS;QACjB,WAAW,EAAE,uBAAuB;QACpC,GAAG,EAAE;YACH;gBACE,IAAI,EAAE,QAAQ;gBACd,GAAG,EAAE,QAAQ;gBACb,KAAK,EAAE,uBAAuB;aAC/B;SACF;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "accept-md-runtime",
3
- "version": "4.0.2",
3
+ "version": "4.0.4",
4
4
  "description": "HTML→Markdown conversion and route handler for accept-md (Next.js)",
5
5
  "type": "module",
6
6
  "license": "MIT",