Skip to content

Commit 9dfcee2

Browse files
committed
feat(llms-txt): write each page's Markdown and llms-full.txt
Two options: `writeMarkdown` writes the Markdown of every page at the `.md` URLs llms.txt links by default, and `writeFull` writes llms-full.txt, holding every page preceded by its URL. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 18a5d64 commit 9dfcee2

4 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎packages/react/src/llms-txt/generate.mjs‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import { populate } from '@doc-kit/core/utils/configuration/templates.mjs';
88
import { writeFile } from '@doc-kit/core/utils/file.mjs';
99

1010
import { buildApiDocLink } from './utils/buildApiDocLink.mjs';
11+
import { buildPages } from './utils/buildPages.mjs';
1112

1213
/**
13-
* Generates a llms.txt file
14+
* Generates a llms.txt file, and optionally the Markdown of every page
15+
* (`writeMarkdown`, at the `.md` URLs llms.txt links by default) and a
16+
* llms-full.txt file holding them all (`writeFull`).
1417
*
1518
* @type {import('./types').Generator['generate']}
1619
*/
@@ -26,8 +29,33 @@ export async function generate(input) {
2629

2730
const filledTemplate = `${populate(template, config)}${apiDocsLinks}`;
2831

29-
if (config.output) {
30-
await writeFile(join(config.output, 'llms.txt'), filledTemplate);
32+
if (!config.output) {
33+
return filledTemplate;
34+
}
35+
36+
await writeFile(join(config.output, 'llms.txt'), filledTemplate);
37+
38+
if (!config.writeMarkdown && !config.writeFull) {
39+
return filledTemplate;
40+
}
41+
42+
const pages = buildPages(input);
43+
44+
if (config.writeMarkdown) {
45+
await Promise.all(
46+
pages.map(({ path, markdown }) =>
47+
writeFile(join(config.output, `${path}.md`), markdown)
48+
)
49+
);
50+
}
51+
52+
if (config.writeFull) {
53+
const full = pages.map(
54+
({ path, markdown }) =>
55+
`---\nurl: ${populate(config.pageURL, { ...config, path })}\n---\n${markdown}`
56+
);
57+
58+
await writeFile(join(config.output, 'llms-full.txt'), full.join('\n'));
3159
}
3260

3361
return filledTemplate;

‎packages/react/src/llms-txt/index.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export default {
2121
defaultConfiguration: {
2222
templatePath: join(import.meta.dirname, 'template.txt'),
2323
pageURL: '{baseURL}{path}.md',
24+
writeMarkdown: false,
25+
writeFull: false,
2426
},
2527

2628
generate,

‎packages/react/src/llms-txt/types.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export type Generator = GeneratorMetadata<
44
{
55
templatePath: string;
66
pageURL: string;
7+
/** Write each page's Markdown at `{path}.md` */
8+
writeMarkdown: boolean;
9+
/** Write llms-full.txt, holding every page's Markdown */
10+
writeFull: boolean;
711
},
812
Generate<Array<MetadataEntry>, Promise<string>>
913
>;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getRemark, getRemarkMdx } from '@doc-kit/core/utils/remark.mjs';
2+
3+
/**
4+
* The Markdown of every page, from the content of its entries, in the order
5+
* of the input.
6+
*
7+
* @param {Array<import('@doc-kit/core/generators/metadata/types').MetadataEntry>} entries
8+
* @returns {Array<{ path: string, markdown: string }>}
9+
*/
10+
export const buildPages = entries => {
11+
/** @type {Map<string, Array<import('@doc-kit/core/generators/metadata/types').MetadataEntry>>} */
12+
const pages = new Map();
13+
14+
for (const entry of entries) {
15+
if (!entry.synthetic) {
16+
pages.set(entry.path, [...(pages.get(entry.path) ?? []), entry]);
17+
}
18+
}
19+
20+
return [...pages].map(([path, sections]) => {
21+
const remark = sections[0].mdx ? getRemarkMdx() : getRemark();
22+
23+
const markdown = sections
24+
.map(({ content }) => remark.stringify(content).trim())
25+
.filter(Boolean)
26+
.join('\n\n');
27+
28+
return { path, markdown: `${markdown}\n` };
29+
});
30+
};

0 commit comments

Comments
 (0)