|
| 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" }); |
0 commit comments