@@ -5,6 +5,10 @@ const README_PATH = './README.md';
5
5
6
6
const MKDOCS_PATH = 'mkdocs.yml' ;
7
7
8
+ const dishesFolder = 'dishes' ;
9
+
10
+ const starsystemFolder = 'starsystem' ;
11
+
8
12
const ignorePaths = [ '.git' , 'README.md' , 'node_modules' , 'CONTRIBUTING.md' , '.github' ] ;
9
13
10
14
const categories = {
@@ -59,18 +63,78 @@ const categories = {
59
63
mkdocs : '' ,
60
64
} ,
61
65
} ;
66
+ async function countStars ( filename ) {
67
+ const data = await fs . readFile ( filename , 'utf-8' ) ;
68
+ let stars = 0 ;
69
+ const lines = data . split ( '\n' ) ;
70
+ lines . forEach ( line => {
71
+ stars += ( line . match ( / ★ / g) || [ ] ) . length ;
72
+ } ) ;
73
+ return stars ;
74
+ }
75
+ async function organizeByStars ( dishesFolder , starsystemFolder ) {
76
+ const dishes = { } ;
77
+
78
+ async function processFolder ( folderPath ) {
79
+ const files = await readdir ( folderPath ) ;
80
+ for ( const filename of files ) {
81
+ const filepath = path . join ( folderPath , filename ) ;
82
+ const fileStat = await stat ( filepath ) ;
83
+ if ( fileStat . isFile ( ) && filename . endsWith ( '.md' ) ) {
84
+ const stars = await countStars ( filepath ) ;
85
+ dishes [ filepath ] = stars ;
86
+ } else if ( fileStat . isDirectory ( ) ) {
87
+ await processFolder ( filepath ) ;
88
+ }
89
+ }
90
+ }
91
+ const dishesFolderAbs = path . resolve ( dishesFolder ) ;
92
+ const starsystemFolderAbs = path . resolve ( starsystemFolder ) ;
93
+
94
+ if ( ! await fs . access ( starsystemFolderAbs ) . then ( ( ) => true ) . catch ( ( ) => false ) ) {
95
+ await fs . mkdir ( starsystemFolderAbs , { recursive : true } ) ;
96
+ }
97
+
98
+ if ( ! await fs . access ( dishesFolderAbs ) . then ( ( ) => true ) . catch ( ( ) => false ) ) {
99
+ console . log ( `Directory not found: ${ dishesFolderAbs } , creating directory...` ) ;
100
+ await fs . mkdir ( dishesFolderAbs , { recursive : true } ) ;
101
+ }
102
+ await processFolder ( dishesFolderAbs ) ;
103
+
104
+ const starRatings = Array . from ( new Set ( Object . values ( dishes ) ) ) . sort ( ( a , b ) => b - a ) ;
105
+ const navigationLinks = [ ] ;
106
+
107
+ for ( const stars of starRatings ) {
108
+ const starsFile = path . join ( starsystemFolderAbs , `${ stars } Star.md` ) ;
109
+ const content = [ `# Dishes with ${ stars } Stars` , '' ] ;
110
+ for ( const [ filepath , starCount ] of Object . entries ( dishes ) ) {
111
+ if ( starCount === stars ) {
112
+ const relativePath = path . relative ( starsystemFolderAbs , filepath ) . replace ( / \\ / g, '/' ) ;
113
+ content . push ( `* [${ path . basename ( filepath , '.md' ) } ](./${ relativePath } )` ) ;
114
+ }
115
+ }
116
+ await writeFile ( starsFile , content . join ( '\n' ) , 'utf-8' ) ;
117
+ navigationLinks . push ( `- [${ stars } Star Dishes](${ path . relative ( path . dirname ( README_PATH ) , starsFile ) . replace ( / \\ / g, '/' ) } )` ) ;
118
+ }
119
+
120
+ return navigationLinks ;
121
+ }
62
122
63
123
async function main ( ) {
64
124
try {
65
125
let README_BEFORE = ( README_MAIN = README_AFTER = '' ) ;
66
126
let MKDOCS_BEFORE = ( MKDOCS_MAIN = MKDOCS_AFTER = '' ) ;
67
127
const markdownObj = await getAllMarkdown ( '.' ) ;
128
+ // Debug logging to understand the structure of markdownObj
129
+ console . log ( "Markdown Object Structure:" , JSON . stringify ( markdownObj , null , 2 ) ) ;
130
+
68
131
for ( const markdown of markdownObj ) {
69
132
if ( markdown . path . includes ( 'tips/advanced' ) ) {
70
133
README_AFTER += inlineReadmeTemplate ( markdown . file , markdown . path ) ;
71
134
MKDOCS_AFTER += inlineMkdocsTemplate ( markdown . file , markdown . path ) ;
72
135
continue ;
73
136
}
137
+
74
138
75
139
if ( markdown . path . includes ( 'tips' ) ) {
76
140
README_BEFORE += inlineReadmeTemplate ( markdown . file , markdown . path ) ;
@@ -93,10 +157,27 @@ async function main() {
93
157
README_MAIN += categoryReadmeTemplate ( category . title , category . readme ) ;
94
158
MKDOCS_MAIN += categoryMkdocsTemplate ( category . title , category . mkdocs ) ;
95
159
}
160
+ let MKDOCS_TEMPLATE ;
161
+ let README_TEMPLATE ;
162
+ try {
163
+ MKDOCS_TEMPLATE = await fs . readFile ( "./.github/templates/mkdocs_template.yml" , "utf-8" ) ;
164
+ } catch ( error ) {
165
+ MKDOCS_TEMPLATE = `site_name: My Docs\nnav:\n {{main}}\n` ;
166
+ console . warn ( "mkdocs_template.yml not found, using default template" ) ;
167
+ }
96
168
97
- const MKDOCS_TEMPLATE = await fs . readFile ( "./.github/templates/mkdocs_template.yml" , "utf-8" ) ;
98
- const README_TEMPLATE = await fs . readFile ( "./.github/templates/readme_template.md" , "utf-8" ) ;
169
+ try {
170
+ README_TEMPLATE = await fs . readFile ( "./.github/templates/readme_template.md" , "utf-8" ) ;
171
+ } catch ( error ) {
172
+ README_TEMPLATE = `# My Project\n\n{{before}}\n\n{{main}}\n\n{{after}}` ;
173
+ console . warn ( "readme_template.md not found, using default template" ) ;
174
+ }
175
+ const navigationLinks = await organizeByStars ( dishesFolder , starsystemFolder ) ;
176
+ // Debug logging to ensure navigationLinks is defined and contains data
177
+ console . log ( "Navigation Links:" , navigationLinks ) ;
178
+ const navigationSection = `\n## Navigation\n\n${ navigationLinks . join ( '\n' ) } ` ;
99
179
180
+
100
181
await writeFile (
101
182
README_PATH ,
102
183
README_TEMPLATE
@@ -118,9 +199,9 @@ async function main() {
118
199
}
119
200
}
120
201
121
- async function getAllMarkdown ( path ) {
202
+ async function getAllMarkdown ( dir ) {
122
203
const paths = [ ] ;
123
- const files = await readdir ( path ) ;
204
+ const files = await readdir ( dir ) ;
124
205
// chinese alphabetic order
125
206
files . sort ( ( a , b ) => a . localeCompare ( b , 'zh-CN' ) ) ;
126
207
@@ -131,7 +212,7 @@ async function getAllMarkdown(path) {
131
212
// return aStat.mtime - bStat.mtime;
132
213
// });
133
214
for ( const file of files ) {
134
- const filePath = ` ${ path } / ${ file } ` ;
215
+ const filePath = path . join ( dir , file ) ;
135
216
if ( ignorePaths . includes ( file ) ) continue ;
136
217
const fileStat = await stat ( filePath ) ;
137
218
if ( fileStat . isFile ( ) && file . endsWith ( '.md' ) ) {
0 commit comments