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