File size: 2,411 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import path from 'path';
import fs from 'fs-extra';
import { glob } from 'glob';

async function generateLLms() {
  const cwd = process.cwd();
  const siteDir = path.resolve(cwd, '_site');
  const docsDir = ['components', 'docs'];

  const matchSuffix = '.en-US.md';

  // Ensure siteDir
  await fs.ensureDir(siteDir);

  const docs = await glob(`{${docsDir.join(',')}}/**/*.md`);
  const filteredDocs = docs.filter((doc) => doc.includes(matchSuffix));

  const docsIndex: Array<{ title: string; url: string }> = [];
  const docsBody: string[] = [];

  for (const markdown of filteredDocs) {
    const mdPath = path.join(cwd, markdown);

    const fsContent = (await fs.readFile(mdPath, 'utf-8')).trim();

    // e.g. title: Button -> Button
    const title = fsContent.match(/title:\s*(.*)/)?.[1].trim();

    if (!title) {
      console.log('MISS title, ignore:', mdPath);
      continue;
    }

    // URL
    let url = `https://ant.design/${markdown.replace(matchSuffix, '')}`;
    if (url.includes('/components/')) {
      url = url.replace('/index', '');
    }

    // Docs: title
    docsIndex.push({
      title,
      url,
    });

    // Docs: content
    const parsedContent = fsContent.replace(/^---[\s\S]*?---\n/, '').trim();

    const fullContent = [
      // Title
      '---',
      `Title: ${title}`,
      `URL: ${url}`,
      '---',
      '',
      // Content
      parsedContent,
      '',
    ].join('\n');

    docsBody.push(fullContent);
  }
  const docsIndexContent = [
    '# Ant Design - Enterprise-class React UI library',
    '',
    '- Ant Design, developed by Ant Group, is a React UI library that 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.',
    '',
    '## Docs',
    '',
    ...docsIndex.map(({ title, url }) => `- [${title}](${url})`),
    '',
  ].join('\n');

  const docsBodyContent = docsBody.join('\n');

  await fs.writeFile(path.join(siteDir, 'llms.txt'), docsIndexContent);
  await fs.writeFile(path.join(siteDir, 'llms-full.txt'), docsBodyContent);
  console.log('Generated llms.txt and llms-full.txt');
}
(async () => {
  if (require.main === module) {
    await generateLLms();
  }
})().catch((e) => {
  console.error(e);
  process.exit(1);
});