accept-md-runtime 4.0.0 → 4.0.2

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 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 const path = typeof pathRaw === 'string' ? pathRaw : (pathRaw[0] || '/');\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 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";
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,qyEA+DtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,u5EA4DtC,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,8+EAmEtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,6qFAoEtC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB;;;;;;;;EAYnC"}
package/dist/templates.js CHANGED
@@ -59,6 +59,10 @@ export async function GET(request) {
59
59
  if (!path.startsWith('/')) {
60
60
  path = '/' + path;
61
61
  }
62
+ // Exclude /api and /_next paths - return 404 for these
63
+ if (path.startsWith('/api/') || path.startsWith('/_next/')) {
64
+ return NextResponse.json({ error: 'Not found' }, { status: 404 });
65
+ }
62
66
  const config = loadConfig(process.cwd());
63
67
  // Construct baseUrl reliably: prefer config, then use request origin, fall back to localhost
64
68
  let baseUrl = config.baseUrl;
@@ -101,7 +105,15 @@ export default async function handler(req, res) {
101
105
  const pathFromHeader = req.headers['x-accept-md-path'];
102
106
  const pathFromQuery = Array.isArray(req.query.path) ? req.query.path[0] : req.query.path;
103
107
  const pathRaw = (pathFromHeader || pathFromQuery) || '/';
104
- const path = typeof pathRaw === 'string' ? pathRaw : (pathRaw[0] || '/');
108
+ let path = typeof pathRaw === 'string' ? pathRaw : (pathRaw[0] || '/');
109
+ // Ensure path starts with /
110
+ if (!path.startsWith('/')) {
111
+ path = '/' + path;
112
+ }
113
+ // Exclude /api and /_next paths - return 404 for these
114
+ if (path.startsWith('/api/') || path.startsWith('/_next/')) {
115
+ return res.status(404).json({ error: 'Not found' });
116
+ }
105
117
  const config = loadConfig(process.cwd());
106
118
  // Construct baseUrl reliably on Vercel: use host header with protocol, fall back to origin/referer, then VERCEL_URL, then localhost
107
119
  let baseUrl = config.baseUrl;
@@ -154,8 +166,8 @@ export default async function handler(req, res) {
154
166
  */
155
167
  export function getNextConfigRewrite() {
156
168
  return {
157
- source: '/:path((?!api|_next).)*',
158
- destination: '/api/accept-md/:path*',
169
+ source: '/:path*',
170
+ destination: '/api/accept-md?path=:path*',
159
171
  has: [
160
172
  {
161
173
  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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+DzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4DzC,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO;QACL,MAAM,EAAE,yBAAyB;QACjC,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"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "accept-md-runtime",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "description": "HTML→Markdown conversion and route handler for accept-md (Next.js)",
5
5
  "type": "module",
6
6
  "license": "MIT",