@vllnt/next-llms 0.1.0-canary.78c9be3
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/LICENSE +21 -0
- package/README.md +121 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/llms-full.d.ts +14 -0
- package/dist/llms-full.d.ts.map +1 -0
- package/dist/llms-full.js +31 -0
- package/dist/llms-full.js.map +1 -0
- package/dist/llms-text.d.ts +23 -0
- package/dist/llms-text.d.ts.map +1 -0
- package/dist/llms-text.js +42 -0
- package/dist/llms-text.js.map +1 -0
- package/dist/markdown-route.d.ts +29 -0
- package/dist/markdown-route.d.ts.map +1 -0
- package/dist/markdown-route.js +62 -0
- package/dist/markdown-route.js.map +1 -0
- package/dist/types.d.ts +93 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +81 -0
- package/src/index.test.ts +184 -0
- package/src/index.ts +13 -0
- package/src/llms-full.ts +34 -0
- package/src/llms-text.ts +45 -0
- package/src/markdown-route.ts +71 -0
- package/src/types.ts +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 bntvllnt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
<!-- Badges -->
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@vllnt/next-llms)
|
|
4
|
+
[](https://github.com/vllnt/next-llms/actions/workflows/ci.yml)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
# @vllnt/next-llms
|
|
8
|
+
|
|
9
|
+
Make your Next.js site legible to AI agents — generate a spec-compliant
|
|
10
|
+
`llms.txt` / `llms-full.txt` manifest and serve any page as raw markdown.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
// app/llms.txt/route.ts
|
|
14
|
+
import { generateLlmsText } from "@vllnt/next-llms";
|
|
15
|
+
|
|
16
|
+
export function GET() {
|
|
17
|
+
return new Response(
|
|
18
|
+
generateLlmsText({
|
|
19
|
+
title: "Acme",
|
|
20
|
+
summary: "Acme is a widget API.",
|
|
21
|
+
sections: [
|
|
22
|
+
{
|
|
23
|
+
title: "Docs",
|
|
24
|
+
links: [{ title: "Quickstart", url: "/docs/quickstart.md" }],
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
}),
|
|
28
|
+
{ headers: { "Content-Type": "text/plain; charset=utf-8" } },
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Emit the `llms.txt` index and its `llms-full.txt` companion, and expose any
|
|
34
|
+
page's markdown at `/path.md` so agents skip HTML parsing. Zero runtime
|
|
35
|
+
dependencies — pure Web-standard APIs that run in any Next.js runtime (and
|
|
36
|
+
Remix, Hono, Deno, …).
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
- **Spec-compliant `llms.txt`** — `generateLlmsText` emits the llmstxt.org
|
|
41
|
+
shape: an `#` title, a `>` blockquote, `## Section` link lists, and a
|
|
42
|
+
droppable `## Optional` section.
|
|
43
|
+
- **`llms-full.txt` companion** — `generateLlmsFullText` inlines every page's
|
|
44
|
+
full markdown so an agent reads the whole site in one request.
|
|
45
|
+
- **Raw-markdown routes** — `createMarkdownRoute` serves any page as
|
|
46
|
+
`text/markdown`, stripping a trailing `.md` and resolving the slug through
|
|
47
|
+
your own content source.
|
|
48
|
+
- **Zero dependencies** — pure TypeScript over Web `Request` / `Response`;
|
|
49
|
+
nothing added to your bundle.
|
|
50
|
+
- **Runtime-agnostic** — the route handler imports nothing from `next`; it runs
|
|
51
|
+
on the Edge, Node, or any Web-standard runtime.
|
|
52
|
+
- **Typed end to end** — concrete config types guide correct usage; no `any`.
|
|
53
|
+
- **Cache-friendly** — sensible `Cache-Control` defaults, overridable per route.
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pnpm add @vllnt/next-llms
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
No peer dependencies.
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
// app/[...slug]/route.ts — serve any page as raw markdown
|
|
67
|
+
import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
68
|
+
|
|
69
|
+
export const GET = createMarkdownRoute(async (slug) => {
|
|
70
|
+
const doc = await getDoc(slug.join("/")); // your content source
|
|
71
|
+
return doc?.markdown ?? null; // null → 404
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`/docs/intro.md` resolves the slug `["docs", "intro"]` and returns the markdown
|
|
76
|
+
with `Content-Type: text/markdown`, or `404` when the resolver returns `null`.
|
|
77
|
+
|
|
78
|
+
## API Reference
|
|
79
|
+
|
|
80
|
+
| Export | Kind | Result |
|
|
81
|
+
| ----------------------------------------- | -------- | ----------------------------------------- |
|
|
82
|
+
| `generateLlmsText(config)` | function | `string` — the `llms.txt` index |
|
|
83
|
+
| `generateLlmsFullText(config)` | function | `string` — the `llms-full.txt` bundle |
|
|
84
|
+
| `createMarkdownRoute(resolver, options?)` | factory | `(request: Request) => Promise<Response>` |
|
|
85
|
+
|
|
86
|
+
Full reference: [docs/API.md](docs/API.md).
|
|
87
|
+
|
|
88
|
+
## Security
|
|
89
|
+
|
|
90
|
+
- No secrets touched — the package generates text and serves whatever your
|
|
91
|
+
resolver returns.
|
|
92
|
+
- Your resolver is the boundary — validate and authorize the slug before
|
|
93
|
+
returning content.
|
|
94
|
+
- Server-controlled cache headers — defaults are safe and overridable per route.
|
|
95
|
+
|
|
96
|
+
See [docs/API.md](docs/API.md).
|
|
97
|
+
|
|
98
|
+
## Testing
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pnpm test # single run
|
|
102
|
+
pnpm test:coverage # enforced 100% on covered files
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Contributing
|
|
106
|
+
|
|
107
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
108
|
+
|
|
109
|
+
## Author
|
|
110
|
+
|
|
111
|
+
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
112
|
+
[bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
113
|
+
|
|
114
|
+
Part of the [@vllnt](https://github.com/vllnt) open-source fleet —
|
|
115
|
+
[vllnt.com](https://vllnt.com)
|
|
116
|
+
|
|
117
|
+
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
MIT — see [LICENSE](LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { generateLlmsFullText } from "./llms-full.js";
|
|
2
|
+
export { generateLlmsText } from "./llms-text.js";
|
|
3
|
+
export { createMarkdownRoute } from "./markdown-route.js";
|
|
4
|
+
export type { LlmsConfig, LlmsFullConfig, LlmsFullPage, LlmsFullSection, LlmsLink, LlmsSection, MarkdownResolver, MarkdownRouteOptions, } from "./types.js";
|
|
5
|
+
//# 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,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,YAAY,EACV,UAAU,EACV,cAAc,EACd,YAAY,EACZ,eAAe,EACf,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LlmsFullConfig } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generate an `llms-full.txt` manifest — the same header as `llms.txt` followed
|
|
4
|
+
* by every page's full markdown content inline, so an agent can read the whole
|
|
5
|
+
* site in a single request.
|
|
6
|
+
*
|
|
7
|
+
* Hierarchy: `# {title}` (site) → `## {section}` → `### {page}` + the page body.
|
|
8
|
+
*
|
|
9
|
+
* @param config - The manifest content with full page bodies.
|
|
10
|
+
* @returns The `llms-full.txt` file contents (no trailing newline).
|
|
11
|
+
* @see https://llmstxt.org
|
|
12
|
+
*/
|
|
13
|
+
export declare function generateLlmsFullText(config: LlmsFullConfig): string;
|
|
14
|
+
//# sourceMappingURL=llms-full.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llms-full.d.ts","sourceRoot":"","sources":["../src/llms-full.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAiC,MAAM,YAAY,CAAC;AAehF;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,CAOnE"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function renderPage(page) {
|
|
2
|
+
const head = page.url
|
|
3
|
+
? `### ${page.title}\n\nSource: ${page.url}`
|
|
4
|
+
: `### ${page.title}`;
|
|
5
|
+
return `${head}\n\n${page.content}`;
|
|
6
|
+
}
|
|
7
|
+
function renderFullSection(section) {
|
|
8
|
+
const heading = `## ${section.title}`;
|
|
9
|
+
const pages = section.pages.map(renderPage).join("\n\n");
|
|
10
|
+
return pages ? `${heading}\n\n${pages}` : heading;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Generate an `llms-full.txt` manifest — the same header as `llms.txt` followed
|
|
14
|
+
* by every page's full markdown content inline, so an agent can read the whole
|
|
15
|
+
* site in a single request.
|
|
16
|
+
*
|
|
17
|
+
* Hierarchy: `# {title}` (site) → `## {section}` → `### {page}` + the page body.
|
|
18
|
+
*
|
|
19
|
+
* @param config - The manifest content with full page bodies.
|
|
20
|
+
* @returns The `llms-full.txt` file contents (no trailing newline).
|
|
21
|
+
* @see https://llmstxt.org
|
|
22
|
+
*/
|
|
23
|
+
export function generateLlmsFullText(config) {
|
|
24
|
+
return [
|
|
25
|
+
`# ${config.title}`,
|
|
26
|
+
...(config.summary ? [`> ${config.summary}`] : []),
|
|
27
|
+
...(config.details ? [config.details] : []),
|
|
28
|
+
...config.sections.map(renderFullSection),
|
|
29
|
+
].join("\n\n");
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=llms-full.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llms-full.js","sourceRoot":"","sources":["../src/llms-full.ts"],"names":[],"mappings":"AAEA,SAAS,UAAU,CAAC,IAAkB;IACpC,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG;QACnB,CAAC,CAAC,OAAO,IAAI,CAAC,KAAK,eAAe,IAAI,CAAC,GAAG,EAAE;QAC5C,CAAC,CAAC,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACxB,OAAO,GAAG,IAAI,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;AACtC,CAAC;AAED,SAAS,iBAAiB,CAAC,OAAwB;IACjD,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IACtC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACzD,OAAO,KAAK,CAAC,CAAC,CAAC,GAAG,OAAO,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AACpD,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAsB;IACzD,OAAO;QACL,KAAK,MAAM,CAAC,KAAK,EAAE;QACnB,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAClD,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3C,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,iBAAiB,CAAC;KAC1C,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { LlmsConfig } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generate an `llms.txt` index manifest from a config, following the llms.txt
|
|
4
|
+
* specification: an `# {title}` H1, a `> {summary}` blockquote, optional free
|
|
5
|
+
* prose, `## {section}` link lists, and a droppable `## Optional` section.
|
|
6
|
+
*
|
|
7
|
+
* @param config - The manifest content.
|
|
8
|
+
* @returns The `llms.txt` file contents (no trailing newline).
|
|
9
|
+
* @see https://llmstxt.org
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* generateLlmsText({
|
|
14
|
+
* title: "Acme Docs",
|
|
15
|
+
* summary: "Everything an agent needs to use Acme.",
|
|
16
|
+
* sections: [
|
|
17
|
+
* { title: "Docs", links: [{ title: "Quickstart", url: "/docs/quickstart.md" }] },
|
|
18
|
+
* ],
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function generateLlmsText(config: LlmsConfig): string;
|
|
23
|
+
//# sourceMappingURL=llms-text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llms-text.d.ts","sourceRoot":"","sources":["../src/llms-text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAyB,MAAM,YAAY,CAAC;AAcpE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAU3D"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
function renderLink(link) {
|
|
2
|
+
return link.notes
|
|
3
|
+
? `- [${link.title}](${link.url}): ${link.notes}`
|
|
4
|
+
: `- [${link.title}](${link.url})`;
|
|
5
|
+
}
|
|
6
|
+
function renderSection(section) {
|
|
7
|
+
const heading = `## ${section.title}`;
|
|
8
|
+
const links = section.links.map(renderLink).join("\n");
|
|
9
|
+
return links ? `${heading}\n\n${links}` : heading;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Generate an `llms.txt` index manifest from a config, following the llms.txt
|
|
13
|
+
* specification: an `# {title}` H1, a `> {summary}` blockquote, optional free
|
|
14
|
+
* prose, `## {section}` link lists, and a droppable `## Optional` section.
|
|
15
|
+
*
|
|
16
|
+
* @param config - The manifest content.
|
|
17
|
+
* @returns The `llms.txt` file contents (no trailing newline).
|
|
18
|
+
* @see https://llmstxt.org
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* generateLlmsText({
|
|
23
|
+
* title: "Acme Docs",
|
|
24
|
+
* summary: "Everything an agent needs to use Acme.",
|
|
25
|
+
* sections: [
|
|
26
|
+
* { title: "Docs", links: [{ title: "Quickstart", url: "/docs/quickstart.md" }] },
|
|
27
|
+
* ],
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function generateLlmsText(config) {
|
|
32
|
+
return [
|
|
33
|
+
`# ${config.title}`,
|
|
34
|
+
...(config.summary ? [`> ${config.summary}`] : []),
|
|
35
|
+
...(config.details ? [config.details] : []),
|
|
36
|
+
...(config.sections ?? []).map(renderSection),
|
|
37
|
+
...(config.optional && config.optional.length > 0
|
|
38
|
+
? [renderSection({ links: config.optional, title: "Optional" })]
|
|
39
|
+
: []),
|
|
40
|
+
].join("\n\n");
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=llms-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llms-text.js","sourceRoot":"","sources":["../src/llms-text.ts"],"names":[],"mappings":"AAEA,SAAS,UAAU,CAAC,IAAc;IAChC,OAAO,IAAI,CAAC,KAAK;QACf,CAAC,CAAC,MAAM,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC,KAAK,EAAE;QACjD,CAAC,CAAC,MAAM,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC;AACvC,CAAC;AAED,SAAS,aAAa,CAAC,OAAoB;IACzC,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IACtC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvD,OAAO,KAAK,CAAC,CAAC,CAAC,GAAG,OAAO,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AACpD,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAkB;IACjD,OAAO;QACL,KAAK,MAAM,CAAC,KAAK,EAAE;QACnB,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAClD,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3C,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC;QAC7C,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC;YAC/C,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC,CAAC;YAChE,CAAC,CAAC,EAAE,CAAC;KACR,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { MarkdownResolver, MarkdownRouteOptions } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create a Next.js App Router route handler that serves a slug's markdown
|
|
4
|
+
* content. Mount it at a catch-all route (`app/[...slug]/route.ts`). The handler
|
|
5
|
+
* is a pure Web-standard `(request) => Promise<Response>` — it imports nothing
|
|
6
|
+
* from `next` and runs in any Web runtime.
|
|
7
|
+
*
|
|
8
|
+
* - Strips a trailing `.md` from the path (`/docs/intro.md` resolves the slug
|
|
9
|
+
* `["docs", "intro"]`).
|
|
10
|
+
* - Returns `200` with `Content-Type: text/markdown; charset=utf-8` and a
|
|
11
|
+
* `Cache-Control` header when the resolver returns a string.
|
|
12
|
+
* - Returns `404` (plain text) when the resolver returns `null`.
|
|
13
|
+
*
|
|
14
|
+
* @param resolver - Maps a slug to its markdown, or `null` when not found.
|
|
15
|
+
* @param options - Optional cache-control override.
|
|
16
|
+
* @returns A route handler suitable for `export const GET = ...`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* // app/[...slug]/route.ts
|
|
21
|
+
* import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
22
|
+
*
|
|
23
|
+
* export const GET = createMarkdownRoute(async (slug) =>
|
|
24
|
+
* getDoc(slug.join("/")),
|
|
25
|
+
* );
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function createMarkdownRoute(resolver: MarkdownResolver, options?: MarkdownRouteOptions): (request: Request) => Promise<Response>;
|
|
29
|
+
//# sourceMappingURL=markdown-route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown-route.d.ts","sourceRoot":"","sources":["../src/markdown-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAqBzE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,GAAE,oBAAyB,GACjC,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAoBzC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const DEFAULT_CACHE_CONTROL = "public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800";
|
|
2
|
+
/**
|
|
3
|
+
* Decode a URL pathname into slug segments, stripping a trailing `.md` from the
|
|
4
|
+
* last segment. `/docs/intro.md` → `["docs", "intro"]`; `/` → `[]`.
|
|
5
|
+
*/
|
|
6
|
+
function pathnameToSlug(pathname) {
|
|
7
|
+
const segments = pathname
|
|
8
|
+
.split("/")
|
|
9
|
+
.filter((segment) => segment.length > 0)
|
|
10
|
+
.map((segment) => decodeURIComponent(segment));
|
|
11
|
+
const last = segments.at(-1);
|
|
12
|
+
if (last?.endsWith(".md")) {
|
|
13
|
+
segments[segments.length - 1] = last.slice(0, -3);
|
|
14
|
+
}
|
|
15
|
+
return segments;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a Next.js App Router route handler that serves a slug's markdown
|
|
19
|
+
* content. Mount it at a catch-all route (`app/[...slug]/route.ts`). The handler
|
|
20
|
+
* is a pure Web-standard `(request) => Promise<Response>` — it imports nothing
|
|
21
|
+
* from `next` and runs in any Web runtime.
|
|
22
|
+
*
|
|
23
|
+
* - Strips a trailing `.md` from the path (`/docs/intro.md` resolves the slug
|
|
24
|
+
* `["docs", "intro"]`).
|
|
25
|
+
* - Returns `200` with `Content-Type: text/markdown; charset=utf-8` and a
|
|
26
|
+
* `Cache-Control` header when the resolver returns a string.
|
|
27
|
+
* - Returns `404` (plain text) when the resolver returns `null`.
|
|
28
|
+
*
|
|
29
|
+
* @param resolver - Maps a slug to its markdown, or `null` when not found.
|
|
30
|
+
* @param options - Optional cache-control override.
|
|
31
|
+
* @returns A route handler suitable for `export const GET = ...`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* // app/[...slug]/route.ts
|
|
36
|
+
* import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
37
|
+
*
|
|
38
|
+
* export const GET = createMarkdownRoute(async (slug) =>
|
|
39
|
+
* getDoc(slug.join("/")),
|
|
40
|
+
* );
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function createMarkdownRoute(resolver, options = {}) {
|
|
44
|
+
const cacheControl = options.cacheControl ?? DEFAULT_CACHE_CONTROL;
|
|
45
|
+
return async (request) => {
|
|
46
|
+
const slug = pathnameToSlug(new URL(request.url).pathname);
|
|
47
|
+
const content = await resolver(slug);
|
|
48
|
+
return content === null
|
|
49
|
+
? new Response("Not Found", {
|
|
50
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
|
51
|
+
status: 404,
|
|
52
|
+
})
|
|
53
|
+
: new Response(content, {
|
|
54
|
+
headers: {
|
|
55
|
+
"Cache-Control": cacheControl,
|
|
56
|
+
"Content-Type": "text/markdown; charset=utf-8",
|
|
57
|
+
},
|
|
58
|
+
status: 200,
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=markdown-route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown-route.js","sourceRoot":"","sources":["../src/markdown-route.ts"],"names":[],"mappings":"AAEA,MAAM,qBAAqB,GACzB,qEAAqE,CAAC;AAExE;;;GAGG;AACH,SAAS,cAAc,CAAC,QAAgB;IACtC,MAAM,QAAQ,GAAG,QAAQ;SACtB,KAAK,CAAC,GAAG,CAAC;SACV,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;SACvC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;IACjD,MAAM,IAAI,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7B,IAAI,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,mBAAmB,CACjC,QAA0B,EAC1B,UAAgC,EAAE;IAElC,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,qBAAqB,CAAC;IAEnE,OAAO,KAAK,EAAE,OAAgB,EAAqB,EAAE;QACnD,MAAM,IAAI,GAAG,cAAc,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;QAC3D,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAC;QAErC,OAAO,OAAO,KAAK,IAAI;YACrB,CAAC,CAAC,IAAI,QAAQ,CAAC,WAAW,EAAE;gBACxB,OAAO,EAAE,EAAE,cAAc,EAAE,2BAA2B,EAAE;gBACxD,MAAM,EAAE,GAAG;aACZ,CAAC;YACJ,CAAC,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE;gBACpB,OAAO,EAAE;oBACP,eAAe,EAAE,YAAY;oBAC7B,cAAc,EAAE,8BAA8B;iBAC/C;gBACD,MAAM,EAAE,GAAG;aACZ,CAAC,CAAC;IACT,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single link entry in an `llms.txt` section — rendered as
|
|
3
|
+
* `- [title](url): notes`.
|
|
4
|
+
*/
|
|
5
|
+
export type LlmsLink = {
|
|
6
|
+
/** Human-readable link text. */
|
|
7
|
+
title: string;
|
|
8
|
+
/** Absolute or root-relative URL. */
|
|
9
|
+
url: string;
|
|
10
|
+
/** Optional one-line description shown after the link. */
|
|
11
|
+
notes?: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A named H2 section of an `llms.txt` manifest (e.g. `## Docs`) and its links.
|
|
15
|
+
*/
|
|
16
|
+
export type LlmsSection = {
|
|
17
|
+
/** Section heading, rendered as `## {title}`. */
|
|
18
|
+
title: string;
|
|
19
|
+
/** Links listed under the section. */
|
|
20
|
+
links: LlmsLink[];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Input for {@link generateLlmsText} — the `llms.txt` index manifest.
|
|
24
|
+
*
|
|
25
|
+
* @see https://llmstxt.org
|
|
26
|
+
*/
|
|
27
|
+
export type LlmsConfig = {
|
|
28
|
+
/** Site/project name, rendered as the `# {title}` H1. */
|
|
29
|
+
title: string;
|
|
30
|
+
/** One-line summary, rendered as a `> {summary}` blockquote. */
|
|
31
|
+
summary?: string;
|
|
32
|
+
/** Free-form markdown shown after the blockquote, before the sections. */
|
|
33
|
+
details?: string;
|
|
34
|
+
/** Named link-list sections (`## Docs`, `## Examples`, …). */
|
|
35
|
+
sections?: LlmsSection[];
|
|
36
|
+
/**
|
|
37
|
+
* Links placed under the special `## Optional` section. Agents may skip this
|
|
38
|
+
* section when a shorter context helps, per the llms.txt spec.
|
|
39
|
+
*/
|
|
40
|
+
optional?: LlmsLink[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* A page whose full markdown content goes inline into `llms-full.txt`.
|
|
44
|
+
*/
|
|
45
|
+
export type LlmsFullPage = {
|
|
46
|
+
/** Page title, rendered as a `### {title}` heading. */
|
|
47
|
+
title: string;
|
|
48
|
+
/** Optional source URL, rendered as a `Source: {url}` line under the title. */
|
|
49
|
+
url?: string;
|
|
50
|
+
/** The page's full markdown content. */
|
|
51
|
+
content: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* A named group of pages in `llms-full.txt`, rendered as a `## {title}` section.
|
|
55
|
+
*/
|
|
56
|
+
export type LlmsFullSection = {
|
|
57
|
+
/** Section heading, rendered as `## {title}`. */
|
|
58
|
+
title: string;
|
|
59
|
+
/** Pages whose content goes inline under the section. */
|
|
60
|
+
pages: LlmsFullPage[];
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Input for {@link generateLlmsFullText} — the expanded `llms-full.txt` manifest.
|
|
64
|
+
*
|
|
65
|
+
* @see https://llmstxt.org
|
|
66
|
+
*/
|
|
67
|
+
export type LlmsFullConfig = {
|
|
68
|
+
/** Site/project name, rendered as the `# {title}` H1. */
|
|
69
|
+
title: string;
|
|
70
|
+
/** One-line summary, rendered as a `> {summary}` blockquote. */
|
|
71
|
+
summary?: string;
|
|
72
|
+
/** Free-form markdown shown after the blockquote. */
|
|
73
|
+
details?: string;
|
|
74
|
+
/** Sections whose pages go inline in full. */
|
|
75
|
+
sections: LlmsFullSection[];
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Maps a route slug to its markdown content, or `null` if there is no page for
|
|
79
|
+
* the slug. The slug is the path segments with any trailing `.md` stripped.
|
|
80
|
+
*/
|
|
81
|
+
export type MarkdownResolver = (slug: string[]) => string | null | Promise<string | null>;
|
|
82
|
+
/**
|
|
83
|
+
* Options for {@link createMarkdownRoute}.
|
|
84
|
+
*/
|
|
85
|
+
export type MarkdownRouteOptions = {
|
|
86
|
+
/**
|
|
87
|
+
* `Cache-Control` header value for resolved (200) responses.
|
|
88
|
+
*
|
|
89
|
+
* @defaultValue `"public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800"`
|
|
90
|
+
*/
|
|
91
|
+
cacheControl?: string;
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG;IACrB,gCAAgC;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,qCAAqC;IACrC,GAAG,EAAE,MAAM,CAAC;IACZ,0DAA0D;IAC1D,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,iDAAiD;IACjD,KAAK,EAAE,MAAM,CAAC;IACd,sCAAsC;IACtC,KAAK,EAAE,QAAQ,EAAE,CAAC;CACnB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAC;IACd,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0EAA0E;IAC1E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC;IACzB;;;OAGG;IACH,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;CACvB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAC;IACd,+EAA+E;IAC/E,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,wCAAwC;IACxC,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,iDAAiD;IACjD,KAAK,EAAE,MAAM,CAAC;IACd,yDAAyD;IACzD,KAAK,EAAE,YAAY,EAAE,CAAC;CACvB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAC;IACd,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,qDAAqD;IACrD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,8CAA8C;IAC9C,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAC7B,IAAI,EAAE,MAAM,EAAE,KACX,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;AAE5C;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vllnt/next-llms",
|
|
3
|
+
"version": "0.1.0-canary.78c9be3",
|
|
4
|
+
"description": "AI discoverability for Next.js sites — generate llms.txt / llms-full.txt manifests and serve any page as raw markdown",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@9.15.4",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./package.json": "./package.json",
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"module": "./dist/index.js",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc --project ./tsconfig.build.json",
|
|
24
|
+
"typecheck": "tsc --noEmit",
|
|
25
|
+
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"format": "prettier --write .",
|
|
28
|
+
"format:check": "prettier --check .",
|
|
29
|
+
"test": "vitest run --passWithNoTests",
|
|
30
|
+
"test:watch": "vitest --typecheck --clearScreen false",
|
|
31
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
32
|
+
"generate:llms": "node scripts/generate-llms.mjs",
|
|
33
|
+
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^20.0.0",
|
|
38
|
+
"@vitest/coverage-v8": "^4.1.8",
|
|
39
|
+
"@vllnt/eslint-config": "^1.0.0",
|
|
40
|
+
"@vllnt/typescript": "^1.0.0",
|
|
41
|
+
"eslint": "^9.0.0",
|
|
42
|
+
"prettier": "^3.4.0",
|
|
43
|
+
"typescript": "^5.7.0",
|
|
44
|
+
"vitest": "^4.1.8"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/vllnt/next-llms#readme",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/vllnt/next-llms/issues"
|
|
49
|
+
},
|
|
50
|
+
"author": {
|
|
51
|
+
"name": "bntvllnt",
|
|
52
|
+
"url": "https://bntvllnt.com"
|
|
53
|
+
},
|
|
54
|
+
"funding": {
|
|
55
|
+
"type": "github",
|
|
56
|
+
"url": "https://github.com/sponsors/bntvllnt"
|
|
57
|
+
},
|
|
58
|
+
"repository": {
|
|
59
|
+
"type": "git",
|
|
60
|
+
"url": "https://github.com/vllnt/next-llms"
|
|
61
|
+
},
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=18"
|
|
64
|
+
},
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public"
|
|
67
|
+
},
|
|
68
|
+
"sideEffects": false,
|
|
69
|
+
"keywords": [
|
|
70
|
+
"nextjs",
|
|
71
|
+
"next",
|
|
72
|
+
"llms-txt",
|
|
73
|
+
"llms.txt",
|
|
74
|
+
"ai",
|
|
75
|
+
"llm",
|
|
76
|
+
"seo",
|
|
77
|
+
"app-router",
|
|
78
|
+
"markdown",
|
|
79
|
+
"discoverability"
|
|
80
|
+
]
|
|
81
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createMarkdownRoute,
|
|
5
|
+
generateLlmsFullText,
|
|
6
|
+
generateLlmsText,
|
|
7
|
+
} from "./index.js";
|
|
8
|
+
|
|
9
|
+
describe("generateLlmsText", () => {
|
|
10
|
+
it("renders a full manifest with summary, details, sections, and Optional", () => {
|
|
11
|
+
const out = generateLlmsText({
|
|
12
|
+
title: "Acme Docs",
|
|
13
|
+
summary: "Everything an agent needs to use Acme.",
|
|
14
|
+
details: "Acme is a widget API.",
|
|
15
|
+
sections: [
|
|
16
|
+
{
|
|
17
|
+
title: "Docs",
|
|
18
|
+
links: [
|
|
19
|
+
{
|
|
20
|
+
title: "Quickstart",
|
|
21
|
+
url: "/docs/quickstart.md",
|
|
22
|
+
notes: "Start here",
|
|
23
|
+
},
|
|
24
|
+
{ title: "Reference", url: "/docs/reference.md" },
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
optional: [{ title: "Changelog", url: "/changelog.md" }],
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
expect(out).toBe(
|
|
32
|
+
[
|
|
33
|
+
"# Acme Docs",
|
|
34
|
+
"> Everything an agent needs to use Acme.",
|
|
35
|
+
"Acme is a widget API.",
|
|
36
|
+
"## Docs\n\n- [Quickstart](/docs/quickstart.md): Start here\n- [Reference](/docs/reference.md)",
|
|
37
|
+
"## Optional\n\n- [Changelog](/changelog.md)",
|
|
38
|
+
].join("\n\n"),
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("renders a minimal manifest with only a title", () => {
|
|
43
|
+
expect(generateLlmsText({ title: "Bare" })).toBe("# Bare");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("renders a section heading with no links when the link list is empty", () => {
|
|
47
|
+
expect(
|
|
48
|
+
generateLlmsText({
|
|
49
|
+
title: "T",
|
|
50
|
+
sections: [{ title: "Empty", links: [] }],
|
|
51
|
+
}),
|
|
52
|
+
).toBe("# T\n\n## Empty");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("omits the Optional section when optional is an empty array", () => {
|
|
56
|
+
expect(generateLlmsText({ title: "T", optional: [] })).toBe("# T");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("generateLlmsFullText", () => {
|
|
61
|
+
it("inlines page content under sections, with and without source URLs", () => {
|
|
62
|
+
const out = generateLlmsFullText({
|
|
63
|
+
title: "Acme",
|
|
64
|
+
summary: "Full text.",
|
|
65
|
+
details: "Intro.",
|
|
66
|
+
sections: [
|
|
67
|
+
{
|
|
68
|
+
title: "Docs",
|
|
69
|
+
pages: [
|
|
70
|
+
{
|
|
71
|
+
title: "Quickstart",
|
|
72
|
+
url: "/docs/quickstart",
|
|
73
|
+
content: "# QS\n\nDo this.",
|
|
74
|
+
},
|
|
75
|
+
{ title: "Reference", content: "# Ref\n\nDetails." },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
expect(out).toBe(
|
|
82
|
+
[
|
|
83
|
+
"# Acme",
|
|
84
|
+
"> Full text.",
|
|
85
|
+
"Intro.",
|
|
86
|
+
"## Docs\n\n### Quickstart\n\nSource: /docs/quickstart\n\n# QS\n\nDo this.\n\n### Reference\n\n# Ref\n\nDetails.",
|
|
87
|
+
].join("\n\n"),
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("renders a minimal full manifest (title + empty sections)", () => {
|
|
92
|
+
expect(generateLlmsFullText({ title: "Bare", sections: [] })).toBe(
|
|
93
|
+
"# Bare",
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("renders a section heading with no pages when the page list is empty", () => {
|
|
98
|
+
expect(
|
|
99
|
+
generateLlmsFullText({
|
|
100
|
+
title: "T",
|
|
101
|
+
sections: [{ title: "Empty", pages: [] }],
|
|
102
|
+
}),
|
|
103
|
+
).toBe("# T\n\n## Empty");
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("createMarkdownRoute", () => {
|
|
108
|
+
it("returns 200 markdown with default cache headers when the resolver resolves", async () => {
|
|
109
|
+
const handler = createMarkdownRoute(() => Promise.resolve("# Hello"));
|
|
110
|
+
const response = await handler(
|
|
111
|
+
new Request("https://site.test/docs/intro.md"),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
expect(response.status).toBe(200);
|
|
115
|
+
expect(response.headers.get("Content-Type")).toBe(
|
|
116
|
+
"text/markdown; charset=utf-8",
|
|
117
|
+
);
|
|
118
|
+
expect(response.headers.get("Cache-Control")).toBe(
|
|
119
|
+
"public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800",
|
|
120
|
+
);
|
|
121
|
+
expect(await response.text()).toBe("# Hello");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("returns 404 plain text when the resolver returns null", async () => {
|
|
125
|
+
const handler = createMarkdownRoute(() => null);
|
|
126
|
+
const response = await handler(new Request("https://site.test/missing.md"));
|
|
127
|
+
|
|
128
|
+
expect(response.status).toBe(404);
|
|
129
|
+
expect(response.headers.get("Content-Type")).toBe(
|
|
130
|
+
"text/plain; charset=utf-8",
|
|
131
|
+
);
|
|
132
|
+
expect(await response.text()).toBe("Not Found");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("honors a custom cacheControl option", async () => {
|
|
136
|
+
const handler = createMarkdownRoute(() => "x", {
|
|
137
|
+
cacheControl: "no-store",
|
|
138
|
+
});
|
|
139
|
+
const response = await handler(new Request("https://site.test/a.md"));
|
|
140
|
+
|
|
141
|
+
expect(response.headers.get("Cache-Control")).toBe("no-store");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("strips a trailing .md and passes the decoded slug to the resolver", async () => {
|
|
145
|
+
let received: string[] = [];
|
|
146
|
+
const handler = createMarkdownRoute((slug) => {
|
|
147
|
+
received = slug;
|
|
148
|
+
return "ok";
|
|
149
|
+
});
|
|
150
|
+
await handler(new Request("https://site.test/docs/getting%20started.md"));
|
|
151
|
+
|
|
152
|
+
expect(received).toEqual(["docs", "getting started"]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("passes a slug without a .md suffix unchanged", async () => {
|
|
156
|
+
let received: string[] = [];
|
|
157
|
+
const handler = createMarkdownRoute((slug) => {
|
|
158
|
+
received = slug;
|
|
159
|
+
return "ok";
|
|
160
|
+
});
|
|
161
|
+
await handler(new Request("https://site.test/docs/intro"));
|
|
162
|
+
|
|
163
|
+
expect(received).toEqual(["docs", "intro"]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("resolves an empty slug for the root path", async () => {
|
|
167
|
+
let received: string[] = ["unset"];
|
|
168
|
+
const handler = createMarkdownRoute((slug) => {
|
|
169
|
+
received = slug;
|
|
170
|
+
return "root";
|
|
171
|
+
});
|
|
172
|
+
await handler(new Request("https://site.test/"));
|
|
173
|
+
|
|
174
|
+
expect(received).toEqual([]);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("treats an empty string as content (200), not a 404", async () => {
|
|
178
|
+
const handler = createMarkdownRoute(() => "");
|
|
179
|
+
const response = await handler(new Request("https://site.test/empty.md"));
|
|
180
|
+
|
|
181
|
+
expect(response.status).toBe(200);
|
|
182
|
+
expect(await response.text()).toBe("");
|
|
183
|
+
});
|
|
184
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { generateLlmsFullText } from "./llms-full.js";
|
|
2
|
+
export { generateLlmsText } from "./llms-text.js";
|
|
3
|
+
export { createMarkdownRoute } from "./markdown-route.js";
|
|
4
|
+
export type {
|
|
5
|
+
LlmsConfig,
|
|
6
|
+
LlmsFullConfig,
|
|
7
|
+
LlmsFullPage,
|
|
8
|
+
LlmsFullSection,
|
|
9
|
+
LlmsLink,
|
|
10
|
+
LlmsSection,
|
|
11
|
+
MarkdownResolver,
|
|
12
|
+
MarkdownRouteOptions,
|
|
13
|
+
} from "./types.js";
|
package/src/llms-full.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { LlmsFullConfig, LlmsFullPage, LlmsFullSection } from "./types.js";
|
|
2
|
+
|
|
3
|
+
function renderPage(page: LlmsFullPage): string {
|
|
4
|
+
const head = page.url
|
|
5
|
+
? `### ${page.title}\n\nSource: ${page.url}`
|
|
6
|
+
: `### ${page.title}`;
|
|
7
|
+
return `${head}\n\n${page.content}`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function renderFullSection(section: LlmsFullSection): string {
|
|
11
|
+
const heading = `## ${section.title}`;
|
|
12
|
+
const pages = section.pages.map(renderPage).join("\n\n");
|
|
13
|
+
return pages ? `${heading}\n\n${pages}` : heading;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Generate an `llms-full.txt` manifest — the same header as `llms.txt` followed
|
|
18
|
+
* by every page's full markdown content inline, so an agent can read the whole
|
|
19
|
+
* site in a single request.
|
|
20
|
+
*
|
|
21
|
+
* Hierarchy: `# {title}` (site) → `## {section}` → `### {page}` + the page body.
|
|
22
|
+
*
|
|
23
|
+
* @param config - The manifest content with full page bodies.
|
|
24
|
+
* @returns The `llms-full.txt` file contents (no trailing newline).
|
|
25
|
+
* @see https://llmstxt.org
|
|
26
|
+
*/
|
|
27
|
+
export function generateLlmsFullText(config: LlmsFullConfig): string {
|
|
28
|
+
return [
|
|
29
|
+
`# ${config.title}`,
|
|
30
|
+
...(config.summary ? [`> ${config.summary}`] : []),
|
|
31
|
+
...(config.details ? [config.details] : []),
|
|
32
|
+
...config.sections.map(renderFullSection),
|
|
33
|
+
].join("\n\n");
|
|
34
|
+
}
|
package/src/llms-text.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { LlmsConfig, LlmsLink, LlmsSection } from "./types.js";
|
|
2
|
+
|
|
3
|
+
function renderLink(link: LlmsLink): string {
|
|
4
|
+
return link.notes
|
|
5
|
+
? `- [${link.title}](${link.url}): ${link.notes}`
|
|
6
|
+
: `- [${link.title}](${link.url})`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function renderSection(section: LlmsSection): string {
|
|
10
|
+
const heading = `## ${section.title}`;
|
|
11
|
+
const links = section.links.map(renderLink).join("\n");
|
|
12
|
+
return links ? `${heading}\n\n${links}` : heading;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generate an `llms.txt` index manifest from a config, following the llms.txt
|
|
17
|
+
* specification: an `# {title}` H1, a `> {summary}` blockquote, optional free
|
|
18
|
+
* prose, `## {section}` link lists, and a droppable `## Optional` section.
|
|
19
|
+
*
|
|
20
|
+
* @param config - The manifest content.
|
|
21
|
+
* @returns The `llms.txt` file contents (no trailing newline).
|
|
22
|
+
* @see https://llmstxt.org
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* generateLlmsText({
|
|
27
|
+
* title: "Acme Docs",
|
|
28
|
+
* summary: "Everything an agent needs to use Acme.",
|
|
29
|
+
* sections: [
|
|
30
|
+
* { title: "Docs", links: [{ title: "Quickstart", url: "/docs/quickstart.md" }] },
|
|
31
|
+
* ],
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export function generateLlmsText(config: LlmsConfig): string {
|
|
36
|
+
return [
|
|
37
|
+
`# ${config.title}`,
|
|
38
|
+
...(config.summary ? [`> ${config.summary}`] : []),
|
|
39
|
+
...(config.details ? [config.details] : []),
|
|
40
|
+
...(config.sections ?? []).map(renderSection),
|
|
41
|
+
...(config.optional && config.optional.length > 0
|
|
42
|
+
? [renderSection({ links: config.optional, title: "Optional" })]
|
|
43
|
+
: []),
|
|
44
|
+
].join("\n\n");
|
|
45
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { MarkdownResolver, MarkdownRouteOptions } from "./types.js";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_CACHE_CONTROL =
|
|
4
|
+
"public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Decode a URL pathname into slug segments, stripping a trailing `.md` from the
|
|
8
|
+
* last segment. `/docs/intro.md` → `["docs", "intro"]`; `/` → `[]`.
|
|
9
|
+
*/
|
|
10
|
+
function pathnameToSlug(pathname: string): string[] {
|
|
11
|
+
const segments = pathname
|
|
12
|
+
.split("/")
|
|
13
|
+
.filter((segment) => segment.length > 0)
|
|
14
|
+
.map((segment) => decodeURIComponent(segment));
|
|
15
|
+
const last = segments.at(-1);
|
|
16
|
+
if (last?.endsWith(".md")) {
|
|
17
|
+
segments[segments.length - 1] = last.slice(0, -3);
|
|
18
|
+
}
|
|
19
|
+
return segments;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Create a Next.js App Router route handler that serves a slug's markdown
|
|
24
|
+
* content. Mount it at a catch-all route (`app/[...slug]/route.ts`). The handler
|
|
25
|
+
* is a pure Web-standard `(request) => Promise<Response>` — it imports nothing
|
|
26
|
+
* from `next` and runs in any Web runtime.
|
|
27
|
+
*
|
|
28
|
+
* - Strips a trailing `.md` from the path (`/docs/intro.md` resolves the slug
|
|
29
|
+
* `["docs", "intro"]`).
|
|
30
|
+
* - Returns `200` with `Content-Type: text/markdown; charset=utf-8` and a
|
|
31
|
+
* `Cache-Control` header when the resolver returns a string.
|
|
32
|
+
* - Returns `404` (plain text) when the resolver returns `null`.
|
|
33
|
+
*
|
|
34
|
+
* @param resolver - Maps a slug to its markdown, or `null` when not found.
|
|
35
|
+
* @param options - Optional cache-control override.
|
|
36
|
+
* @returns A route handler suitable for `export const GET = ...`.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* // app/[...slug]/route.ts
|
|
41
|
+
* import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
42
|
+
*
|
|
43
|
+
* export const GET = createMarkdownRoute(async (slug) =>
|
|
44
|
+
* getDoc(slug.join("/")),
|
|
45
|
+
* );
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function createMarkdownRoute(
|
|
49
|
+
resolver: MarkdownResolver,
|
|
50
|
+
options: MarkdownRouteOptions = {},
|
|
51
|
+
): (request: Request) => Promise<Response> {
|
|
52
|
+
const cacheControl = options.cacheControl ?? DEFAULT_CACHE_CONTROL;
|
|
53
|
+
|
|
54
|
+
return async (request: Request): Promise<Response> => {
|
|
55
|
+
const slug = pathnameToSlug(new URL(request.url).pathname);
|
|
56
|
+
const content = await resolver(slug);
|
|
57
|
+
|
|
58
|
+
return content === null
|
|
59
|
+
? new Response("Not Found", {
|
|
60
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
|
61
|
+
status: 404,
|
|
62
|
+
})
|
|
63
|
+
: new Response(content, {
|
|
64
|
+
headers: {
|
|
65
|
+
"Cache-Control": cacheControl,
|
|
66
|
+
"Content-Type": "text/markdown; charset=utf-8",
|
|
67
|
+
},
|
|
68
|
+
status: 200,
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single link entry in an `llms.txt` section — rendered as
|
|
3
|
+
* `- [title](url): notes`.
|
|
4
|
+
*/
|
|
5
|
+
export type LlmsLink = {
|
|
6
|
+
/** Human-readable link text. */
|
|
7
|
+
title: string;
|
|
8
|
+
/** Absolute or root-relative URL. */
|
|
9
|
+
url: string;
|
|
10
|
+
/** Optional one-line description shown after the link. */
|
|
11
|
+
notes?: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A named H2 section of an `llms.txt` manifest (e.g. `## Docs`) and its links.
|
|
16
|
+
*/
|
|
17
|
+
export type LlmsSection = {
|
|
18
|
+
/** Section heading, rendered as `## {title}`. */
|
|
19
|
+
title: string;
|
|
20
|
+
/** Links listed under the section. */
|
|
21
|
+
links: LlmsLink[];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Input for {@link generateLlmsText} — the `llms.txt` index manifest.
|
|
26
|
+
*
|
|
27
|
+
* @see https://llmstxt.org
|
|
28
|
+
*/
|
|
29
|
+
export type LlmsConfig = {
|
|
30
|
+
/** Site/project name, rendered as the `# {title}` H1. */
|
|
31
|
+
title: string;
|
|
32
|
+
/** One-line summary, rendered as a `> {summary}` blockquote. */
|
|
33
|
+
summary?: string;
|
|
34
|
+
/** Free-form markdown shown after the blockquote, before the sections. */
|
|
35
|
+
details?: string;
|
|
36
|
+
/** Named link-list sections (`## Docs`, `## Examples`, …). */
|
|
37
|
+
sections?: LlmsSection[];
|
|
38
|
+
/**
|
|
39
|
+
* Links placed under the special `## Optional` section. Agents may skip this
|
|
40
|
+
* section when a shorter context helps, per the llms.txt spec.
|
|
41
|
+
*/
|
|
42
|
+
optional?: LlmsLink[];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A page whose full markdown content goes inline into `llms-full.txt`.
|
|
47
|
+
*/
|
|
48
|
+
export type LlmsFullPage = {
|
|
49
|
+
/** Page title, rendered as a `### {title}` heading. */
|
|
50
|
+
title: string;
|
|
51
|
+
/** Optional source URL, rendered as a `Source: {url}` line under the title. */
|
|
52
|
+
url?: string;
|
|
53
|
+
/** The page's full markdown content. */
|
|
54
|
+
content: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A named group of pages in `llms-full.txt`, rendered as a `## {title}` section.
|
|
59
|
+
*/
|
|
60
|
+
export type LlmsFullSection = {
|
|
61
|
+
/** Section heading, rendered as `## {title}`. */
|
|
62
|
+
title: string;
|
|
63
|
+
/** Pages whose content goes inline under the section. */
|
|
64
|
+
pages: LlmsFullPage[];
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Input for {@link generateLlmsFullText} — the expanded `llms-full.txt` manifest.
|
|
69
|
+
*
|
|
70
|
+
* @see https://llmstxt.org
|
|
71
|
+
*/
|
|
72
|
+
export type LlmsFullConfig = {
|
|
73
|
+
/** Site/project name, rendered as the `# {title}` H1. */
|
|
74
|
+
title: string;
|
|
75
|
+
/** One-line summary, rendered as a `> {summary}` blockquote. */
|
|
76
|
+
summary?: string;
|
|
77
|
+
/** Free-form markdown shown after the blockquote. */
|
|
78
|
+
details?: string;
|
|
79
|
+
/** Sections whose pages go inline in full. */
|
|
80
|
+
sections: LlmsFullSection[];
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Maps a route slug to its markdown content, or `null` if there is no page for
|
|
85
|
+
* the slug. The slug is the path segments with any trailing `.md` stripped.
|
|
86
|
+
*/
|
|
87
|
+
export type MarkdownResolver = (
|
|
88
|
+
slug: string[],
|
|
89
|
+
) => string | null | Promise<string | null>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Options for {@link createMarkdownRoute}.
|
|
93
|
+
*/
|
|
94
|
+
export type MarkdownRouteOptions = {
|
|
95
|
+
/**
|
|
96
|
+
* `Cache-Control` header value for resolved (200) responses.
|
|
97
|
+
*
|
|
98
|
+
* @defaultValue `"public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800"`
|
|
99
|
+
*/
|
|
100
|
+
cacheControl?: string;
|
|
101
|
+
};
|