accept-md-runtime 4.0.6-canary.0 → 5.0.0

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # accept-md-runtime
2
2
 
3
- Runtime for [accept-md](https://github.com/slick-enterprises/accept-md): HTML-to-Markdown conversion and the handler that serves markdown for Next.js pages when clients send `Accept: text/markdown`. Use this in your Next.js API route or Route Handler.
3
+ Runtime for [accept-md](https://github.com/slick-enterprises/accept-md): HTML-to-Markdown conversion and the handler that serves markdown for Next.js or SvelteKit pages when clients send `Accept: text/markdown`. Use this in your Next.js API route / Route Handler or SvelteKit `+server` route.
4
4
 
5
5
  ## Installation
6
6
 
@@ -13,7 +13,7 @@ pnpm add accept-md-runtime
13
13
 
14
14
  ## Quick setup
15
15
 
16
- 1. Add middleware that rewrites `Accept: text/markdown` requests to your handler (e.g. `/api/accept-md?path=...`).
16
+ 1. Add rewrites or middleware (Next.js) or a `hooks.server` handle (SvelteKit) that rewrites `Accept: text/markdown` requests to your handler (e.g. `/api/accept-md?path=...`).
17
17
  2. In that API route / Route Handler, call `getMarkdownForPath` with the request and path.
18
18
 
19
19
  Or use the CLI: `npx accept-md init` to generate middleware and handler for you.
package/dist/index.d.ts CHANGED
@@ -5,5 +5,5 @@ export { getMarkdownForPath } from './handler.js';
5
5
  export type { GetMarkdownOptions } from './handler.js';
6
6
  export type { NextMarkdownConfig } from './types.js';
7
7
  export { loadConfig } from './config.js';
8
- export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_TEMPLATE, SVELTEKIT_ROUTE_HANDLER_TEMPLATE, getNextConfigRewrite, } from './templates.js';
8
+ export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_TEMPLATE, SVELTEKIT_ROUTE_HANDLER_TEMPLATE, SVELTEKIT_HOOKS_TEMPLATE, getNextConfigRewrite, } from './templates.js';
9
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvD,YAAY,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,0BAA0B,EAC1B,gCAAgC,EAChC,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvD,YAAY,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,0BAA0B,EAC1B,gCAAgC,EAChC,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -2,5 +2,5 @@
2
2
  export { htmlToMarkdown } from './markdown.js';
3
3
  export { getMarkdownForPath } from './handler.js';
4
4
  export { loadConfig } from './config.js';
