Skip to content

Commit 165b963

Browse files
authored
docs: add script to generate llm.txt (#9281)
1 parent a1b16a9 commit 165b963

5 files changed

Lines changed: 148 additions & 0 deletions

File tree

angular.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"site/doc/404.html",
3434
"site/doc/google854eb8b183564acb.html",
3535
"site/doc/robots.txt",
36+
"site/doc/llms.txt",
37+
"site/doc/llms-full.txt",
3638
{
3739
"glob": "**/*",
3840
"input": "node_modules/@ant-design/icons-angular/src/inline-svg/",

package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"format": "prettier --config .prettierrc.js --list-different \\\"component/**/*{.ts}\\\"",
1919
"stage-release": "ts-node --project scripts/release/tsconfig.json scripts/release/release.ts",
2020
"gulp": "gulp",
21+
"llms": "tsx scripts/generate-llms.ts",
2122
"lint": "npm run lint:script && npm run lint:style",
2223
"lint:script": "eslint --cache",
2324
"lint:style": "stylelint components/**/*.less",
@@ -131,6 +132,7 @@
131132
"terser": "^5.43.0",
132133
"ts-node": "^10.9.1",
133134
"tslib": "^2.0.3",
135+
"tsx": "^4.20.3",
134136
"typescript": "~5.8.2",
135137
"typescript-eslint": "^8.37.0",
136138
"yaml-front-matter": "^4.1.1",

scripts/generate-llms.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Use of this source code is governed by an MIT-style license that can be
3+
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
4+
*/
5+
6+
import * as fs from 'fs-extra';
7+
import { glob } from 'glob';
8+
9+
import { resolve, join } from 'path';
10+
11+
/**
12+
* @see https://github.com/ant-design/ant-design/blob/master/scripts/generate-llms.ts
13+
*/
14+
export async function generateLLms(): Promise<void> {
15+
const cwd = process.cwd();
16+
const siteDir = resolve(cwd, 'site', 'doc');
17+
const docsDir = ['components', 'docs'];
18+
19+
const matchSuffix = '.en-US.md';
20+
21+
// Ensure siteDir
22+
await fs.ensureDir(siteDir);
23+
24+
const docs = await glob(`{${docsDir.join(',')}}/**/*.md`);
25+
const ignoreDocs = ['changelog', 'join', 'migration', 'recommendation'];
26+
const filteredDocs = docs.filter(doc => doc.includes(matchSuffix) && !ignoreDocs.some(title => doc.includes(title)));
27+
28+
const docsIndex: Array<{ title: string; url: string }> = [];
29+
const docsBody: string[] = [];
30+
31+
for (const markdown of filteredDocs) {
32+
const mdPath = join(cwd, markdown);
33+
34+
const fsContent = (await fs.readFile(mdPath, 'utf-8')).trim();
35+
36+
// e.g. title: Button -> Button
37+
const title = fsContent.match(/title:\s*(.*)/)?.[1].trim();
38+
39+
if (!title) {
40+
console.log('MISS title, ignore:', mdPath);
41+
continue;
42+
}
43+
44+
// URL
45+
let url = `https://ng.ant.design/${markdown.replace(matchSuffix, '')}/en`;
46+
if (url.includes('/components/')) {
47+
url = url.replace('/doc/index', '');
48+
}
49+
50+
// Docs: title
51+
docsIndex.push({
52+
title,
53+
url
54+
});
55+
56+
// Docs: content
57+
const parsedContent = fsContent.replace(/^---[\s\S]*?---\n/, '').trim();
58+
59+
const fullContent = [
60+
// Title
61+
'---',
62+
`Title: ${title}`,
63+
`URL: ${url}`,
64+
'---',
65+
'',
66+
// Content
67+
parsedContent,
68+
''
69+
].join('\n');
70+
71+
docsBody.push(fullContent);
72+
}
73+
const docsIndexContent = [
74+
'# ng-zorro-antd - Ant Design of Angular',
75+
'',
76+
'- An enterprise-class Angular UI component library based on Ant Design, which aims to provide a high-quality design language and development framework for enterprise-level backend management systems. It offers a rich set of components and design guidelines, helping developers build modern, responsive, and high-performance web applications.\n' +
77+
'',
78+
'## Docs',
79+
'',
80+
...docsIndex.map(({ title, url }) => `- [${title}](${url})`),
81+
''
82+
].join('\n');
83+
84+
const docsBodyContent = docsBody.join('\n');
85+
86+
await fs.writeFile(join(siteDir, 'llms.txt'), docsIndexContent);
87+
await fs.writeFile(join(siteDir, 'llms-full.txt'), docsBodyContent);
88+
console.log('Generated llms.txt and llms-full.txt');
89+
}
90+
91+
(async () => {
92+
if (require.main === module) {
93+
await generateLLms();
94+
}
95+
})().catch(e => {
96+
console.error(e);
97+
process.exit(1);
98+
});

scripts/gulp/tasks/site.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { debounce } from 'lodash';
1111
import { join } from 'path';
1212

1313
import { buildConfig } from '../../build-config';
14+
import { generateLLms } from '../../generate-llms';
1415
import { generate } from '../../prerender/ngsw-config';
1516
import { generateSitemap } from '../../prerender/sitemap';
1617
import { execNodeTask, execTask } from '../util/task-helpers';
@@ -50,6 +51,7 @@ task('watch:site', () => {
5051
task('init:site', async done => {
5152
siteGenerate('init');
5253
await themeGenerate();
54+
await generateLLms();
5355
done();
5456
});
5557

0 commit comments

Comments
 (0)