1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+
4+ function getPositionForItem ( itemPath ) {
5+ const stat = fs . statSync ( itemPath ) ;
6+ if ( stat . isDirectory ( ) ) {
7+ const categoryFile = path . join ( itemPath , '_category_.json' ) ;
8+ if ( fs . existsSync ( categoryFile ) ) {
9+ const category = JSON . parse ( fs . readFileSync ( categoryFile , 'utf8' ) ) ;
10+ return category . position || Infinity ;
11+ }
12+ return Infinity ;
13+ } else if ( itemPath . endsWith ( '.md' ) ) {
14+ const { frontmatter } = extractFrontmatterAndContent ( itemPath ) ;
15+ return getSidebarPosition ( frontmatter ) ;
16+ }
17+ return Infinity ;
18+ }
19+
20+ function extractFrontmatterAndContent ( filePath ) {
21+ const content = fs . readFileSync ( filePath , 'utf8' ) ;
22+ const frontmatterMatch = content . match ( / ^ - - - \n ( [ \s \S ] * ?) \n - - - \n ( [ \s \S ] * ) $ / ) ;
23+ if ( ! frontmatterMatch ) {
24+ return { frontmatter : '' , content : content } ;
25+ }
26+ const frontmatter = frontmatterMatch [ 1 ] ;
27+ const body = frontmatterMatch [ 2 ] ;
28+ return { frontmatter, content : body } ;
29+ }
30+
31+ function getSidebarPosition ( frontmatter ) {
32+ const match = frontmatter . match ( / s i d e b a r _ p o s i t i o n : \s * ( \d + ) / ) ;
33+ return match ? parseInt ( match [ 1 ] , 10 ) : Infinity ;
34+ }
35+
36+ function collectContent ( dirPath ) {
37+ const items = fs . readdirSync ( dirPath ) . map ( item => path . join ( dirPath , item ) ) ;
38+
39+ // Filter to directories and .md files
40+ const validItems = items . filter ( item => {
41+ const stat = fs . statSync ( item ) ;
42+ return stat . isDirectory ( ) || item . endsWith ( '.md' ) ;
43+ } ) ;
44+
45+ // Sort by position
46+ validItems . sort ( ( a , b ) => getPositionForItem ( a ) - getPositionForItem ( b ) ) ;
47+
48+ let combined = '' ;
49+ for ( const item of validItems ) {
50+ const stat = fs . statSync ( item ) ;
51+ if ( stat . isDirectory ( ) ) {
52+ combined += collectContent ( item ) ;
53+ } else if ( item . endsWith ( '.md' ) ) {
54+ const { content } = extractFrontmatterAndContent ( item ) ;
55+ combined += content + '\n\n' ;
56+ }
57+ }
58+
59+ return combined ;
60+ }
61+
62+ function main ( ) {
63+ const docsDir = path . join ( __dirname , '..' , 'docs' ) ;
64+ const buildDir = path . join ( __dirname , '..' , 'build' ) ;
65+ const outputFile = path . join ( buildDir , 'llms.txt' ) ;
66+
67+ const combinedContent = collectContent ( docsDir ) ;
68+
69+ fs . writeFileSync ( outputFile , combinedContent , 'utf8' ) ;
70+
71+ console . log ( 'llms.txt generated successfully' ) ;
72+ }
73+
74+ main ( ) ;
0 commit comments