5
- export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_TEMPLATE, SVELTEKIT_ROUTE_HANDLER_TEMPLATE, getNextConfigRewrite, } from './templates.js';
5
+ export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_TEMPLATE, SVELTEKIT_ROUTE_HANDLER_TEMPLATE, SVELTEKIT_HOOKS_TEMPLATE, getNextConfigRewrite, } from './templates.js';
6
6
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGlD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,0BAA0B,EAC1B,gCAAgC,EAChC,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGlD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,0BAA0B,EAC1B,gCAAgC,EAChC,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
@@ -5,6 +5,7 @@ export declare const MIDDLEWARE_TEMPLATE = "// Generated by accept-md. Do not ed
5
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 // Headers that may carry the original matched path when using rewrites on Vercel/Next.js\n const pathFromMatchedHeader =\n request.headers.get('x-matched-path') ||\n request.headers.get('x-vercel-original-path') ||\n request.headers.get('x-original-path') ||\n request.headers.get('x-rewrite-path');\n const pathname = request.nextUrl.pathname;\n // Never use the handler path itself - always prefer, in order:\n // 1) explicit header, 2) internal matched-path header, 3) query param, 4) pathname (if not handler), then default to '/'\n let path = pathFromHeader && pathFromHeader.trim() !== '' ? pathFromHeader : null;\n if (!path && pathFromMatchedHeader && pathFromMatchedHeader.trim() !== '') {\n path = pathFromMatchedHeader;\n }\n if (!path && pathFromQuery && pathFromQuery.trim() !== '') {\n path = pathFromQuery;\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
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 pathFromMatchedHeader =\n req.headers['x-matched-path'] ||\n req.headers['x-vercel-original-path'] ||\n req.headers['x-original-path'] ||\n req.headers['x-rewrite-path'];\n // Determine raw path in priority order: header, internal matched-path header, query, then fallback\n let pathRaw = pathFromHeader || pathFromMatchedHeader || 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
  export declare const SVELTEKIT_ROUTE_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();\nconst HANDLER_PATH = '/api/accept-md';\n\nexport async function GET(event) {\n const url = event.url;\n const request = event.request;\n\n const pathFromHeader = request.headers.get('x-accept-md-path');\n const pathFromQuery = url.searchParams.get('path');\n const pathname = url.pathname;\n\n // Determine raw path in priority order:\n // 1) explicit header\n // 2) query parameter\n // 3) path suffix after /api/accept-md (for /api/accept-md/[...path] routes)\n let path = pathFromHeader && pathFromHeader.trim() !== '' ? pathFromHeader : null;\n if (!path && pathFromQuery && pathFromQuery.trim() !== '') {\n path = pathFromQuery;\n }\n if (!path && pathname.startsWith(HANDLER_PATH + '/')) {\n path = pathname.slice(HANDLER_PATH.length);\n if (path === '') {\n path = '/';\n }\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 (keeps behavior aligned with Next.js)\n if (path.startsWith('/api/') || path.startsWith('/_next/')) {\n return new Response(JSON.stringify({ error: 'Not found' }), {\n status: 404,\n headers: {\n 'Content-Type': 'application/json; charset=utf-8',\n },\n });\n }\n\n const config = loadConfig(process.cwd());\n\n // Construct baseUrl: prefer config, then use request origin, fall back to localhost\n let baseUrl = config.baseUrl;\n if (!baseUrl) {\n baseUrl = url.origin || 'http://localhost:' + (process.env.PORT || 5173);\n }\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\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 Response(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 const message = err instanceof Error ? err.message : 'Markdown generation failed';\n return new Response(JSON.stringify({ error: message }), {\n status: 500,\n headers: {\n 'Content-Type': 'application/json; charset=utf-8',\n },\n });\n }\n}\n";
8
+ export declare const SVELTEKIT_HOOKS_TEMPLATE = "// Generated by accept-md. Do not edit the markdown block by hand.\n\n/** @type { import('@sveltejs/kit').Handle } */\nexport const handle = async ({ event, resolve }) => {\n const url = new URL(event.request.url);\n\n // Don't rewrite the handler itself to avoid loops\n if (url.pathname.startsWith('/api/accept-md')) {\n return resolve(event);\n }\n\n const accept = event.request.headers.get('accept') || '';\n\n if (/\\btext\\/markdown\\b/i.test(accept)) {\n const markdownUrl = new URL(`/api/accept-md${url.pathname}`, url.origin);\n markdownUrl.searchParams.set('path', url.pathname);\n\n return await fetch(markdownUrl.toString(), {\n method: 'GET',\n headers: event.request.headers,\n });\n }\n\n return resolve(event);\n};\n";
8
9
  /**
9
10
  * Returns the rewrite configuration object for next.config.js/ts
10
11
  * 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,spGAgFtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,qyGAiFtC,CAAC;AAEF,eAAO,MAAM,gCAAgC,qmFAiF5C,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,spGAgFtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,qyGAiFtC,CAAC;AAEF,eAAO,MAAM,gCAAgC,qmFAiF5C,CAAC;AAEF,eAAO,MAAM,wBAAwB,mwBAyBpC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB;;;;;;;;EAYnC"}
package/dist/templates.js CHANGED
@@ -268,6 +268,32 @@ export async function GET(event) {
268
268
  }
269
269
  }
270
270
  `;
271
+ export const SVELTEKIT_HOOKS_TEMPLATE = `// Generated by accept-md. Do not edit the markdown block by hand.
272
+
273
+ /** @type { import('@sveltejs/kit').Handle } */
274
+ export const handle = async ({ event, resolve }) => {
275
+ const url = new URL(event.request.url);
276
+
277
+ // Don't rewrite the handler itself to avoid loops
278
+ if (url.pathname.startsWith('/api/accept-md')) {
279
+ return resolve(event);
280
+ }
281
+
282
+ const accept = event.request.headers.get('accept') || '';
283
+
284
+ if (/\\btext\\/markdown\\b/i.test(accept)) {
285
+ const markdownUrl = new URL(\`/api/accept-md\${url.pathname}\`, url.origin);
286
+ markdownUrl.searchParams.set('path', url.pathname);
287
+
288
+ return await fetch(markdownUrl.toString(), {
289
+ method: 'GET',
290
+ headers: event.request.headers,
291
+ });
292
+ }
293
+
294
+ return resolve(event);
295
+ };
296
+ `;
271
297
  /**
272
298
  * Returns the rewrite configuration object for next.config.js/ts
273
299
  * This is the preferred method over middleware (Next.js is moving away from middleware).
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgFzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiFzC,CAAC;AAEF,MAAM,CAAC,MAAM,gCAAgC,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiF/C,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"}
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgFzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiFzC,CAAC;AAEF,MAAM,CAAC,MAAM,gCAAgC,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiF/C,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;CAyBvC,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.6-canary.0",
3
+ "version": "5.0.0",
4
4
  "description": "HTML→Markdown conversion and route handler for accept-md (Next.js)",
5
5
  "type": "module",
6
6
  "license": "MIT",