Skip to content

Commit cc267dd

Browse files
committed
Add llms-txt plugin to generate llms.txt
Introduce a Docusaurus plugin (src/plugins/llms-txt.ts) that generates llms.txt at postBuild by extracting docs (respecting sidebars and the 'current' version) and writing links using site URL. Register the plugin in docusaurus.config.ts with title, description and product mappings for turing/shio/dumont. Remove the now-static static/llms.txt file since the output is produced dynamically during the build.
1 parent fb42aef commit cc267dd

3 files changed

Lines changed: 133 additions & 75 deletions

File tree

docusaurus.config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ const config: Config = {
159159
},
160160
},
161161
],
162+
[
163+
require.resolve("./src/plugins/llms-txt"),
164+
{
165+
title: "Viglet Docs",
166+
description:
167+
"Documentation for Turing ES, Shio CMS and Dumont DEP",
168+
products: [
169+
{
170+
pluginId: "turing",
171+
label: "Turing ES - Enterprise Search with Generative AI",
172+
},
173+
{
174+
pluginId: "shio",
175+
label: "Shio CMS - Content Management System",
176+
},
177+
{
178+
pluginId: "dumont",
179+
label: "Dumont DEP - Data Entry Platform",
180+
},
181+
],
182+
},
183+
],
162184
],
163185

164186
themeConfig: {

src/plugins/llms-txt.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import type { Plugin, LoadContext } from "@docusaurus/types";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
5+
type DocInfo = {
6+
title: string;
7+
permalink: string;
8+
};
9+
10+
type ProductConfig = {
11+
pluginId: string;
12+
label: string;
13+
};
14+
15+
type PluginOptions = {
16+
title: string;
17+
description: string;
18+
products: ProductConfig[];
19+
};
20+
21+
function extractDocsFromSidebar(
22+
items: any[],
23+
docsById: Map<string, DocInfo>,
24+
): DocInfo[] {
25+
const result: DocInfo[] = [];
26+
for (const item of items) {
27+
if (item.type === "doc" || item.type === "ref") {
28+
const doc = docsById.get(item.id);
29+
if (doc) result.push(doc);
30+
} else if (item.type === "category") {
31+
if (item.link?.type === "doc") {
32+
const doc = docsById.get(item.link.id);
33+
if (doc) result.push(doc);
34+
}
35+
if (item.items) {
36+
result.push(...extractDocsFromSidebar(item.items, docsById));
37+
}
38+
}
39+
}
40+
return result;
41+
}
42+
43+
export default function llmsTxtPlugin(
44+
_context: LoadContext,
45+
options: PluginOptions,
46+
): Plugin {
47+
const productDocs = new Map<string, DocInfo[]>();
48+
49+
return {
50+
name: "llms-txt-plugin",
51+
52+
async allContentLoaded({ allContent }) {
53+
const docsContent =
54+
allContent["docusaurus-plugin-content-docs"] as Record<string, any>;
55+
if (!docsContent) return;
56+
57+
for (const product of options.products) {
58+
const content = docsContent[product.pluginId];
59+
if (!content?.loadedVersions) continue;
60+
61+
const currentVersion = content.loadedVersions.find(
62+
(v: any) => v.versionName === "current",
63+
);
64+
if (!currentVersion) continue;
65+
66+
const docsById = new Map<string, DocInfo>();
67+
for (const doc of currentVersion.docs) {
68+
docsById.set(doc.id, {
69+
title: doc.title,
70+
permalink: doc.permalink,
71+
});
72+
}
73+
74+
const allSidebarItems = Object.values(
75+
currentVersion.sidebars || {},
76+
).flat() as any[];
77+
78+
if (allSidebarItems.length > 0) {
79+
productDocs.set(
80+
product.pluginId,
81+
extractDocsFromSidebar(allSidebarItems, docsById),
82+
);
83+
} else {
84+
productDocs.set(product.pluginId, Array.from(docsById.values()));
85+
}
86+
}
87+
},
88+
89+
async postBuild({ outDir, siteConfig }) {
90+
const lines: string[] = [];
91+
lines.push(`# ${options.title}`);
92+
lines.push("");
93+
lines.push(`> ${options.description}`);
94+
95+
for (const product of options.products) {
96+
const docs = productDocs.get(product.pluginId);
97+
if (!docs || docs.length === 0) continue;
98+
99+
lines.push("");
100+
lines.push(`## ${product.label}`);
101+
lines.push("");
102+
for (const doc of docs) {
103+
lines.push(`- [${doc.title}](${siteConfig.url}${doc.permalink})`);
104+
}
105+
}
106+
107+
lines.push("");
108+
fs.writeFileSync(path.join(outDir, "llms.txt"), lines.join("\n"));
109+
},
110+
};
111+
}

static/llms.txt

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)