accept-md-runtime 1.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/dist/config.d.ts +6 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +34 -0
- package/dist/config.js.map +1 -0
- package/dist/handler.d.ts +16 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/handler.js +68 -0
- package/dist/handler.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown.d.ts +15 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +41 -0
- package/dist/markdown.js.map +1 -0
- package/dist/templates.d.ts +7 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +88 -0
- package/dist/templates.js.map +1 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +44 -0
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAgBrD,wBAAgB,UAAU,CAAC,WAAW,GAAE,MAAsB,GAAG,kBAAkB,CAalF"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime config loader for accept-md (avoids pulling in scanner).
|
|
3
|
+
*/
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const DEFAULTS = {
|
|
9
|
+
include: ['/**'],
|
|
10
|
+
exclude: ['/api/**', '/_next/**', '/__markdown/**'],
|
|
11
|
+
cleanSelectors: ['nav', 'footer', '.no-markdown', '[data-no-markdown]', 'script', 'style'],
|
|
12
|
+
outputMode: 'markdown',
|
|
13
|
+
cache: true,
|
|
14
|
+
transformers: [],
|
|
15
|
+
debug: false,
|
|
16
|
+
};
|
|
17
|
+
const CONFIG_NAMES = ['accept-md.config.js', 'accept-md.config.mjs', 'accept-md.config.cjs'];
|
|
18
|
+
export function loadConfig(projectRoot = process.cwd()) {
|
|
19
|
+
for (const name of CONFIG_NAMES) {
|
|
20
|
+
const path = join(projectRoot, name);
|
|
21
|
+
if (!existsSync(path))
|
|
22
|
+
continue;
|
|
23
|
+
try {
|
|
24
|
+
const mod = require(path);
|
|
25
|
+
const user = mod.default ?? mod;
|
|
26
|
+
return { ...DEFAULTS, ...user };
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
//
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return { ...DEFAULTS };
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C,MAAM,QAAQ,GAAuB;IACnC,OAAO,EAAE,CAAC,KAAK,CAAC;IAChB,OAAO,EAAE,CAAC,SAAS,EAAE,WAAW,EAAE,gBAAgB,CAAC;IACnD,cAAc,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,oBAAoB,EAAE,QAAQ,EAAE,OAAO,CAAC;IAC1F,UAAU,EAAE,UAAU;IACtB,KAAK,EAAE,IAAI;IACX,YAAY,EAAE,EAAE;IAChB,KAAK,EAAE,KAAK;CACb,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,qBAAqB,EAAE,sBAAsB,EAAE,sBAAsB,CAAC,CAAC;AAE7F,MAAM,UAAU,UAAU,CAAC,cAAsB,OAAO,CAAC,GAAG,EAAE;IAC5D,KAAK,MAAM,IAAI,IAAI,YAAY,EAAE,CAAC;QAChC,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;YAAE,SAAS;QAChC,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;YAC1B,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC;YAChC,OAAO,EAAE,GAAG,QAAQ,EAAE,GAAG,IAAI,EAAE,CAAC;QAClC,CAAC;QAAC,MAAM,CAAC;YACP,EAAE;QACJ,CAAC;IACH,CAAC;IACD,OAAO,EAAE,GAAG,QAAQ,EAAE,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handler logic: fetch page HTML and return markdown. Used by generated route/API.
|
|
3
|
+
*/
|
|
4
|
+
import type { NextMarkdownConfig } from './types.js';
|
|
5
|
+
export interface GetMarkdownOptions {
|
|
6
|
+
pathname: string;
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
config: NextMarkdownConfig;
|
|
9
|
+
/** In-memory cache (pathname -> markdown). Caller can pass a Map for caching. */
|
|
10
|
+
cache?: Map<string, string>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Fetch the page at baseUrl + pathname (as HTML), convert to markdown, optionally cache.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getMarkdownForPath(options: GetMarkdownOptions): Promise<string>;
|
|
16
|
+
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAgCrD,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,kBAAkB,CAAC;IAC3B,iFAAiF;IACjF,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC7B;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,CA2BrF"}
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handler logic: fetch page HTML and return markdown. Used by generated route/API.
|
|
3
|
+
*/
|
|
4
|
+
import { htmlToMarkdown } from './markdown.js';
|
|
5
|
+
const DEFAULT_EXCLUDE = ['/api/**', '/_next/**', '/__markdown/**'];
|
|
6
|
+
function pathMatches(pathname, patterns) {
|
|
7
|
+
const norm = pathname.endsWith('/') && pathname !== '/' ? pathname.slice(0, -1) : pathname;
|
|
8
|
+
const parts = norm.split('/').filter(Boolean);
|
|
9
|
+
for (const p of patterns) {
|
|
10
|
+
const segs = p.split('/').filter(Boolean);
|
|
11
|
+
if (segs[0] === '**')
|
|
12
|
+
return true;
|
|
13
|
+
let i = 0;
|
|
14
|
+
for (const seg of segs) {
|
|
15
|
+
if (seg === '**')
|
|
16
|
+
return true;
|
|
17
|
+
if (i >= parts.length)
|
|
18
|
+
break;
|
|
19
|
+
if (seg === '*' || seg === parts[i]) {
|
|
20
|
+
i++;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
if (i === segs.length && i === parts.length)
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
function shouldExclude(pathname, config) {
|
|
31
|
+
if (config.exclude?.length && pathMatches(pathname, config.exclude))
|
|
32
|
+
return true;
|
|
33
|
+
if (config.include && config.include.length > 0 && !pathMatches(pathname, config.include))
|
|
34
|
+
return true;
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Fetch the page at baseUrl + pathname (as HTML), convert to markdown, optionally cache.
|
|
39
|
+
*/
|
|
40
|
+
export async function getMarkdownForPath(options) {
|
|
41
|
+
const { pathname, baseUrl, config, cache } = options;
|
|
42
|
+
const normalizedPath = pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
43
|
+
if (shouldExclude(normalizedPath, config)) {
|
|
44
|
+
throw new Error(`Path excluded from markdown: ${normalizedPath}`);
|
|
45
|
+
}
|
|
46
|
+
const cacheKey = normalizedPath;
|
|
47
|
+
if (config.cache !== false && cache?.has(cacheKey)) {
|
|
48
|
+
return cache.get(cacheKey);
|
|
49
|
+
}
|
|
50
|
+
const url = `${baseUrl.replace(/\/$/, '')}${normalizedPath}`;
|
|
51
|
+
const res = await fetch(url, {
|
|
52
|
+
headers: { Accept: 'text/html' },
|
|
53
|
+
// Use same host so we hit our own app (SSR/SSG)
|
|
54
|
+
});
|
|
55
|
+
if (!res.ok) {
|
|
56
|
+
throw new Error(`Failed to fetch page: ${res.status} ${res.statusText}`);
|
|
57
|
+
}
|
|
58
|
+
const html = await res.text();
|
|
59
|
+
const md = htmlToMarkdown(html, {
|
|
60
|
+
cleanSelectors: config.cleanSelectors,
|
|
61
|
+
transformers: config.transformers,
|
|
62
|
+
});
|
|
63
|
+
if (config.cache !== false && cache) {
|
|
64
|
+
cache.set(cacheKey, md);
|
|
65
|
+
}
|
|
66
|
+
return md;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../src/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AE,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,yBAAyB,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;IAC3E,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;IAC9B,MAAM,EAAE,GAAG,cAAc,CAAC,IAAI,EAAE;QAC9B,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,YAAY,EAAE,MAAM,CAAC,YAAY;KAClC,CAAC,CAAC;IACH,IAAI,MAAM,CAAC,KAAK,KAAK,KAAK,IAAI,KAAK,EAAE,CAAC;QACpC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAC1B,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { htmlToMarkdown } from './markdown.js';
|
|
2
|
+
export type { MarkdownOptions } from './markdown.js';
|
|
3
|
+
export { getMarkdownForPath } from './handler.js';
|
|
4
|
+
export type { GetMarkdownOptions } from './handler.js';
|
|
5
|
+
export type { NextMarkdownConfig } from './types.js';
|
|
6
|
+
export { loadConfig } from './config.js';
|
|
7
|
+
export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_TEMPLATE } from './templates.js';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,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,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { htmlToMarkdown } from './markdown.js';
|
|
2
|
+
export { getMarkdownForPath } from './handler.js';
|
|
3
|
+
export { loadConfig } from './config.js';
|
|
4
|
+
export { MIDDLEWARE_TEMPLATE, APP_ROUTE_HANDLER_TEMPLATE, PAGES_API_HANDLER_TEMPLATE } from './templates.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,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,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML to Markdown conversion using Turndown with cleanup and optional linkedom for Node.
|
|
3
|
+
*/
|
|
4
|
+
export interface MarkdownOptions {
|
|
5
|
+
cleanSelectors?: string[];
|
|
6
|
+
headingStyle?: 'setext' | 'atx';
|
|
7
|
+
codeBlockStyle?: 'indented' | 'fenced';
|
|
8
|
+
/** Custom transformers applied after conversion (md -> md) */
|
|
9
|
+
transformers?: Array<(md: string) => string>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Convert HTML to Markdown. Preserves headings, images, links, tables; strips scripts/styles.
|
|
13
|
+
*/
|
|
14
|
+
export declare function htmlToMarkdown(html: string, options?: MarkdownOptions): string;
|
|
15
|
+
//# sourceMappingURL=markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,YAAY,CAAC,EAAE,QAAQ,GAAG,KAAK,CAAC;IAChC,cAAc,CAAC,EAAE,UAAU,GAAG,QAAQ,CAAC;IACvC,8DAA8D;IAC9D,YAAY,CAAC,EAAE,KAAK,CAAC,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;CAC9C;AAiBD;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,MAAM,CAwBlF"}
|
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML to Markdown conversion using Turndown with cleanup and optional linkedom for Node.
|
|
3
|
+
*/
|
|
4
|
+
import TurndownService from 'turndown';
|
|
5
|
+
import { parseHTML } from 'linkedom';
|
|
6
|
+
/**
|
|
7
|
+
* Parse HTML in Node (linkedom) and optionally remove nodes by selector before conversion.
|
|
8
|
+
*/
|
|
9
|
+
function cleanHtml(html, selectors) {
|
|
10
|
+
const { document } = parseHTML(html);
|
|
11
|
+
for (const sel of selectors) {
|
|
12
|
+
try {
|
|
13
|
+
document.querySelectorAll(sel).forEach((el) => el.remove());
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// invalid selector, skip
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return document.documentElement.outerHTML;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Convert HTML to Markdown. Preserves headings, images, links, tables; strips scripts/styles.
|
|
23
|
+
*/
|
|
24
|
+
export function htmlToMarkdown(html, options = {}) {
|
|
25
|
+
const { cleanSelectors = ['nav', 'footer', '.no-markdown', '[data-no-markdown]', 'script', 'style'], headingStyle = 'atx', codeBlockStyle = 'fenced', transformers = [], } = options;
|
|
26
|
+
const cleaned = cleanHtml(html, cleanSelectors);
|
|
27
|
+
const service = new TurndownService({
|
|
28
|
+
headingStyle,
|
|
29
|
+
codeBlockStyle,
|
|
30
|
+
bulletListMarker: '-',
|
|
31
|
+
linkStyle: 'inlined',
|
|
32
|
+
});
|
|
33
|
+
// Tables and default rules are fine; explicitly drop scripts/styles if still present
|
|
34
|
+
service.remove(['script', 'style', 'noscript']);
|
|
35
|
+
let md = service.turndown(cleaned);
|
|
36
|
+
for (const fn of transformers) {
|
|
37
|
+
md = fn(md);
|
|
38
|
+
}
|
|
39
|
+
return md.trim();
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=markdown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.js","sourceRoot":"","sources":["../src/markdown.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,eAAe,MAAM,UAAU,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAUrC;;GAEG;AACH,SAAS,SAAS,CAAC,IAAY,EAAE,SAAmB;IAClD,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IACrC,KAAK,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;QAC5B,IAAI,CAAC;YACH,QAAQ,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC;QAC9D,CAAC;QAAC,MAAM,CAAC;YACP,yBAAyB;QAC3B,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC,eAAe,CAAC,SAAS,CAAC;AAC5C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY,EAAE,UAA2B,EAAE;IACxE,MAAM,EACJ,cAAc,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,oBAAoB,EAAE,QAAQ,EAAE,OAAO,CAAC,EAC3F,YAAY,GAAG,KAAK,EACpB,cAAc,GAAG,QAAQ,EACzB,YAAY,GAAG,EAAE,GAClB,GAAG,OAAO,CAAC;IAEZ,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC;IAChD,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC;QAClC,YAAY;QACZ,cAAc;QACd,gBAAgB,EAAE,GAAG;QACrB,SAAS,EAAE,SAAS;KACrB,CAAC,CAAC;IAEH,qFAAqF;IACrF,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC;IAEhD,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACnC,KAAK,MAAM,EAAE,IAAI,YAAY,EAAE,CAAC;QAC9B,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;IACd,CAAC;IACD,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated middleware and route handler templates.
|
|
3
|
+
*/
|
|
4
|
+
export declare const MIDDLEWARE_TEMPLATE = "// Generated by accept-md. Do not edit the markdown block by hand.\nimport { NextResponse } from 'next/server';\nimport type { NextRequest } from 'next/server';\n\nconst MARKDOWN_ACCEPT = /\\\\btext\\\\/markdown\\\\b/;\nconst EXCLUDED_PREFIXES = ['/api/', '/_next/'];\nconst MARKDOWN_HANDLER_PATH = '/api/accept-md';\n\nexport function middleware(request: NextRequest) {\n const pathname = request.nextUrl.pathname;\n const accept = request.headers.get('accept') || '';\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 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 type { NextRequest } from 'next/server';\nimport { getMarkdownForPath, loadConfig } from 'accept-md-runtime';\n\nconst cache = new Map<string, string>();\n\nexport async function GET(request: NextRequest) {\n const path = request.nextUrl.searchParams.get('path') || '/';\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 });\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\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 path = req.query.path || '/';\n const config = loadConfig(process.cwd());\n const baseUrl = config.baseUrl || (req.headers.origin || req.headers.referer || '').replace(/\\\\/?$/, '') || ('http://localhost:' + (process.env.PORT || 3000));\n try {\n const markdown = await getMarkdownForPath({\n pathname: path,\n baseUrl,\n config,\n cache: config.cache !== false ? cache : undefined,\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
|
+
//# sourceMappingURL=templates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,mBAAmB,gyBAmB/B,CAAC;AAEF,eAAO,MAAM,0BAA0B,qkCA+BtC,CAAC;AAEF,eAAO,MAAM,0BAA0B,smCA+BtC,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated middleware and route handler templates.
|
|
3
|
+
*/
|
|
4
|
+
export const MIDDLEWARE_TEMPLATE = `// Generated by accept-md. Do not edit the markdown block by hand.
|
|
5
|
+
import { NextResponse } from 'next/server';
|
|
6
|
+
import type { NextRequest } from 'next/server';
|
|
7
|
+
|
|
8
|
+
const MARKDOWN_ACCEPT = /\\\\btext\\\\/markdown\\\\b/;
|
|
9
|
+
const EXCLUDED_PREFIXES = ['/api/', '/_next/'];
|
|
10
|
+
const MARKDOWN_HANDLER_PATH = '/api/accept-md';
|
|
11
|
+
|
|
12
|
+
export function middleware(request: NextRequest) {
|
|
13
|
+
const pathname = request.nextUrl.pathname;
|
|
14
|
+
const accept = request.headers.get('accept') || '';
|
|
15
|
+
if (!MARKDOWN_ACCEPT.test(accept)) return NextResponse.next();
|
|
16
|
+
if (EXCLUDED_PREFIXES.some((p) => pathname.startsWith(p))) return NextResponse.next();
|
|
17
|
+
|
|
18
|
+
const url = request.nextUrl.clone();
|
|
19
|
+
url.pathname = MARKDOWN_HANDLER_PATH;
|
|
20
|
+
url.searchParams.set('path', pathname);
|
|
21
|
+
return NextResponse.rewrite(url);
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
export const APP_ROUTE_HANDLER_TEMPLATE = `// Generated by accept-md. Do not edit the markdown block by hand.
|
|
25
|
+
import { NextResponse } from 'next/server';
|
|
26
|
+
import type { NextRequest } from 'next/server';
|
|
27
|
+
import { getMarkdownForPath, loadConfig } from 'accept-md-runtime';
|
|
28
|
+
|
|
29
|
+
const cache = new Map<string, string>();
|
|
30
|
+
|
|
31
|
+
export async function GET(request: NextRequest) {
|
|
32
|
+
const path = request.nextUrl.searchParams.get('path') || '/';
|
|
33
|
+
const config = loadConfig(process.cwd());
|
|
34
|
+
const baseUrl = config.baseUrl || request.nextUrl.origin;
|
|
35
|
+
try {
|
|
36
|
+
const markdown = await getMarkdownForPath({
|
|
37
|
+
pathname: path,
|
|
38
|
+
baseUrl,
|
|
39
|
+
config,
|
|
40
|
+
cache: config.cache !== false ? cache : undefined,
|
|
41
|
+
});
|
|
42
|
+
return new NextResponse(markdown, {
|
|
43
|
+
headers: {
|
|
44
|
+
'Content-Type': 'text/markdown; charset=utf-8',
|
|
45
|
+
'Cache-Control': config.cache ? 'public, s-maxage=60, stale-while-revalidate' : 'no-store',
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
} catch (err) {
|
|
49
|
+
return NextResponse.json(
|
|
50
|
+
{ error: err instanceof Error ? err.message : 'Markdown generation failed' },
|
|
51
|
+
{ status: 500 }
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
export const PAGES_API_HANDLER_TEMPLATE = `// Generated by accept-md. Do not edit the markdown block by hand.
|
|
57
|
+
import { getMarkdownForPath, loadConfig } from 'accept-md-runtime';
|
|
58
|
+
|
|
59
|
+
const cache = new Map();
|
|
60
|
+
|
|
61
|
+
export default async function handler(req, res) {
|
|
62
|
+
if (req.method !== 'GET') {
|
|
63
|
+
res.setHeader('Allow', 'GET');
|
|
64
|
+
return res.status(405).end();
|
|
65
|
+
}
|
|
66
|
+
const path = req.query.path || '/';
|
|
67
|
+
const config = loadConfig(process.cwd());
|
|
68
|
+
const baseUrl = config.baseUrl || (req.headers.origin || req.headers.referer || '').replace(/\\\\/?$/, '') || ('http://localhost:' + (process.env.PORT || 3000));
|
|
69
|
+
try {
|
|
70
|
+
const markdown = await getMarkdownForPath({
|
|
71
|
+
pathname: path,
|
|
72
|
+
baseUrl,
|
|
73
|
+
config,
|
|
74
|
+
cache: config.cache !== false ? cache : undefined,
|
|
75
|
+
});
|
|
76
|
+
res.setHeader('Content-Type', 'text/markdown; charset=utf-8');
|
|
77
|
+
if (config.cache) {
|
|
78
|
+
res.setHeader('Cache-Control', 'public, s-maxage=60, stale-while-revalidate');
|
|
79
|
+
}
|
|
80
|
+
res.status(200).send(markdown);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
res.status(500).json({
|
|
83
|
+
error: err instanceof Error ? err.message : 'Markdown generation failed',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
`;
|
|
88
|
+
//# sourceMappingURL=templates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.js","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,mBAAmB,GAAG;;;;;;;;;;;;;;;;;;;CAmBlC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BzC,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BzC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config type used at runtime by the handler (avoids pulling in full core).
|
|
3
|
+
*/
|
|
4
|
+
export interface NextMarkdownConfig {
|
|
5
|
+
include?: string[];
|
|
6
|
+
exclude?: string[];
|
|
7
|
+
cleanSelectors?: string[];
|
|
8
|
+
outputMode?: 'markdown';
|
|
9
|
+
cache?: boolean;
|
|
10
|
+
transformers?: Array<(md: string) => string>;
|
|
11
|
+
baseUrl?: string;
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,kBAAkB;IACjC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,YAAY,CAAC,EAAE,KAAK,CAAC,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "accept-md-runtime",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/hemanthvalsaraj/accept-md.git"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/hemanthvalsaraj/accept-md#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/hemanthvalsaraj/accept-md/issues"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"turndown": "^7.2.0",
|
|
27
|
+
"linkedom": "^0.18.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"next": ">=12.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^20.10.0",
|
|
34
|
+
"@types/turndown": "^5.0.0",
|
|
35
|
+
"typescript": "^5.3.0",
|
|
36
|
+
"vitest": "^1.2.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc",
|
|
40
|
+
"clean": "rm -rf dist",
|
|
41
|
+
"lint": "eslint src",
|
|
42
|
+
"test": "vitest run"
|
|
43
|
+
}
|
|
44
|
+
}
|