|
1 | 1 | const { readdir, writeFile, stat } = require('fs/promises');
|
2 | 2 | const fs = require('fs').promises;
|
| 3 | +const path = require('path'); |
3 | 4 |
|
4 | 5 | const README_PATH = './README.md';
|
5 |
| - |
6 | 6 | const MKDOCS_PATH = 'mkdocs.yml';
|
| 7 | +const dishesFolder = 'dishes'; |
| 8 | +const starsystemFolder = 'starsystem'; |
7 | 9 |
|
8 | 10 | const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github'];
|
9 | 11 |
|
@@ -60,12 +62,77 @@ const categories = {
|
60 | 62 | },
|
61 | 63 | };
|
62 | 64 |
|
| 65 | +async function countStars(filename) { |
| 66 | + const data = await fs.readFile(filename, 'utf-8'); |
| 67 | + let stars = 0; |
| 68 | + const lines = data.split('\n'); |
| 69 | + lines.forEach(line => { |
| 70 | + stars += (line.match(/★/g) || []).length; |
| 71 | + }); |
| 72 | + return stars; |
| 73 | +} |
| 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 | + |
| 92 | + const dishesFolderAbs = path.resolve(dishesFolder); |
| 93 | + const starsystemFolderAbs = path.resolve(starsystemFolder); |
| 94 | + |
| 95 | + if (!await fs.access(starsystemFolderAbs).then(() => true).catch(() => false)) { |
| 96 | + await fs.mkdir(starsystemFolderAbs, { recursive: true }); |
| 97 | + } |
| 98 | + |
| 99 | + if (!await fs.access(dishesFolderAbs).then(() => true).catch(() => false)) { |
| 100 | + console.log(`Directory not found: ${dishesFolderAbs}, creating directory...`); |
| 101 | + await fs.mkdir(dishesFolderAbs, { recursive: true }); |
| 102 | + } |
| 103 | + |
| 104 | + await processFolder(dishesFolderAbs); |
| 105 | + |
| 106 | + const starRatings = Array.from(new Set(Object.values(dishes))).sort((a, b) => b - a); |
| 107 | + const navigationLinks = []; |
| 108 | + |
| 109 | + for (const stars of starRatings) { |
| 110 | + const starsFile = path.join(starsystemFolderAbs, `${stars}Star.md`); |
| 111 | + const content = [`# Dishes with ${stars} Stars`, '']; |
| 112 | + for (const [filepath, starCount] of Object.entries(dishes)) { |
| 113 | + if (starCount === stars) { |
| 114 | + const relativePath = path.relative(starsystemFolderAbs, filepath).replace(/\\/g, '/'); |
| 115 | + content.push(`* [${path.basename(filepath, '.md')}](./${relativePath})`); |
| 116 | + } |
| 117 | + } |
| 118 | + await writeFile(starsFile, content.join('\n'), 'utf-8'); |
| 119 | + navigationLinks.push(`- [${stars} Star Dishes](${path.relative(path.dirname(README_PATH), starsFile).replace(/\\/g, '/')})`); |
| 120 | + } |
| 121 | + |
| 122 | + return navigationLinks; |
| 123 | +} |
| 124 | + |
63 | 125 | async function main() {
|
64 | 126 | try {
|
65 |
| - let README_BEFORE = (README_MAIN = README_AFTER = ''); |
66 |
| - let MKDOCS_BEFORE = (MKDOCS_MAIN = MKDOCS_AFTER = ''); |
| 127 | + let README_BEFORE = '', README_MAIN = '', README_AFTER = ''; |
| 128 | + let MKDOCS_BEFORE = '', MKDOCS_MAIN = '', MKDOCS_AFTER = ''; |
67 | 129 | const markdownObj = await getAllMarkdown('.');
|
| 130 | + |
| 131 | + // Debug logging to understand the structure of markdownObj |
| 132 | + console.log("Markdown Object Structure:", JSON.stringify(markdownObj, null, 2)); |
| 133 | + |
68 | 134 | for (const markdown of markdownObj) {
|
| 135 | + console.log("Processing markdown:", markdown); |
69 | 136 | if (markdown.path.includes('tips/advanced')) {
|
70 | 137 | README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path);
|
71 | 138 | MKDOCS_AFTER += inlineMkdocsTemplate(markdown.file, markdown.path);
|
@@ -94,48 +161,62 @@ async function main() {
|
94 | 161 | MKDOCS_MAIN += categoryMkdocsTemplate(category.title, category.mkdocs);
|
95 | 162 | }
|
96 | 163 |
|
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"); |
| 164 | + let MKDOCS_TEMPLATE; |
| 165 | + let README_TEMPLATE; |
| 166 | + |
| 167 | + try { |
| 168 | + MKDOCS_TEMPLATE = await fs.readFile("./.github/templates/mkdocs_template.yml", "utf-8"); |
| 169 | + } catch (error) { |
| 170 | + MKDOCS_TEMPLATE = `site_name: My Docs\nnav:\n {{main}}\n`; |
| 171 | + console.warn("mkdocs_template.yml not found, using default template"); |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8"); |
| 176 | + } catch (error) { |
| 177 | + README_TEMPLATE = `# My Project\n\n{{before}}\n\n{{main}}\n\n{{after}}`; |
| 178 | + console.warn("readme_template.md not found, using default template"); |
| 179 | + } |
| 180 | + |
| 181 | + const navigationLinks = await organizeByStars(dishesFolder, starsystemFolder); |
| 182 | + // Debug logging to ensure navigationLinks is defined and contains data |
| 183 | + console.log("Navigation Links:", navigationLinks); |
| 184 | + const navigationSection = `\n## Navigation\n\n${navigationLinks.join('\n')}`; |
99 | 185 |
|
100 | 186 | await writeFile(
|
101 | 187 | README_PATH,
|
102 | 188 | README_TEMPLATE
|
103 | 189 | .replace('{{before}}', README_BEFORE.trim())
|
104 | 190 | .replace('{{main}}', README_MAIN.trim())
|
105 |
| - .replace('{{after}}', README_AFTER.trim()), |
| 191 | + .replace('{{after}}', README_AFTER.trim())+ navigationSection, |
106 | 192 | );
|
107 | 193 |
|
108 |
| - |
109 | 194 | await writeFile(
|
110 | 195 | MKDOCS_PATH,
|
111 | 196 | MKDOCS_TEMPLATE
|
112 | 197 | .replace('{{before}}', MKDOCS_BEFORE)
|
113 | 198 | .replace('{{main}}', MKDOCS_MAIN)
|
114 | 199 | .replace('{{after}}', MKDOCS_AFTER),
|
115 | 200 | );
|
| 201 | + |
| 202 | + // Organize files by star rating |
| 203 | + //await organizeByStars(dishesFolder, starsystemFolder); |
116 | 204 | } catch (error) {
|
117 | 205 | console.error(error);
|
118 | 206 | }
|
119 | 207 | }
|
120 | 208 |
|
121 |
| -async function getAllMarkdown(path) { |
| 209 | +async function getAllMarkdown(dir) { |
122 | 210 | const paths = [];
|
123 |
| - const files = await readdir(path); |
124 |
| - // chinese alphabetic order |
| 211 | + const files = await readdir(dir); |
125 | 212 | files.sort((a, b) => a.localeCompare(b, 'zh-CN'));
|
126 | 213 |
|
127 |
| - // mtime order |
128 |
| - // files.sort(async (a, b) => { |
129 |
| - // const aStat = await stat(`${path}/${a}`); |
130 |
| - // const bStat = await stat(`${path}/${b}`); |
131 |
| - // return aStat.mtime - bStat.mtime; |
132 |
| - // }); |
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')) {
|
138 |
| - paths.push({ path, file }); |
| 219 | + paths.push({ path: dir, file }); |
139 | 220 | } else if (fileStat.isDirectory()) {
|
140 | 221 | const subFiles = await getAllMarkdown(filePath);
|
141 | 222 | paths.push(...subFiles);
|
|
0 commit comments