Skip to content

Commit 35c9119

Browse files
committed
llm.txt generation (#94)
1 parent ac76c44 commit 35c9119

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"build:web": "lerna run build:docs,build:sb --concurrency 1",
1616
"test": "lerna run test --stream",
1717
"postinstall": "zx scripts/prepare.mjs",
18+
"llmtxt": "zx scripts/generate-llmtxt.mjs",
1819
"verify": "yarn lint && lerna run build,build:esm,test --stream"
1920
},
2021
"dependencies": {
@@ -24,6 +25,7 @@
2425
"@types/react": "18.2.66",
2526
"@types/react-dom": "18.2.22",
2627
"eslint": "^8.57.0",
28+
"front-matter": "^4.0.2",
2729
"lerna": "^8.1.3",
2830
"typedoc": "^0.25.13",
2931
"typedoc-plugin-markdown": "^3.17.1",

packages/docs/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ yarn-error.log*
2121

2222
docs/changelog/*.md
2323
docs/contributing/1-overview.mdx
24+
25+
static/llm-full.txt
26+
static/llms-full.txt
27+
static/llm.txt
28+
static/llms.txt
29+
static/llm

scripts/generate-llmtxt.mjs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env zx
2+
3+
import "zx/globals";
4+
import fm from "front-matter";
5+
6+
const prefix = "https://headless-tree.lukasbach.com/llm";
7+
const { entries: sb } = await fs.readJSON(path.join(__dirname, `../packages/sb-react/storybook-static/index.json`));
8+
const files = await glob([`./packages/docs/docs/**/*.mdx`, `./packages/docs/docs/**/*.md`]);
9+
const slugs = [];
10+
11+
let entrySmall = "# Headless Tree Documentation\n\n";
12+
let entryFull = `${entrySmall}`;
13+
14+
const consumeMdx = async (file) => {
15+
const content = await fs.readFile(file, { encoding: "utf8" });
16+
const { attributes, body } = fm(content);
17+
if (!attributes?.slug) return;
18+
19+
const bodyWithFixedLinks = body.replace(/\[(.*?)\]\((.*?)\)/g, (match, p1, p2) => {
20+
if (p2.startsWith("http")) return match;
21+
const matchedSlug = slugs.find(slug => p2 === slug || p2.startsWith(`${slug}#`));
22+
if (matchedSlug) {
23+
return `[${p1}](${prefix}${matchedSlug}.md)`;
24+
}
25+
return p1;
26+
});
27+
28+
// fix <DemoBox stories={["abc"]} /> to ```abc```
29+
const bodyWithFixedDemoBox = bodyWithFixedLinks.replace(/<DemoBox (.*?) \/>/g, (match, p1) => {
30+
const story = /initialStory="([^"]*)"/.exec(p1)?.[1] ?? /stories=\{\["([^"]*)"/.exec(p1)?.[1];
31+
const file = sb[story]?.importPath;
32+
if (!file) return "";
33+
const code = fs.readFileSync(path.join(__dirname, "../packages/sb-react", file), { encoding: "utf8" });
34+
return `\`\`\`ts jsx\n${code}\`\`\``;
35+
});
36+
37+
await fs.ensureDir(path.dirname(path.join(__dirname, "../packages/docs/static/llm", `${attributes.slug}.md`)));
38+
await fs.writeFile(path.join(__dirname, "../packages/docs/static/llm", `${attributes.slug}.md`), bodyWithFixedDemoBox, { encoding: "utf8" });
39+
40+
entryFull += `\n---\n<!-- ${attributes.slug } -->\n# ${attributes.title}\n\n${bodyWithFixedDemoBox}\n`;
41+
entrySmall += `\n- [${attributes.title}](${prefix}${attributes.slug}.md)${attributes.subtitle ? ": " : ""}${attributes.subtitle ?? ""}`;
42+
}
43+
44+
for (const file of files) {
45+
const content = await fs.readFile(file, { encoding: "utf8" });
46+
const { attributes } = fm(content);
47+
if (attributes?.slug) slugs.push(attributes.slug);
48+
}
49+
50+
51+
for (const file of files) {
52+
await consumeMdx(file);
53+
}
54+
await fs.writeFile(path.join(__dirname, "../packages/docs/static/llms.txt"), entrySmall, { encoding: "utf8" });
55+
await fs.writeFile(path.join(__dirname, "../packages/docs/static/llm.txt"), entrySmall, { encoding: "utf8" });
56+
await fs.writeFile(path.join(__dirname, "../packages/docs/static/llm-full.txt"), entryFull, { encoding: "utf8" });
57+
await fs.writeFile(path.join(__dirname, "../packages/docs/static/llms-full.txt"), entryFull, { encoding: "utf8" });

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20591,6 +20591,7 @@ __metadata:
2059120591
"@types/react": "npm:18.2.66"
2059220592
"@types/react-dom": "npm:18.2.22"
2059320593
eslint: "npm:^8.57.0"
20594+
front-matter: "npm:^4.0.2"
2059420595
lerna: "npm:^8.1.3"
2059520596
typedoc: "npm:^0.25.13"
2059620597
typedoc-plugin-markdown: "npm:^3.17.1"

0 commit comments

Comments
 (0)