accept-md-runtime 2.0.0 → 2.0.1
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/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/templates.d.ts +14 -1
- package/dist/templates.d.ts.map +1 -1
- package/dist/templates.js +35 -1
- package/dist/templates.js.map +1 -1
- package/package.json +1 -1
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 } from './templates.js';
|
|
8
|
+
export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_TEMPLATE, getNextConfigRewrite, } from './templates.js';
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,
|
|
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,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 } from './templates.js';
|
|
5
|
+
export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_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,
|
|
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,oBAAoB,GACrB,MAAM,gBAAgB,CAAC"}
|
package/dist/templates.d.ts
CHANGED
|
@@ -3,5 +3,18 @@
|
|
|
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
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 const path = pathFromHeader ?? pathFromQuery ?? (pathname !== HANDLER_PATH ? pathname : null) ?? '/';\n const config = loadConfig(process.cwd());\n const baseUrl = config.baseUrl || request.nextUrl.origin;\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
|
|
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";
|
|
7
|
+
/**
|
|
8
|
+
* Returns the rewrite configuration object for next.config.js/ts
|
|
9
|
+
* This is the preferred method over middleware (Next.js is moving away from middleware).
|
|
10
|
+
*/
|
|
11
|
+
export declare function getNextConfigRewrite(): {
|
|
12
|
+
source: string;
|
|
13
|
+
destination: string;
|
|
14
|
+
has: {
|
|
15
|
+
type: string;
|
|
16
|
+
key: string;
|
|
17
|
+
value: string;
|
|
18
|
+
}[];
|
|
19
|
+
};
|
|
7
20
|
//# sourceMappingURL=templates.d.ts.map
|
package/dist/templates.d.ts.map
CHANGED
|
@@ -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,u1CAoCtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,
|
|
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,u1CAoCtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,u5EA4DtC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB;;;;;;;;EAYnC"}
|
package/dist/templates.js
CHANGED
|
@@ -76,7 +76,24 @@ export default async function handler(req, res) {
|
|
|
76
76
|
const pathRaw = (pathFromHeader || pathFromQuery) || '/';
|
|
77
77
|
const path = typeof pathRaw === 'string' ? pathRaw : (pathRaw[0] || '/');
|
|
78
78
|
const config = loadConfig(process.cwd());
|
|
79
|
-
|
|
79
|
+
// Construct baseUrl reliably on Vercel: use host header with protocol, fall back to origin/referer, then VERCEL_URL, then localhost
|
|
80
|
+
let baseUrl = config.baseUrl;
|
|
81
|
+
if (!baseUrl) {
|
|
82
|
+
const host = req.headers.host;
|
|
83
|
+
if (host) {
|
|
84
|
+
const protocol = req.headers['x-forwarded-proto'] || (process.env.VERCEL_URL ? 'https' : 'http');
|
|
85
|
+
baseUrl = protocol + '://' + host;
|
|
86
|
+
} else {
|
|
87
|
+
const originOrReferer = (req.headers.origin || req.headers.referer || '').replace(/\\\\/?$/, '');
|
|
88
|
+
if (originOrReferer) {
|
|
89
|
+
baseUrl = originOrReferer;
|
|
90
|
+
} else if (process.env.VERCEL_URL) {
|
|
91
|
+
baseUrl = process.env.VERCEL_URL.startsWith('http') ? process.env.VERCEL_URL : 'https://' + process.env.VERCEL_URL;
|
|
92
|
+
} else {
|
|
93
|
+
baseUrl = 'http://localhost:' + (process.env.PORT || 3000);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
80
97
|
// Convert req.headers to Headers for forwarding (e.g., for Vercel deployment protection)
|
|
81
98
|
const headers = new Headers();
|
|
82
99
|
for (const [key, value] of Object.entries(req.headers)) {
|
|
@@ -104,4 +121,21 @@ export default async function handler(req, res) {
|
|
|
104
121
|
}
|
|
105
122
|
}
|
|
106
123
|
`;
|
|
124
|
+
/**
|
|
125
|
+
* Returns the rewrite configuration object for next.config.js/ts
|
|
126
|
+
* This is the preferred method over middleware (Next.js is moving away from middleware).
|
|
127
|
+
*/
|
|
128
|
+
export function getNextConfigRewrite() {
|
|
129
|
+
return {
|
|
130
|
+
source: '/:path*',
|
|
131
|
+
destination: '/api/accept-md?path=:path*',
|
|
132
|
+
has: [
|
|
133
|
+
{
|
|
134
|
+
type: 'header',
|
|
135
|
+
key: 'accept',
|
|
136
|
+
value: '(.*)text/markdown(.*)',
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
};
|
|
140
|
+
}
|
|
107
141
|
//# sourceMappingURL=templates.js.map
|
package/dist/templates.js.map
CHANGED
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoCzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG
|
|
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoCzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4DzC,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"}
|