@vllnt/next-llms 0.1.0 → 0.1.1-canary.349aa6b
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/CHANGELOG.md +67 -7
- package/README.md +206 -85
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/llms-text.d.ts +3 -1
- package/dist/llms-text.d.ts.map +1 -1
- package/dist/llms-text.js +3 -1
- package/dist/llms-text.js.map +1 -1
- package/dist/markdown-route.d.ts +13 -7
- package/dist/markdown-route.d.ts.map +1 -1
- package/dist/markdown-route.js +22 -12
- package/dist/markdown-route.js.map +1 -1
- package/dist/types.d.ts +3 -1
- package/dist/types.d.ts.map +1 -1
- package/docs/API.md +60 -29
- package/llms.txt +48 -21
- package/package.json +19 -31
- package/src/index.ts +7 -0
- package/src/llms-text.ts +3 -1
- package/src/markdown-route.ts +22 -12
- package/src/types.ts +3 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,66 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.1.1] - 2026-11-27
|
|
10
|
+
|
|
11
|
+
Documentation and tooling release. The published code, exports and types are
|
|
12
|
+
unchanged from 0.1.0; only TSDoc comments in `dist/` and `src/`, and the
|
|
13
|
+
matching source maps, differ.
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
|
|
17
|
+
- `llms.txt` links the files shipped in the package (`README.md`, `docs/API.md`,
|
|
18
|
+
`CHANGELOG.md`, `LICENSE`) by relative path, so they match the installed
|
|
19
|
+
version, adds a "For coding agents" note, and ends with an "Ecosystem" list:
|
|
20
|
+
the package page, the release feed, the vllnt oss index, package search, vllnt
|
|
21
|
+
and the author. The README gains matching "For coding agents" and "Related
|
|
22
|
+
packages" sections and adds a "Links" section before "License", in place of
|
|
23
|
+
"Author", with the package page, release feed, oss index, package search,
|
|
24
|
+
source, community, vllnt.com, author and sponsor links. The entry module's
|
|
25
|
+
TSDoc links the package page.
|
|
26
|
+
- `package.json` `homepage` points to
|
|
27
|
+
`https://oss.vllnt.com/packages/next-llms`.
|
|
28
|
+
- Every push to `main` that changes the package or its shipped docs publishes a
|
|
29
|
+
canary; the canary job no longer depends on a repository variable.
|
|
30
|
+
- The README is rewritten for coding agents: a one-sentence summary, then "For
|
|
31
|
+
coding agents", "When to use", "Install", "Quick start", "API", "Common
|
|
32
|
+
tasks", "Errors and fixes", "Limits and gotchas" and "Compatibility". The
|
|
33
|
+
examples are self-contained, and "Errors and fixes" lists every error the
|
|
34
|
+
package raises itself or returns, plus the Next.js `500` for malformed
|
|
35
|
+
percent-encoding. A test checks the section order, the code blocks, the links,
|
|
36
|
+
that every export and error is documented, and runs the README examples
|
|
37
|
+
against the built package.
|
|
38
|
+
- `package.json` `description` and `keywords` name what the package does and its
|
|
39
|
+
stack. `llms.txt` carries the same summary and use cases and links the API
|
|
40
|
+
reference and the errors table.
|
|
41
|
+
- `docs/API.md` gives each exported type its own heading and replaces the
|
|
42
|
+
undefined `getDoc` placeholder with a self-contained example; so does the
|
|
43
|
+
`createMarkdownRoute` TSDoc example. It no longer says to authorize in the
|
|
44
|
+
resolver, which only receives the slug, and no longer claims Remix, Hono and
|
|
45
|
+
Deno support, which is untested.
|
|
46
|
+
- The docs state that empty segments from `//` are dropped and only a segment
|
|
47
|
+
left empty by stripping `.md` gets `400`, that `.md` is stripped before the
|
|
48
|
+
segments are URL-decoded, that `cacheControl: ""` sends an empty
|
|
49
|
+
`Cache-Control` header, and that CI runs the tests on Node.js 24 only.
|
|
50
|
+
- Development tooling moves to eslint 10, pnpm 12 and `@vllnt/typescript` 3
|
|
51
|
+
(also `@vllnt/eslint-config` 2.1.0, `prettier` ^3.10.1, `vitest` and
|
|
52
|
+
`@vitest/coverage-v8` ^5.1.1); the pnpm settings move from `package.json` to
|
|
53
|
+
`pnpm-workspace.yaml`, which adds a `minimatch` override. CI now checks
|
|
54
|
+
formatting (`pnpm format:check`).
|
|
55
|
+
- Every publish (canary and stable) first runs `pnpm test:package`, which
|
|
56
|
+
installs the packed tarball into a clean project, imports every `exports`
|
|
57
|
+
entry, calls both functions and type-checks a strict TypeScript consumer. The
|
|
58
|
+
package contents check also denies `.spec.mts` and `.spec.cts` files, and CI
|
|
59
|
+
runs its test harness. CI type-checks the test files too.
|
|
60
|
+
|
|
61
|
+
### Fixed
|
|
62
|
+
|
|
63
|
+
- The `llms.txt` link-list example showed a GitHub URL instead of `url`.
|
|
64
|
+
- `llms.txt` now documents the `400` response for unsafe paths and the `404` for
|
|
65
|
+
any non-string resolver result.
|
|
66
|
+
- README links to `SECURITY.md` and `CONTRIBUTING.md`, which are not in the
|
|
67
|
+
package, now point to GitHub so they resolve from `node_modules`.
|
|
68
|
+
|
|
9
69
|
## [0.1.0] - 2026-11-23
|
|
10
70
|
|
|
11
71
|
First stable release. Earlier `0.1.0-canary.*` builds (npm `latest` pointed at
|
|
@@ -15,9 +75,9 @@ version.
|
|
|
15
75
|
### Added
|
|
16
76
|
|
|
17
77
|
- First stable release of `@vllnt/next-llms`.
|
|
18
|
-
- `generateLlmsText(config)` — generate
|
|
19
|
-
`#` title, a `>` blockquote, optional free prose, `## Section` link
|
|
20
|
-
a droppable `## Optional` section
|
|
78
|
+
- `generateLlmsText(config)` — generate an `llms.txt` index in the llmstxt.org
|
|
79
|
+
shape: an `#` title, a `>` blockquote, optional free prose, `## Section` link
|
|
80
|
+
lists, and a droppable `## Optional` section.
|
|
21
81
|
- `createMarkdownRoute(resolver, options?)` — a Web-standard
|
|
22
82
|
`(request) => Promise<Response>` App Router route handler that strips a
|
|
23
83
|
trailing `.md`, resolves the slug, and returns `text/markdown` with a
|
|
@@ -29,10 +89,10 @@ version.
|
|
|
29
89
|
### Security
|
|
30
90
|
|
|
31
91
|
- `createMarkdownRoute` now answers `400` without calling the resolver when the
|
|
32
|
-
path has malformed percent-encoding (previously an unhandled `URIError`)
|
|
33
|
-
|
|
34
|
-
(for example `..%2F..%2Fsecret`), so
|
|
35
|
-
receive a traversal sequence.
|
|
92
|
+
path has malformed percent-encoding (previously an unhandled `URIError`), a
|
|
93
|
+
segment left empty by stripping `.md`, or a decoded segment that is `.`, `..`,
|
|
94
|
+
or contains `/`, `\` or a NUL byte (for example `..%2F..%2Fsecret`), so
|
|
95
|
+
resolvers that map slugs to files never receive a traversal sequence.
|
|
36
96
|
- `createMarkdownRoute` answers `404` when the resolver returns a non-string
|
|
37
97
|
value such as `undefined` (previously an empty `200`), sets
|
|
38
98
|
`X-Content-Type-Options: nosniff`, and throws a `TypeError` at creation for an
|
package/README.md
CHANGED
|
@@ -1,131 +1,252 @@
|
|
|
1
|
-
|
|
1
|
+
# @vllnt/next-llms
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](./LICENSE)
|
|
3
|
+
Generate an llms.txt file and serve pages as Markdown from Next.js App Router
|
|
4
|
+
route handlers, using Web-standard Request and Response and no dependencies.
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
## For coding agents
|
|
7
|
+
|
|
8
|
+
Docs ship in `node_modules/@vllnt/next-llms/` (`README.md`, `docs/API.md`,
|
|
9
|
+
`llms.txt`, `CHANGELOG.md`, `src/`) and match the installed version. The bundled
|
|
10
|
+
`llms.txt` describes this package; it is not output of `generateLlmsText`.
|
|
11
|
+
|
|
12
|
+
## When to use
|
|
13
|
+
|
|
14
|
+
- Use it when your Next.js site should serve an `/llms.txt` link index.
|
|
15
|
+
- Use it when you have Markdown for your pages and want it at `.md` URLs.
|
|
16
|
+
- Do not use it to build the link list from your routes; build it in your code.
|
|
17
|
+
- Do not use it to convert HTML to Markdown; use a converter such as `turndown`.
|
|
18
|
+
- Do not use it for `llms-full.txt`; concatenate your pages in your own route.
|
|
8
19
|
|
|
9
|
-
|
|
10
|
-
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add @vllnt/next-llms
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
11
27
|
|
|
12
28
|
```ts
|
|
13
|
-
// app/
|
|
29
|
+
// app/[...slug]/route.ts — serves /docs/intro.md (and /docs/intro)
|
|
30
|
+
import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
31
|
+
|
|
32
|
+
const pages: Record<string, string> = { "docs/intro": "# Intro\n\nHello." };
|
|
33
|
+
|
|
34
|
+
export const GET = createMarkdownRoute((slug) => pages[slug.join("/")] ?? null);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// app/llms.txt/route.ts — serves /llms.txt
|
|
14
39
|
import { generateLlmsText } from "@vllnt/next-llms";
|
|
15
40
|
|
|
16
|
-
export function GET() {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}),
|
|
28
|
-
{ headers: { "Content-Type": "text/plain; charset=utf-8" } },
|
|
29
|
-
);
|
|
41
|
+
export function GET(): Response {
|
|
42
|
+
const text = generateLlmsText({
|
|
43
|
+
title: "Acme",
|
|
44
|
+
summary: "Acme is a widget API.",
|
|
45
|
+
sections: [
|
|
46
|
+
{ title: "Docs", links: [{ title: "Intro", url: "/docs/intro.md" }] },
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
return new Response(text, {
|
|
50
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
|
51
|
+
});
|
|
30
52
|
}
|
|
31
53
|
```
|
|
32
54
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
## Features
|
|
55
|
+
[](https://www.npmjs.com/package/@vllnt/next-llms)
|
|
56
|
+
[](https://github.com/vllnt/next-llms/actions/workflows/ci.yml)
|
|
57
|
+
[](./LICENSE)
|
|
38
58
|
|
|
39
|
-
|
|
40
|
-
shape: an `#` title, a `>` blockquote, `## Section` link lists, and a
|
|
41
|
-
droppable `## Optional` section.
|
|
42
|
-
- **Raw-markdown routes** — `createMarkdownRoute` serves any page as
|
|
43
|
-
`text/markdown`, stripping a trailing `.md` and resolving the slug through
|
|
44
|
-
your own content source.
|
|
45
|
-
- **Zero dependencies** — pure TypeScript over Web `Request` / `Response`;
|
|
46
|
-
nothing added to your bundle.
|
|
47
|
-
- **Runtime-agnostic** — the route handler imports nothing from `next`; it runs
|
|
48
|
-
on the Edge, Node, or any Web-standard runtime.
|
|
49
|
-
- **Typed end to end** — concrete config types guide correct usage; no `any`.
|
|
50
|
-
- **Cache-friendly** — sensible `Cache-Control` defaults, overridable per route.
|
|
59
|
+
## API
|
|
51
60
|
|
|
52
|
-
|
|
61
|
+
Everything is exported from the package root. Full reference:
|
|
62
|
+
[docs/API.md](docs/API.md).
|
|
53
63
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
| Export | Kind | What it is |
|
|
65
|
+
| -------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
|
66
|
+
| [`generateLlmsText(config)`](docs/API.md#generatellmstextconfig) | function | Returns the `llms.txt` text as a `string`; adds no trailing newline (a `details` that ends with one keeps it when no section follows it). |
|
|
67
|
+
| [`createMarkdownRoute(resolver, options?)`](docs/API.md#createmarkdownrouteresolver-options) | function | Returns a `(request: Request) => Promise<Response>` route handler. |
|
|
68
|
+
| [`LlmsConfig`](docs/API.md#llmsconfig) | type | Input of `generateLlmsText`: title, summary, details, sections, optional. |
|
|
69
|
+
| [`LlmsSection`](docs/API.md#llmssection) | type | One `## {title}` section and its links. |
|
|
70
|
+
| [`LlmsLink`](docs/API.md#llmslink) | type | One `- [title](url): notes` line. |
|
|
71
|
+
| [`MarkdownResolver`](docs/API.md#markdownresolver) | type | `(slug: string[]) => string \| null \| Promise<string \| null>`. |
|
|
72
|
+
| [`MarkdownRouteOptions`](docs/API.md#markdownrouteoptions) | type | `{ cacheControl?: string }` for `createMarkdownRoute`. |
|
|
57
73
|
|
|
58
|
-
|
|
59
|
-
`react`). Every push to `main` can publish a prerelease to the `canary` channel
|
|
60
|
-
(`pnpm add @vllnt/next-llms@canary`); stable releases are published from a
|
|
61
|
-
`vX.Y.Z` tag. The older `generateLlmsFullText` API from early canary builds is
|
|
62
|
-
not part of the stable API; use the two exports below.
|
|
74
|
+
## Common tasks
|
|
63
75
|
|
|
64
|
-
|
|
76
|
+
Serve Markdown files from a `content/` folder (Node.js runtime). The resolver
|
|
77
|
+
never receives a `.` or `..` segment or one containing `/`, `\` or NUL: URL
|
|
78
|
+
parsing resolves dot segments first (`/docs/../x` gives `["x"]`) and the route
|
|
79
|
+
answers `400` for the rest.
|
|
65
80
|
|
|
66
81
|
```ts
|
|
67
|
-
// app/[...slug]/route.ts —
|
|
82
|
+
// app/[...slug]/route.ts — /docs/intro.md reads content/docs/intro.md
|
|
83
|
+
import { readFile } from "node:fs/promises";
|
|
84
|
+
import { join } from "node:path";
|
|
85
|
+
|
|
68
86
|
import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
69
87
|
|
|
88
|
+
const CONTENT_DIR = join(process.cwd(), "content");
|
|
89
|
+
|
|
70
90
|
export const GET = createMarkdownRoute(async (slug) => {
|
|
71
|
-
|
|
72
|
-
|
|
91
|
+
try {
|
|
92
|
+
return await readFile(`${join(CONTENT_DIR, ...slug)}.md`, "utf8");
|
|
93
|
+
} catch {
|
|
94
|
+
return null; // any read error becomes a 404
|
|
95
|
+
}
|
|
73
96
|
});
|
|
74
97
|
```
|
|
75
98
|
|
|
76
|
-
|
|
77
|
-
|
|
99
|
+
Mount the route under a prefix. The slug is built from the full request path, so
|
|
100
|
+
it includes the prefix; drop it yourself.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
// app/md/[...slug]/route.ts — /md/docs/intro.md gives ["md", "docs", "intro"]
|
|
104
|
+
import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
105
|
+
|
|
106
|
+
const pages: Record<string, string> = { "docs/intro": "# Intro\n\nHello." };
|
|
107
|
+
|
|
108
|
+
export const GET = createMarkdownRoute(
|
|
109
|
+
(slug) => pages[slug.slice(1).join("/")] ?? null,
|
|
110
|
+
);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Serve private content. The resolver only receives the slug, so check the request
|
|
114
|
+
in your own `GET` and turn off public caching.
|
|
78
115
|
|
|
79
|
-
|
|
116
|
+
```ts
|
|
117
|
+
// app/[...slug]/route.ts
|
|
118
|
+
import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
80
119
|
|
|
81
|
-
|
|
82
|
-
| ----------------------------------------- | -------- | ----------------------------------------- |
|
|
83
|
-
| `generateLlmsText(config)` | function | `string` — the `llms.txt` index |
|
|
84
|
-
| `createMarkdownRoute(resolver, options?)` | factory | `(request: Request) => Promise<Response>` |
|
|
120
|
+
const pages: Record<string, string> = { "docs/intro": "# Intro\n\nHello." };
|
|
85
121
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
122
|
+
const serve = createMarkdownRoute((slug) => pages[slug.join("/")] ?? null, {
|
|
123
|
+
cacheControl: "private, no-store",
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
export async function GET(request: Request): Promise<Response> {
|
|
127
|
+
const token = process.env.DOCS_TOKEN;
|
|
128
|
+
if (!token || request.headers.get("authorization") !== `Bearer ${token}`) {
|
|
129
|
+
return new Response("Unauthorized", { status: 401 });
|
|
130
|
+
}
|
|
131
|
+
return serve(request);
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Run on the Edge runtime: add `export const runtime = "edge";` to the route file.
|
|
136
|
+
The package uses no Node.js APIs. Next.js 16.4.6 serves it but warns at build
|
|
137
|
+
time that the Edge Runtime is deprecated.
|
|
138
|
+
|
|
139
|
+
## Errors and fixes
|
|
140
|
+
|
|
141
|
+
| Error or response | Cause | Fix |
|
|
142
|
+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
|
|
143
|
+
| `TypeError: title must be a non-empty string` | `generateLlmsText` got a missing, non-string or blank `title`. | Pass a non-empty `title`. |
|
|
144
|
+
| `TypeError: section title must be a non-empty string` | A `sections[].title` is missing or blank. | Give every section a title. |
|
|
145
|
+
| `TypeError: link title must be a non-empty string` | A link in `sections[].links` or `optional` has a `title` that is missing, not a string or blank. | Give every link a title. |
|
|
146
|
+
| `TypeError: link url must be a non-empty string` | A link `url` is missing, not a string or blank. | Give every link a `url`. |
|
|
147
|
+
| `TypeError: link url must not use a javascript:, data: or vbscript: scheme` | A link `url` starts with one of those schemes (case and control characters ignored). | Use an `http(s)` or relative URL. |
|
|
148
|
+
| `TypeError` from the runtime's `Headers` (Node.js: `Headers.append: "<value>" is an invalid header value.` or `Cannot convert argument to a ByteString ...`) | `createMarkdownRoute` got a `cacheControl` with a line break inside the value, a NUL or a character above U+00FF (`Headers` trims leading and trailing whitespace, including line breaks). It is thrown when the route is created, not per request. | Pass a plain ASCII header value. |
|
|
149
|
+
| `400` `Bad Request` (`text/plain`) | A segment is left empty by stripping `.md` (`/.md`, `/docs/.md`), a segment is `.` or `..` after stripping `.md` and decoding (`/docs/...md`), a decoded segment contains `/`, `\` or NUL (for example `/docs/..%2Fsecret.md`), or, outside Next.js, the path has malformed percent-encoding. The resolver is not called. URL parsing resolves plain `.` and `..` segments first (`/docs/../x` gives `["x"]`), and empty segments from `//` are dropped, not rejected (`/docs//intro.md` gives `["docs", "intro"]`). | Request a path whose segments are plain names. |
|
|
150
|
+
| `404` `Not Found` (`text/plain`) | The resolver returned `null` or any non-string (such as `undefined`). | Return the Markdown `string`; return `null` only for missing pages. |
|
|
151
|
+
| Rejected promise (Next.js answers `500`) | The resolver threw or rejected; the handler does not catch it. | Catch errors in the resolver and return `null` or a string. |
|
|
152
|
+
| `500` `Internal Server Error` from Next.js | In Next.js, a path with malformed percent-encoding (such as `/docs/%E0.md`) fails inside Next.js before the route runs. | Nothing to fix in the route; request a correctly encoded path. |
|
|
153
|
+
| Other `TypeError`s, such as `Cannot read properties of undefined (reading 'map')` | Input that does not match the types, for example from plain JavaScript: a section without `links`, or no config at all. `createMarkdownRoute(resolver, null)` throws `Cannot read properties of null (reading 'cacheControl')` at creation; a resolver that is not a function rejects with `resolver is not a function` on each request (Next.js answers `500`). | Pass values that match the exported types (`LlmsConfig`, `MarkdownResolver`, `MarkdownRouteOptions`). |
|
|
154
|
+
|
|
155
|
+
## Limits and gotchas
|
|
156
|
+
|
|
157
|
+
- The slug comes from the full request path, not from Next.js route params: a
|
|
158
|
+
route under `app/md/` gets `"md"` as its first segment.
|
|
159
|
+
- Only a lowercase trailing `.md` on the last segment is removed. `/docs/intro`
|
|
160
|
+
and `/docs/intro.md` give the same slug; `/docs/INTRO.MD` gives `"INTRO.MD"`.
|
|
161
|
+
- `app/[...slug]` does not match `/`. Use `app/[[...slug]]` to also serve `/`
|
|
162
|
+
(slug `[]`).
|
|
163
|
+
- A dynamic page route wins over a root catch-all: with
|
|
164
|
+
`app/blog/[slug]/page.tsx`, `/blog/post.md` renders that page, not your
|
|
165
|
+
Markdown.
|
|
166
|
+
- A root catch-all answers every unmatched URL with a `text/plain` 404, not your
|
|
167
|
+
site's HTML not-found page. Mount it under a prefix to avoid that.
|
|
168
|
+
- Default `Cache-Control` is public:
|
|
169
|
+
`public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800`.
|
|
170
|
+
- `cacheControl: ""` sends an empty `Cache-Control` header; it does not fall
|
|
171
|
+
back to the default. Leave the option out to get the default.
|
|
172
|
+
- An empty string from the resolver is content: `200` with an empty body.
|
|
173
|
+
- `generateLlmsText` adds no trailing newline; a `details` that ends with one
|
|
174
|
+
keeps it when no section (from `sections` or `optional`) follows it. A
|
|
175
|
+
`summary`, `details` or
|
|
176
|
+
`notes` that is `""`, and an empty `optional` array, are left out without a
|
|
177
|
+
warning. Whitespace-only `notes` are also left out, but a whitespace-only
|
|
178
|
+
`summary` renders an empty `> ` line. A section with no links renders only its
|
|
179
|
+
heading.
|
|
180
|
+
- `details` is inserted as-is, even when it is only whitespace; it is not
|
|
181
|
+
escaped.
|
|
182
|
+
- In Next.js, a path with malformed percent-encoding gets Next's own `500`
|
|
183
|
+
before the route runs, so the handler's `400` for it is only seen outside
|
|
184
|
+
Next.js.
|
|
185
|
+
- Only `GET` is created. In Next.js, `HEAD` is answered from `GET`, `OPTIONS`
|
|
186
|
+
gets `204` with `Allow: GET, HEAD, OPTIONS`, and other methods get `405`.
|
|
187
|
+
- ESM only; there is no CommonJS build.
|
|
188
|
+
|
|
189
|
+
## Compatibility
|
|
190
|
+
|
|
191
|
+
- Peer dependencies: none. Runtime dependencies: none.
|
|
192
|
+
- Node.js `>=18` (`engines`). Needs the global `Request`, `Response`, `URL` and
|
|
193
|
+
`Headers`. CI runs the tests on Node.js 24 only, so 18 to 23 are not tested.
|
|
194
|
+
- Next.js is not imported. The package was checked by hand with Next.js 16.4.6
|
|
195
|
+
App Router route handlers on the Node.js and Edge runtimes (Next.js marks the
|
|
196
|
+
Edge Runtime deprecated); CI runs the unit tests outside Next.js.
|
|
197
|
+
- Other runtimes and frameworks with the same Web APIs (Deno, Bun, Hono, Remix)
|
|
198
|
+
are not tested.
|
|
199
|
+
- Release channels: stable on `latest`, prereleases on `canary`
|
|
200
|
+
(`pnpm add @vllnt/next-llms@canary`). `generateLlmsFullText` from early canary
|
|
201
|
+
builds is not part of the stable API.
|
|
202
|
+
|
|
203
|
+
## Testing
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
pnpm test # single run
|
|
207
|
+
pnpm test:coverage # enforced 100% on covered files
|
|
208
|
+
```
|
|
89
209
|
|
|
90
210
|
## Security
|
|
91
211
|
|
|
92
212
|
- No secrets touched — the package generates text and serves whatever your
|
|
93
213
|
resolver returns.
|
|
94
|
-
- Your resolver is the boundary —
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
maps to a file or record.
|
|
214
|
+
- Your resolver is the boundary — decide which slugs it may return. It receives
|
|
215
|
+
only the slug, so check the request yourself for per-user access (see
|
|
216
|
+
[Common tasks](#common-tasks)).
|
|
98
217
|
- `generateLlmsText` escapes link titles and URLs and collapses line breaks, and
|
|
99
218
|
rejects blank titles/URLs and `javascript:`, `data:` or `vbscript:` URLs with
|
|
100
219
|
a `TypeError`.
|
|
101
|
-
- Successful responses default to **public caching
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
caching.
|
|
220
|
+
- Successful responses default to **public caching**. For private or
|
|
221
|
+
personalized content, pass `{ cacheControl: "private, no-store" }`;
|
|
222
|
+
authorization alone does not disable caching.
|
|
105
223
|
|
|
106
|
-
See [docs/API.md](docs/API.md) and the
|
|
224
|
+
See [docs/API.md](docs/API.md) and the
|
|
225
|
+
[security reporting policy](https://github.com/vllnt/next-llms/blob/main/SECURITY.md).
|
|
107
226
|
|
|
108
|
-
##
|
|
227
|
+
## Related packages
|
|
109
228
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
pnpm test:coverage # enforced 100% on covered files
|
|
113
|
-
```
|
|
229
|
+
No other `@vllnt` package covers `llms.txt` manifests or markdown routes. See
|
|
230
|
+
every package at [oss.vllnt.com/packages](https://oss.vllnt.com/packages).
|
|
114
231
|
|
|
115
232
|
## Contributing
|
|
116
233
|
|
|
117
|
-
See
|
|
118
|
-
|
|
119
|
-
## Author
|
|
234
|
+
See
|
|
235
|
+
[CONTRIBUTING.md](https://github.com/vllnt/next-llms/blob/main/CONTRIBUTING.md).
|
|
120
236
|
|
|
121
|
-
|
|
122
|
-
[bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
237
|
+
## Links
|
|
123
238
|
|
|
124
|
-
|
|
125
|
-
[vllnt.com](https://vllnt.com)
|
|
126
|
-
|
|
127
|
-
|
|
239
|
+
- Docs and changelog: [oss.vllnt.com/packages/next-llms](https://oss.vllnt.com/packages/next-llms) · [release feed](https://oss.vllnt.com/packages/next-llms/rss.xml)
|
|
240
|
+
- All @vllnt packages: [oss.vllnt.com](https://oss.vllnt.com) · [search packages and docs on index.vllnt.com](https://index.vllnt.com)
|
|
241
|
+
- Source and issues: [GitHub](https://github.com/vllnt/next-llms) · [issues](https://github.com/vllnt/next-llms/issues) · [npm](https://www.npmjs.com/package/@vllnt/next-llms)
|
|
242
|
+
- Questions and updates: [Discord](https://bntvllnt.com/discord) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
243
|
+
- Company and author: [vllnt.com](https://vllnt.com) · [bntvllnt.com](https://bntvllnt.com) · [GitHub @bntvllnt](https://github.com/bntvllnt) · [LinkedIn](https://linkedin.com/in/bntvllnt)
|
|
244
|
+
- Sponsor: [GitHub Sponsors](https://github.com/sponsors/bntvllnt)
|
|
128
245
|
|
|
129
246
|
## License
|
|
130
247
|
|
|
131
248
|
MIT — see [LICENSE](LICENSE).
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
Part of [vllnt open source](https://oss.vllnt.com) · [Package page](https://oss.vllnt.com/packages/next-llms) · [vllnt.com](https://vllnt.com) · Built by [bntvllnt](https://bntvllnt.com)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate an `llms.txt` file and serve pages as Markdown from Next.js App
|
|
3
|
+
* Router route handlers.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
* @see https://oss.vllnt.com/packages/next-llms
|
|
7
|
+
*/
|
|
1
8
|
export { generateLlmsText } from "./llms-text.js";
|
|
2
9
|
export { createMarkdownRoute } from "./markdown-route.js";
|
|
3
10
|
export type { LlmsConfig, LlmsLink, LlmsSection, MarkdownResolver, MarkdownRouteOptions, } from "./types.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,YAAY,EACV,UAAU,EACV,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,YAAY,EACV,UAAU,EACV,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate an `llms.txt` file and serve pages as Markdown from Next.js App
|
|
3
|
+
* Router route handlers.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
* @see https://oss.vllnt.com/packages/next-llms
|
|
7
|
+
*/
|
|
1
8
|
export { generateLlmsText } from "./llms-text.js";
|
|
2
9
|
export { createMarkdownRoute } from "./markdown-route.js";
|
|
3
10
|
//# 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,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/llms-text.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ import type { LlmsConfig } from "./types.js";
|
|
|
5
5
|
* prose, `## {section}` link lists, and a droppable `## Optional` section.
|
|
6
6
|
*
|
|
7
7
|
* @param config - The manifest content.
|
|
8
|
-
* @returns The `llms.txt` file contents
|
|
8
|
+
* @returns The `llms.txt` file contents. No trailing newline is added; a
|
|
9
|
+
* `details` value that ends with one keeps it when no section (from `sections`
|
|
10
|
+
* or `optional`) follows it.
|
|
9
11
|
* @throws {TypeError} When a title or link URL is blank, or a URL uses a
|
|
10
12
|
* `javascript:`, `data:` or `vbscript:` scheme. Line breaks inside titles,
|
|
11
13
|
* summaries and notes are collapsed to spaces, `[`, `]` and `\` in link titles
|
package/dist/llms-text.d.ts.map
CHANGED
|
@@ -1 +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;AAqDpE
|
|
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;AAqDpE;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAU3D"}
|
package/dist/llms-text.js
CHANGED
|
@@ -45,7 +45,9 @@ function renderSection(section) {
|
|
|
45
45
|
* prose, `## {section}` link lists, and a droppable `## Optional` section.
|
|
46
46
|
*
|
|
47
47
|
* @param config - The manifest content.
|
|
48
|
-
* @returns The `llms.txt` file contents
|
|
48
|
+
* @returns The `llms.txt` file contents. No trailing newline is added; a
|
|
49
|
+
* `details` value that ends with one keeps it when no section (from `sections`
|
|
50
|
+
* or `optional`) follows it.
|
|
49
51
|
* @throws {TypeError} When a title or link URL is blank, or a URL uses a
|
|
50
52
|
* `javascript:`, `data:` or `vbscript:` scheme. Line breaks inside titles,
|
|
51
53
|
* summaries and notes are collapsed to spaces, `[`, `]` and `\` in link titles
|
package/dist/llms-text.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llms-text.js","sourceRoot":"","sources":["../src/llms-text.ts"],"names":[],"mappings":"AAEA,MAAM,iBAAiB,GAAG,iCAAiC,CAAC;AAC5D,0GAA0G;AAC1G,MAAM,YAAY,GAAG,mBAAmB,CAAC;AACzC,MAAM,WAAW,GAAG,4BAA4B,CAAC;AAEjD,+EAA+E;AAC/E,SAAS,kBAAkB,CAAC,SAAiB;IAC3C,IAAI,SAAS,KAAK,GAAG;QAAE,OAAO,KAAK,CAAC;IACpC,IAAI,SAAS,KAAK,GAAG;QAAE,OAAO,KAAK,CAAC;IACpC,OAAO,kBAAkB,CAAC,SAAS,CAAC,CAAC;AACvC,CAAC;AAED,yEAAyE;AACzE,SAAS,UAAU,CAAC,KAAa;IAC/B,OAAO,KAAK,CAAC,UAAU,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AACnD,CAAC;AAED,yDAAyD;AACzD,SAAS,YAAY,CAAC,KAAc,EAAE,KAAa;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,IAAI,SAAS,CAAC,GAAG,KAAK,6BAA6B,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC;AAC3B,CAAC;AAED,SAAS,SAAS,CAAC,GAAY;IAC7B,MAAM,IAAI,GAAG,YAAY,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;IAC3C,IAAI,iBA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/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"}
|
package/dist/markdown-route.d.ts
CHANGED
|
@@ -3,17 +3,21 @@ import type { MarkdownResolver, MarkdownRouteOptions } from "./types.js";
|
|
|
3
3
|
* Create a Next.js App Router route handler that serves a slug's markdown
|
|
4
4
|
* content. Mount it at a catch-all route (`app/[...slug]/route.ts`). The handler
|
|
5
5
|
* is a pure Web-standard `(request) => Promise<Response>` — it imports nothing
|
|
6
|
-
* from `next` and
|
|
6
|
+
* from `next` and uses only Web-standard APIs.
|
|
7
7
|
*
|
|
8
|
-
* - Strips a trailing `.md` from the path (`/docs/intro.md`
|
|
9
|
-
* `["docs", "intro"]`).
|
|
8
|
+
* - Strips a trailing lowercase `.md` from the path (`/docs/intro.md`
|
|
9
|
+
* resolves the slug `["docs", "intro"]`).
|
|
10
10
|
* - Returns `200` with `Content-Type: text/markdown; charset=utf-8` and a
|
|
11
11
|
* `Cache-Control` header when the resolver returns a string.
|
|
12
12
|
* - Returns `404` (plain text) when the resolver returns `null` (or any
|
|
13
13
|
* non-string value, such as `undefined` from a missing map entry).
|
|
14
14
|
* - Returns `400` (plain text) without calling the resolver when the path has
|
|
15
|
-
* malformed percent-encoding
|
|
16
|
-
*
|
|
15
|
+
* malformed percent-encoding, a segment left empty by stripping `.md`
|
|
16
|
+
* (`/docs/.md`), or a decoded segment that is `.`, `..`, or contains `/`, `\`
|
|
17
|
+
* or a NUL byte. URL parsing resolves `.` and `..` segments first
|
|
18
|
+
* (`/docs/../x` gives `["x"]`), so a dot segment is rejected only when
|
|
19
|
+
* stripping `.md` leaves one (`/docs/...md`, `/docs/%2E%2E.md`). Empty
|
|
20
|
+
* segments from `//` are dropped, not rejected.
|
|
17
21
|
* - Sets `X-Content-Type-Options: nosniff` on every response.
|
|
18
22
|
*
|
|
19
23
|
* @param resolver - Maps a slug to its markdown, or `null` when not found.
|
|
@@ -26,8 +30,10 @@ import type { MarkdownResolver, MarkdownRouteOptions } from "./types.js";
|
|
|
26
30
|
* // app/[...slug]/route.ts
|
|
27
31
|
* import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
28
32
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
33
|
+
* const pages: Record<string, string> = { "docs/intro": "# Intro\n\nHello." };
|
|
34
|
+
*
|
|
35
|
+
* export const GET = createMarkdownRoute(
|
|
36
|
+
* (slug) => pages[slug.join("/")] ?? null,
|
|
31
37
|
* );
|
|
32
38
|
* ```
|
|
33
39
|
*/
|
|
@@ -1 +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;
|
|
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;AAuCzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,GAAE,oBAAyB,GACjC,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CA6BzC"}
|
package/dist/markdown-route.js
CHANGED
|
@@ -4,13 +4,17 @@ const PLAIN_TEXT_HEADERS = {
|
|
|
4
4
|
"X-Content-Type-Options": "nosniff",
|
|
5
5
|
};
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Split a URL pathname into slug segments, dropping empty segments (from `//`),
|
|
8
|
+
* then strip a trailing lowercase `.md` from the last raw segment, then
|
|
9
|
+
* URL-decode each segment. `/docs/intro.md` → `["docs", "intro"]`; `/` → `[]`.
|
|
9
10
|
*
|
|
10
11
|
* Returns `null` when the path cannot name a page safely: malformed
|
|
11
|
-
* percent-encoding,
|
|
12
|
-
* `/`, `\` or a NUL byte (so a
|
|
13
|
-
* handed a traversal sequence such
|
|
12
|
+
* percent-encoding, a segment left empty by stripping `.md` (`/docs/.md`), or a
|
|
13
|
+
* decoded segment that is `.`, `..`, or contains `/`, `\` or a NUL byte (so a
|
|
14
|
+
* resolver that maps slugs to files is never handed a traversal sequence such
|
|
15
|
+
* as `..%2F..%2Fsecret`). `new URL` already resolves `.` and `..` segments,
|
|
16
|
+
* including `%2E` forms, so a dot segment reaches this check only when
|
|
17
|
+
* stripping `.md` leaves one (`/docs/...md`, `/docs/%2E%2E.md`).
|
|
14
18
|
*/
|
|
15
19
|
function pathnameToSlug(pathname) {
|
|
16
20
|
const segments = pathname.split("/").filter((segment) => segment.length > 0);
|
|
@@ -32,17 +36,21 @@ function pathnameToSlug(pathname) {
|
|
|
32
36
|
* Create a Next.js App Router route handler that serves a slug's markdown
|
|
33
37
|
* content. Mount it at a catch-all route (`app/[...slug]/route.ts`). The handler
|
|
34
38
|
* is a pure Web-standard `(request) => Promise<Response>` — it imports nothing
|
|
35
|
-
* from `next` and
|
|
39
|
+
* from `next` and uses only Web-standard APIs.
|
|
36
40
|
*
|
|
37
|
-
* - Strips a trailing `.md` from the path (`/docs/intro.md`
|
|
38
|
-
* `["docs", "intro"]`).
|
|
41
|
+
* - Strips a trailing lowercase `.md` from the path (`/docs/intro.md`
|
|
42
|
+
* resolves the slug `["docs", "intro"]`).
|
|
39
43
|
* - Returns `200` with `Content-Type: text/markdown; charset=utf-8` and a
|
|
40
44
|
* `Cache-Control` header when the resolver returns a string.
|
|
41
45
|
* - Returns `404` (plain text) when the resolver returns `null` (or any
|
|
42
46
|
* non-string value, such as `undefined` from a missing map entry).
|
|
43
47
|
* - Returns `400` (plain text) without calling the resolver when the path has
|
|
44
|
-
* malformed percent-encoding
|
|
45
|
-
*
|
|
48
|
+
* malformed percent-encoding, a segment left empty by stripping `.md`
|
|
49
|
+
* (`/docs/.md`), or a decoded segment that is `.`, `..`, or contains `/`, `\`
|
|
50
|
+
* or a NUL byte. URL parsing resolves `.` and `..` segments first
|
|
51
|
+
* (`/docs/../x` gives `["x"]`), so a dot segment is rejected only when
|
|
52
|
+
* stripping `.md` leaves one (`/docs/...md`, `/docs/%2E%2E.md`). Empty
|
|
53
|
+
* segments from `//` are dropped, not rejected.
|
|
46
54
|
* - Sets `X-Content-Type-Options: nosniff` on every response.
|
|
47
55
|
*
|
|
48
56
|
* @param resolver - Maps a slug to its markdown, or `null` when not found.
|
|
@@ -55,8 +63,10 @@ function pathnameToSlug(pathname) {
|
|
|
55
63
|
* // app/[...slug]/route.ts
|
|
56
64
|
* import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
57
65
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
66
|
+
* const pages: Record<string, string> = { "docs/intro": "# Intro\n\nHello." };
|
|
67
|
+
*
|
|
68
|
+
* export const GET = createMarkdownRoute(
|
|
69
|
+
* (slug) => pages[slug.join("/")] ?? null,
|
|
60
70
|
* );
|
|
61
71
|
* ```
|
|
62
72
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-route.js","sourceRoot":"","sources":["../src/markdown-route.ts"],"names":[],"mappings":"AAEA,MAAM,qBAAqB,GACzB,qEAAqE,CAAC;AAExE,MAAM,kBAAkB,GAAG;IACzB,cAAc,EAAE,2BAA2B;IAC3C,wBAAwB,EAAE,SAAS;CACpC,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"markdown-route.js","sourceRoot":"","sources":["../src/markdown-route.ts"],"names":[],"mappings":"AAEA,MAAM,qBAAqB,GACzB,qEAAqE,CAAC;AAExE,MAAM,kBAAkB,GAAG;IACzB,cAAc,EAAE,2BAA2B;IAC3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C,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;QAC3D,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YAClB,OAAO,IAAI,QAAQ,CAAC,aAAa,EAAE;gBACjC,OAAO,EAAE,kBAAkB;gBAC3B,MAAM,EAAE,GAAG;aACZ,CAAC,CAAC;QACL,CAAC;QACD,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAC;QAErC,OAAO,OAAO,OAAO,KAAK,QAAQ;YAChC,CAAC,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE;gBACpB,OAAO,EAAE;oBACP,eAAe,EAAE,YAAY;oBAC7B,cAAc,EAAE,8BAA8B;oBAC9C,wBAAwB,EAAE,SAAS;iBACpC;gBACD,MAAM,EAAE,GAAG;aACZ,CAAC;YACJ,CAAC,CAAC,IAAI,QAAQ,CAAC,WAAW,EAAE;gBACxB,OAAO,EAAE,kBAAkB;gBAC3B,MAAM,EAAE,GAAG;aACZ,CAAC,CAAC;IACT,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -41,7 +41,9 @@ export type LlmsConfig = {
|
|
|
41
41
|
};
|
|
42
42
|
/**
|
|
43
43
|
* Maps a route slug to its markdown content, or `null` if there is no page for
|
|
44
|
-
* the slug. The slug is the path segments
|
|
44
|
+
* the slug. The slug is the non-empty path segments, including any route
|
|
45
|
+
* prefix, with a trailing lowercase `.md` removed from the last raw segment,
|
|
46
|
+
* then each segment URL-decoded.
|
|
45
47
|
*/
|
|
46
48
|
export type MarkdownResolver = (slug: string[]) => string | null | Promise<string | null>;
|
|
47
49
|
/**
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +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,KAA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AAQ,CAAC,EAAE,WAAW,EAAE,CAAC;IACzB;;;OAGG;IACH,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;CACvB,CAAC;AAEF;;;;;GAKG;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/docs/API.md
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
# API Reference — @vllnt/next-llms
|
|
2
2
|
|
|
3
|
-
**Compatibility:**
|
|
4
|
-
|
|
3
|
+
**Compatibility:** uses only Web-standard `Request`, `Response`, `URL` and
|
|
4
|
+
`Headers`. Checked with Next.js 16.4.6 App Router route handlers (Node.js and
|
|
5
|
+
Edge runtimes); other runtimes are not tested. Node `>=18`; CI runs the tests on
|
|
6
|
+
Node.js 24 only, so 18 to 23 are not tested. Zero runtime dependencies.
|
|
5
7
|
|
|
6
|
-
See [release channels](../README.md#
|
|
8
|
+
See [release channels](../README.md#compatibility) and
|
|
9
|
+
[errors and fixes](../README.md#errors-and-fixes). `generateLlmsFullText` from
|
|
7
10
|
early canary builds is not part of the stable API.
|
|
8
11
|
|
|
9
|
-
All exports are available from the package root
|
|
10
|
-
|
|
11
|
-
```ts
|
|
12
|
-
import { generateLlmsText, createMarkdownRoute } from "@vllnt/next-llms";
|
|
13
|
-
```
|
|
12
|
+
All exports are available from the package root (`"@vllnt/next-llms"`).
|
|
14
13
|
|
|
15
14
|
---
|
|
16
15
|
|
|
@@ -23,7 +22,8 @@ Render an `llms.txt` index manifest from a config object, following the
|
|
|
23
22
|
function generateLlmsText(config: LlmsConfig): string;
|
|
24
23
|
```
|
|
25
24
|
|
|
26
|
-
Output layout (blocks joined by a blank line
|
|
25
|
+
Output layout (blocks joined by a blank line; no trailing newline is added, but
|
|
26
|
+
a `details` that ends with one keeps it when no section follows it):
|
|
27
27
|
|
|
28
28
|
1. `# {title}` — required H1.
|
|
29
29
|
2. `> {summary}` — blockquote, when `summary` is set.
|
|
@@ -49,13 +49,24 @@ emitted unchanged.
|
|
|
49
49
|
| `sections` | `LlmsSection[]?` | Named `## Section` link lists. |
|
|
50
50
|
| `optional` | `LlmsLink[]?` | Links under the droppable `## Optional` section. |
|
|
51
51
|
|
|
52
|
-
`LlmsSection`
|
|
53
|
-
|
|
52
|
+
### `LlmsSection`
|
|
53
|
+
|
|
54
|
+
`{ title: string; links: LlmsLink[] }` — one `## {title}` block and its links.
|
|
55
|
+
`title` is required; a section with an empty `links` array renders only its
|
|
56
|
+
heading.
|
|
57
|
+
|
|
58
|
+
### `LlmsLink`
|
|
59
|
+
|
|
60
|
+
`{ title: string; url: string; notes?: string }` — one `- [title](url): notes`
|
|
61
|
+
line. `title` and `url` are required; `: notes` is left out when `notes` is
|
|
62
|
+
empty.
|
|
54
63
|
|
|
55
64
|
### Example
|
|
56
65
|
|
|
57
66
|
```ts
|
|
58
|
-
generateLlmsText
|
|
67
|
+
import { generateLlmsText } from "@vllnt/next-llms";
|
|
68
|
+
|
|
69
|
+
export const text = generateLlmsText({
|
|
59
70
|
title: "Acme Docs",
|
|
60
71
|
summary: "Everything an agent needs to use Acme.",
|
|
61
72
|
sections: [
|
|
@@ -83,35 +94,53 @@ function createMarkdownRoute(
|
|
|
83
94
|
): (request: Request) => Promise<Response>;
|
|
84
95
|
```
|
|
85
96
|
|
|
86
|
-
`MarkdownResolver`
|
|
97
|
+
### `MarkdownResolver`
|
|
98
|
+
|
|
87
99
|
`(slug: string[]) => string | null | Promise<string | null>`. The slug is the
|
|
88
|
-
path
|
|
100
|
+
full request path's non-empty segments: a trailing lowercase `.md` is removed
|
|
101
|
+
from the last raw segment, then each segment is URL-decoded (`/docs/intro%2Emd`
|
|
102
|
+
gives `["docs", "intro.md"]`). It includes any route prefix (`/md/docs/intro.md`
|
|
103
|
+
gives `["md", "docs", "intro"]`). Return a string for content or `null` for not
|
|
104
|
+
found.
|
|
89
105
|
|
|
90
|
-
`MarkdownRouteOptions`
|
|
106
|
+
### `MarkdownRouteOptions`
|
|
107
|
+
|
|
108
|
+
`{ cacheControl?: string }` — the `Cache-Control` value for `200` responses.
|
|
109
|
+
`cacheControl: ""` sends an empty `Cache-Control` header; it does not fall back
|
|
110
|
+
to the default.
|
|
91
111
|
|
|
92
112
|
### Behavior
|
|
93
113
|
|
|
94
|
-
| Request | Resolver returns | Response
|
|
95
|
-
| ---------------------------- | ---------------- |
|
|
96
|
-
| `/docs/intro.md` | `"# Intro…"` | `200`, `Content-Type: text/markdown; charset=utf-8`, `Cache-Control`
|
|
97
|
-
| `/docs/intro.md` | `""` | `200` with an empty body (empty string is content, not absence)
|
|
98
|
-
| `/missing.md` | `null` | `404`, `Content-Type: text/plain; charset=utf-8`, body `Not Found`
|
|
99
|
-
| `/missing.md` | non-string | `404`, same as `null`
|
|
100
|
-
| `/..%2Fsecret`, `/a%00b` | not called | `400`, `text/plain`, body `Bad Request`
|
|
101
|
-
| `/docs/getting%20started.md` | — | resolver receives `["docs", "getting started"]`
|
|
102
|
-
| `/` | — | resolver receives `[]`
|
|
114
|
+
| Request | Resolver returns | Response |
|
|
115
|
+
| ---------------------------- | ---------------- | ------------------------------------------------------------------------------------------- |
|
|
116
|
+
| `/docs/intro.md` | `"# Intro…"` | `200`, `Content-Type: text/markdown; charset=utf-8`, `Cache-Control` |
|
|
117
|
+
| `/docs/intro.md` | `""` | `200` with an empty body (empty string is content, not absence) |
|
|
118
|
+
| `/missing.md` | `null` | `404`, `Content-Type: text/plain; charset=utf-8`, body `Not Found` |
|
|
119
|
+
| `/missing.md` | non-string | `404`, same as `null` |
|
|
120
|
+
| `/..%2Fsecret`, `/a%00b` | not called | `400`, `text/plain`, body `Bad Request` |
|
|
121
|
+
| `/docs/getting%20started.md` | — | resolver receives `["docs", "getting started"]` |
|
|
122
|
+
| `/` | — | resolver receives `[]` (needs `app/[[...slug]]` in Next.js; `[...slug]` does not match `/`) |
|
|
103
123
|
|
|
104
124
|
A request is rejected with `400` (the resolver is not called) when the path has
|
|
105
|
-
malformed percent-encoding
|
|
106
|
-
|
|
125
|
+
malformed percent-encoding, a segment left empty by stripping `.md` (`/.md`,
|
|
126
|
+
`/docs/.md`), a segment that is `.` or `..` after stripping `.md` and decoding
|
|
127
|
+
(`/docs/...md`), or a decoded segment that contains `/`, `\` or a NUL byte. URL
|
|
128
|
+
parsing resolves plain `.` and `..` segments, including `%2E` forms, before the
|
|
129
|
+
handler runs (`/docs/../x` gives `["x"]`), so the resolver never receives them.
|
|
130
|
+
Empty segments from `//` are dropped, not rejected (`/docs//intro.md` gives
|
|
131
|
+
`["docs", "intro"]`). In Next.js, malformed percent-encoding (such as
|
|
132
|
+
`/docs/%E0.md`) gets Next's own `500` before the handler runs, so only the
|
|
133
|
+
segment checks are seen there. Every response carries
|
|
107
134
|
`X-Content-Type-Options: nosniff`. An invalid `cacheControl` value throws a
|
|
108
135
|
`TypeError` when the handler is created.
|
|
109
136
|
|
|
110
137
|
Default `Cache-Control`:
|
|
111
138
|
`public, max-age=3600, s-maxage=86400, stale-while-revalidate=604800` — override
|
|
112
139
|
with `options.cacheControl`. For private or personalized responses, explicitly
|
|
113
|
-
use `{ cacheControl: "private, no-store" }
|
|
114
|
-
|
|
140
|
+
use `{ cacheControl: "private, no-store" }`, and check the request in your own
|
|
141
|
+
`GET` before calling the handler: the resolver receives only the slug (see
|
|
142
|
+
[Common tasks](../README.md#common-tasks)). The public default is not suitable
|
|
143
|
+
for authenticated content.
|
|
115
144
|
|
|
116
145
|
### Example
|
|
117
146
|
|
|
@@ -119,8 +148,10 @@ resolver. The public default is not suitable for authenticated content.
|
|
|
119
148
|
// app/[...slug]/route.ts
|
|
120
149
|
import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
121
150
|
|
|
151
|
+
const pages: Record<string, string> = { "docs/intro": "# Intro\n\nHello." };
|
|
152
|
+
|
|
122
153
|
export const GET = createMarkdownRoute(
|
|
123
|
-
|
|
154
|
+
(slug) => pages[slug.join("/")] ?? null,
|
|
124
155
|
{ cacheControl: "public, max-age=60" },
|
|
125
156
|
);
|
|
126
157
|
```
|
package/llms.txt
CHANGED
|
@@ -1,36 +1,63 @@
|
|
|
1
1
|
# @vllnt/next-llms
|
|
2
2
|
|
|
3
|
-
>
|
|
4
|
-
>
|
|
3
|
+
> Generate an llms.txt file and serve pages as Markdown from Next.js App Router
|
|
4
|
+
> route handlers, using Web-standard Request and Response and no dependencies.
|
|
5
5
|
|
|
6
|
-
`@vllnt/next-llms` is a
|
|
7
|
-
Next.js (App Router) and any Web-standard runtime. It exposes two functions.
|
|
6
|
+
`@vllnt/next-llms` is a TypeScript ESM package with two functions.
|
|
8
7
|
`generateLlmsText(config)` returns an `llms.txt` index string in the llmstxt.org
|
|
9
8
|
shape — an `#` title, a `>` blockquote, optional free prose, `## Section` link
|
|
10
|
-
lists (`- [title](
|
|
9
|
+
lists (`- [title](url): notes`), and a droppable `## Optional` section.
|
|
11
10
|
`createMarkdownRoute(resolver, options?)` returns a Web-standard
|
|
12
|
-
`(request) => Promise<Response>` route handler: it
|
|
13
|
-
strips a trailing `.md
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
`(request) => Promise<Response>` route handler: it drops empty path segments,
|
|
12
|
+
strips a trailing lowercase `.md` from the last segment, then URL-decodes each
|
|
13
|
+
segment, calls the resolver with the slug, and returns `200` `text/markdown`
|
|
14
|
+
with a `Cache-Control` header when the resolver returns a string (an empty
|
|
15
|
+
string is content), or `404` `text/plain` when it returns `null` or any other
|
|
16
|
+
non-string. A path with malformed percent-encoding, a segment that is empty,
|
|
17
|
+
`.` or `..` after stripping `.md` and decoding (such as `/.md` or `/...md`), or
|
|
18
|
+
a decoded segment that contains `/`, `\` or NUL gets `400` without calling the
|
|
19
|
+
resolver (URL parsing already resolves plain `.` and `..` segments, so
|
|
20
|
+
`/docs/../x` gives `["x"]`); in Next.js, malformed percent-encoding gets Next's
|
|
21
|
+
own `500` before the handler runs. In Next.js, `HEAD` is answered from `GET`,
|
|
22
|
+
`OPTIONS` gets `204` and other methods get `405`. The handler imports nothing
|
|
23
|
+
from `next`. Node.js >=18 (CI tests only Node.js 24); no peer or runtime
|
|
24
|
+
dependencies.
|
|
25
|
+
Successful responses use public caching by default; pass
|
|
26
|
+
`cacheControl: "private, no-store"` for private content and check the request in
|
|
27
|
+
your own `GET`, because the resolver only receives the slug.
|
|
28
|
+
|
|
29
|
+
When to use:
|
|
30
|
+
|
|
31
|
+
- Use it when your Next.js site should serve an `/llms.txt` link index.
|
|
32
|
+
- Use it when you have Markdown for your pages and want it at `.md` URLs.
|
|
33
|
+
- Do not use it to build the link list from your routes; build it in your code.
|
|
34
|
+
- Do not use it to convert HTML to Markdown; use a converter such as `turndown`.
|
|
35
|
+
- Do not use it for `llms-full.txt`; concatenate your pages in your own route.
|
|
36
|
+
|
|
37
|
+
For coding agents: the docs ship in `node_modules/@vllnt/next-llms/` next to
|
|
38
|
+
the TypeScript source in `src/` and match the installed version. This file
|
|
39
|
+
describes the package itself; it is not output of `generateLlmsText`.
|
|
23
40
|
|
|
24
41
|
## Docs
|
|
25
42
|
|
|
26
|
-
- [README](
|
|
27
|
-
- [API Reference](
|
|
28
|
-
- [
|
|
43
|
+
- [README](./README.md): Install, quick start, common tasks, limits and gotchas, compatibility
|
|
44
|
+
- [API Reference](./docs/API.md): Every export, its types and the route's response behavior
|
|
45
|
+
- [Errors and fixes](./README.md#errors-and-fixes): Every error the package raises and every error response, its cause and the fix
|
|
46
|
+
- [Changelog](./CHANGELOG.md): Version history and release notes
|
|
29
47
|
- [Contributing](https://github.com/vllnt/next-llms/blob/main/CONTRIBUTING.md): Development setup, validation commands, release workflow
|
|
30
48
|
|
|
31
49
|
## Optional
|
|
32
50
|
|
|
33
|
-
- [AGENTS](https://github.com/vllnt/next-llms/blob/main/AGENTS.md):
|
|
51
|
+
- [AGENTS](https://github.com/vllnt/next-llms/blob/main/AGENTS.md): Agent instructions for contributors to this repository
|
|
34
52
|
- [Security Policy](https://github.com/vllnt/next-llms/blob/main/SECURITY.md): Vulnerability reporting policy
|
|
35
53
|
- [Code of Conduct](https://github.com/vllnt/next-llms/blob/main/CODE_OF_CONDUCT.md): Community participation guidelines
|
|
36
|
-
- [License](
|
|
54
|
+
- [License](./LICENSE): MIT license
|
|
55
|
+
|
|
56
|
+
## Ecosystem
|
|
57
|
+
|
|
58
|
+
- [Package page](https://oss.vllnt.com/packages/next-llms)
|
|
59
|
+
- [Release feed](https://oss.vllnt.com/packages/next-llms/rss.xml)
|
|
60
|
+
- [All @vllnt packages](https://oss.vllnt.com/llms.txt)
|
|
61
|
+
- [Search vllnt packages and docs](https://index.vllnt.com)
|
|
62
|
+
- [vllnt](https://vllnt.com/llms.txt)
|
|
63
|
+
- [Author](https://bntvllnt.com)
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/next-llms",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1-canary.349aa6b",
|
|
4
|
+
"description": "Generate an llms.txt file and serve pages as Markdown from Next.js App Router route handlers, using Web-standard Request and Response and no dependencies.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@
|
|
7
|
+
"packageManager": "pnpm@12.22.0",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
10
|
"src",
|
|
@@ -34,21 +34,22 @@
|
|
|
34
34
|
"test": "vitest run --passWithNoTests",
|
|
35
35
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
36
36
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
37
|
+
"test:package": "bash scripts/test-package.sh",
|
|
37
38
|
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck",
|
|
38
39
|
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\""
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@types/node": "^24.19.2",
|
|
42
43
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
43
|
-
"@vitest/coverage-v8": "^5.
|
|
44
|
-
"@vllnt/eslint-config": "2.
|
|
45
|
-
"@vllnt/typescript": "
|
|
46
|
-
"eslint": "^
|
|
47
|
-
"prettier": "^3.
|
|
44
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
45
|
+
"@vllnt/eslint-config": "2.1.0",
|
|
46
|
+
"@vllnt/typescript": "3.0.0",
|
|
47
|
+
"eslint": "^10.15.0",
|
|
48
|
+
"prettier": "^3.10.1",
|
|
48
49
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
49
|
-
"vitest": "^5.
|
|
50
|
+
"vitest": "^5.1.1"
|
|
50
51
|
},
|
|
51
|
-
"homepage": "https://
|
|
52
|
+
"homepage": "https://oss.vllnt.com/packages/next-llms",
|
|
52
53
|
"bugs": {
|
|
53
54
|
"url": "https://github.com/vllnt/next-llms/issues"
|
|
54
55
|
},
|
|
@@ -72,28 +73,15 @@
|
|
|
72
73
|
},
|
|
73
74
|
"sideEffects": false,
|
|
74
75
|
"keywords": [
|
|
75
|
-
"nextjs",
|
|
76
|
-
"next",
|
|
77
|
-
"llms-txt",
|
|
78
76
|
"llms.txt",
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"seo",
|
|
77
|
+
"llms-txt",
|
|
78
|
+
"nextjs",
|
|
82
79
|
"app-router",
|
|
80
|
+
"route-handler",
|
|
83
81
|
"markdown",
|
|
84
|
-
"
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
"
|
|
88
|
-
|
|
89
|
-
"@vllnt/*"
|
|
90
|
-
],
|
|
91
|
-
"ignoredBuiltDependencies": [
|
|
92
|
-
"esbuild"
|
|
93
|
-
],
|
|
94
|
-
"overrides": {
|
|
95
|
-
"deepmerge-ts@<8.0.0": "8.0.2",
|
|
96
|
-
"handlebars@<4.7.10": "4.7.10"
|
|
97
|
-
}
|
|
98
|
-
}
|
|
82
|
+
"llm",
|
|
83
|
+
"ai-agents",
|
|
84
|
+
"typescript",
|
|
85
|
+
"edge-runtime"
|
|
86
|
+
]
|
|
99
87
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate an `llms.txt` file and serve pages as Markdown from Next.js App
|
|
3
|
+
* Router route handlers.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
* @see https://oss.vllnt.com/packages/next-llms
|
|
7
|
+
*/
|
|
1
8
|
export { generateLlmsText } from "./llms-text.js";
|
|
2
9
|
export { createMarkdownRoute } from "./markdown-route.js";
|
|
3
10
|
export type {
|
package/src/llms-text.ts
CHANGED
|
@@ -57,7 +57,9 @@ function renderSection(section: LlmsSection): string {
|
|
|
57
57
|
* prose, `## {section}` link lists, and a droppable `## Optional` section.
|
|
58
58
|
*
|
|
59
59
|
* @param config - The manifest content.
|
|
60
|
-
* @returns The `llms.txt` file contents
|
|
60
|
+
* @returns The `llms.txt` file contents. No trailing newline is added; a
|
|
61
|
+
* `details` value that ends with one keeps it when no section (from `sections`
|
|
62
|
+
* or `optional`) follows it.
|
|
61
63
|
* @throws {TypeError} When a title or link URL is blank, or a URL uses a
|
|
62
64
|
* `javascript:`, `data:` or `vbscript:` scheme. Line breaks inside titles,
|
|
63
65
|
* summaries and notes are collapsed to spaces, `[`, `]` and `\` in link titles
|
package/src/markdown-route.ts
CHANGED
|
@@ -9,13 +9,17 @@ const PLAIN_TEXT_HEADERS = {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* Split a URL pathname into slug segments, dropping empty segments (from `//`),
|
|
13
|
+
* then strip a trailing lowercase `.md` from the last raw segment, then
|
|
14
|
+
* URL-decode each segment. `/docs/intro.md` → `["docs", "intro"]`; `/` → `[]`.
|
|
14
15
|
*
|
|
15
16
|
* Returns `null` when the path cannot name a page safely: malformed
|
|
16
|
-
* percent-encoding,
|
|
17
|
-
* `/`, `\` or a NUL byte (so a
|
|
18
|
-
* handed a traversal sequence such
|
|
17
|
+
* percent-encoding, a segment left empty by stripping `.md` (`/docs/.md`), or a
|
|
18
|
+
* decoded segment that is `.`, `..`, or contains `/`, `\` or a NUL byte (so a
|
|
19
|
+
* resolver that maps slugs to files is never handed a traversal sequence such
|
|
20
|
+
* as `..%2F..%2Fsecret`). `new URL` already resolves `.` and `..` segments,
|
|
21
|
+
* including `%2E` forms, so a dot segment reaches this check only when
|
|
22
|
+
* stripping `.md` leaves one (`/docs/...md`, `/docs/%2E%2E.md`).
|
|
19
23
|
*/
|
|
20
24
|
function pathnameToSlug(pathname: string): string[] | null {
|
|
21
25
|
const segments = pathname.split("/").filter((segment) => segment.length > 0);
|
|
@@ -37,17 +41,21 @@ function pathnameToSlug(pathname: string): string[] | null {
|
|
|
37
41
|
* Create a Next.js App Router route handler that serves a slug's markdown
|
|
38
42
|
* content. Mount it at a catch-all route (`app/[...slug]/route.ts`). The handler
|
|
39
43
|
* is a pure Web-standard `(request) => Promise<Response>` — it imports nothing
|
|
40
|
-
* from `next` and
|
|
44
|
+
* from `next` and uses only Web-standard APIs.
|
|
41
45
|
*
|
|
42
|
-
* - Strips a trailing `.md` from the path (`/docs/intro.md`
|
|
43
|
-
* `["docs", "intro"]`).
|
|
46
|
+
* - Strips a trailing lowercase `.md` from the path (`/docs/intro.md`
|
|
47
|
+
* resolves the slug `["docs", "intro"]`).
|
|
44
48
|
* - Returns `200` with `Content-Type: text/markdown; charset=utf-8` and a
|
|
45
49
|
* `Cache-Control` header when the resolver returns a string.
|
|
46
50
|
* - Returns `404` (plain text) when the resolver returns `null` (or any
|
|
47
51
|
* non-string value, such as `undefined` from a missing map entry).
|
|
48
52
|
* - Returns `400` (plain text) without calling the resolver when the path has
|
|
49
|
-
* malformed percent-encoding
|
|
50
|
-
*
|
|
53
|
+
* malformed percent-encoding, a segment left empty by stripping `.md`
|
|
54
|
+
* (`/docs/.md`), or a decoded segment that is `.`, `..`, or contains `/`, `\`
|
|
55
|
+
* or a NUL byte. URL parsing resolves `.` and `..` segments first
|
|
56
|
+
* (`/docs/../x` gives `["x"]`), so a dot segment is rejected only when
|
|
57
|
+
* stripping `.md` leaves one (`/docs/...md`, `/docs/%2E%2E.md`). Empty
|
|
58
|
+
* segments from `//` are dropped, not rejected.
|
|
51
59
|
* - Sets `X-Content-Type-Options: nosniff` on every response.
|
|
52
60
|
*
|
|
53
61
|
* @param resolver - Maps a slug to its markdown, or `null` when not found.
|
|
@@ -60,8 +68,10 @@ function pathnameToSlug(pathname: string): string[] | null {
|
|
|
60
68
|
* // app/[...slug]/route.ts
|
|
61
69
|
* import { createMarkdownRoute } from "@vllnt/next-llms";
|
|
62
70
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
71
|
+
* const pages: Record<string, string> = { "docs/intro": "# Intro\n\nHello." };
|
|
72
|
+
*
|
|
73
|
+
* export const GET = createMarkdownRoute(
|
|
74
|
+
* (slug) => pages[slug.join("/")] ?? null,
|
|
65
75
|
* );
|
|
66
76
|
* ```
|
|
67
77
|
*/
|
package/src/types.ts
CHANGED
|
@@ -44,7 +44,9 @@ export type LlmsConfig = {
|
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Maps a route slug to its markdown content, or `null` if there is no page for
|
|
47
|
-
* the slug. The slug is the path segments
|
|
47
|
+
* the slug. The slug is the non-empty path segments, including any route
|
|
48
|
+
* prefix, with a trailing lowercase `.md` removed from the last raw segment,
|
|
49
|
+
* then each segment URL-decoded.
|
|
48
50
|
*/
|
|
49
51
|
export type MarkdownResolver = (
|
|
50
52
|
slug: string[],
|