1
- const fs = require ( 'fs' ) ;
2
- const path = require ( 'path' ) ;
3
-
4
- const chaptersDirectory = './chapters' ;
5
- const indexFilePath = './index.md' ;
6
-
7
- function processChapter ( fileContent , chapterNumber ) {
8
- const lines = fileContent . split ( '\n' ) ;
9
- const processedLines = [ ] ;
10
-
11
- let currentSection = 1 ;
12
- let sectionStack = [ ] ;
13
-
14
- for ( const line of lines ) {
15
- if ( line . startsWith ( '#' ) ) {
16
- const hashCount = line . match ( / ^ # + / ) [ 0 ] . length ;
17
-
18
- while ( sectionStack . length >= hashCount ) {
19
- sectionStack . pop ( ) ;
20
- }
21
-
22
- const sectionNumber = [ ...sectionStack , currentSection ] . join ( '.' ) ;
23
- const newLine = line . replace ( / ^ # + / , `# ${ sectionNumber } ` ) ;
24
- processedLines . push ( newLine ) ;
25
-
26
- sectionStack . push ( currentSection ) ;
27
- currentSection = 1 ;
28
- } else {
29
- processedLines . push ( line ) ;
30
- }
31
- }
32
-
33
- return processedLines . join ( '\n' ) ;
34
- }
35
-
36
- function generateIndex ( ) {
37
- const chapterFiles = fs . readdirSync ( chaptersDirectory ) ;
38
-
39
- const indexLines = [ ] ;
40
-
41
- for ( let i = 0 ; i < chapterFiles . length ; i ++ ) {
42
- const chapterFile = chapterFiles [ i ] ;
43
- const chapterNumber = i + 1 ;
44
- const chapterFilePath = path . join ( chaptersDirectory , chapterFile ) ;
45
-
46
- const fileContent = fs . readFileSync ( chapterFilePath , 'utf8' ) ;
47
- const processedContent = processChapter ( fileContent , chapterNumber ) ;
48
-
49
- indexLines . push ( processedContent ) ;
50
- }
51
-
52
- fs . writeFileSync ( 'test.md' , indexLines . join ( '\n' ) ) ;
53
- console . log ( 'Index file generated successfully.' ) ;
54
- }
55
-
56
- generateIndex ( ) ;
1
+ // wip
0 commit comments