diff --git a/.github/readme-generate.js b/.github/readme-generate.js index ceee1672a0..eb8d610b14 100644 --- a/.github/readme-generate.js +++ b/.github/readme-generate.js @@ -5,6 +5,10 @@ const README_PATH = './README.md'; const MKDOCS_PATH = 'mkdocs.yml'; +const dishesFolder = 'dishes'; + +const starsystemFolder = 'starsystem'; + const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github']; const categories = { @@ -59,18 +63,78 @@ const categories = { mkdocs: '', }, }; +async function countStars(filename) { + const data = await fs.readFile(filename, 'utf-8'); + let stars = 0; + const lines = data.split('\n'); + lines.forEach(line => { + stars += (line.match(/★/g) || []).length; + }); + return stars; +} +async function organizeByStars(dishesFolder, starsystemFolder) { + const dishes = {}; + + async function processFolder(folderPath) { + const files = await readdir(folderPath); + for (const filename of files) { + const filepath = path.join(folderPath, filename); + const fileStat = await stat(filepath); + if (fileStat.isFile() && filename.endsWith('.md')) { + const stars = await countStars(filepath); + dishes[filepath] = stars; + } else if (fileStat.isDirectory()) { + await processFolder(filepath); + } + } + } + const dishesFolderAbs = path.resolve(dishesFolder); + const starsystemFolderAbs = path.resolve(starsystemFolder); + + if (!await fs.access(starsystemFolderAbs).then(() => true).catch(() => false)) { + await fs.mkdir(starsystemFolderAbs, { recursive: true }); + } + + if (!await fs.access(dishesFolderAbs).then(() => true).catch(() => false)) { + console.log(`Directory not found: ${dishesFolderAbs}, creating directory...`); + await fs.mkdir(dishesFolderAbs, { recursive: true }); + } + await processFolder(dishesFolderAbs); + + const starRatings = Array.from(new Set(Object.values(dishes))).sort((a, b) => b - a); + const navigationLinks = []; + + for (const stars of starRatings) { + const starsFile = path.join(starsystemFolderAbs, `${stars}Star.md`); + const content = [`# Dishes with ${stars} Stars`, '']; + for (const [filepath, starCount] of Object.entries(dishes)) { + if (starCount === stars) { + const relativePath = path.relative(starsystemFolderAbs, filepath).replace(/\\/g, '/'); + content.push(`* [${path.basename(filepath, '.md')}](./${relativePath})`); + } + } + await writeFile(starsFile, content.join('\n'), 'utf-8'); + navigationLinks.push(`- [${stars} Star Dishes](${path.relative(path.dirname(README_PATH), starsFile).replace(/\\/g, '/')})`); + } + + return navigationLinks; +} async function main() { try { let README_BEFORE = (README_MAIN = README_AFTER = ''); let MKDOCS_BEFORE = (MKDOCS_MAIN = MKDOCS_AFTER = ''); const markdownObj = await getAllMarkdown('.'); + // Debug logging to understand the structure of markdownObj + console.log("Markdown Object Structure:", JSON.stringify(markdownObj, null, 2)); + for (const markdown of markdownObj) { if (markdown.path.includes('tips/advanced')) { README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path); MKDOCS_AFTER += inlineMkdocsTemplate(markdown.file, markdown.path); continue; } + if (markdown.path.includes('tips')) { README_BEFORE += inlineReadmeTemplate(markdown.file, markdown.path); @@ -93,10 +157,27 @@ async function main() { README_MAIN += categoryReadmeTemplate(category.title, category.readme); MKDOCS_MAIN += categoryMkdocsTemplate(category.title, category.mkdocs); } + let MKDOCS_TEMPLATE; + let README_TEMPLATE; + try { + MKDOCS_TEMPLATE = await fs.readFile("./.github/templates/mkdocs_template.yml", "utf-8"); + } catch (error) { + MKDOCS_TEMPLATE = `site_name: My Docs\nnav:\n {{main}}\n`; + console.warn("mkdocs_template.yml not found, using default template"); + } - const MKDOCS_TEMPLATE = await fs.readFile("./.github/templates/mkdocs_template.yml", "utf-8"); - const README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8"); + try { + README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8"); + } catch (error) { + README_TEMPLATE = `# My Project\n\n{{before}}\n\n{{main}}\n\n{{after}}`; + console.warn("readme_template.md not found, using default template"); + } + const navigationLinks = await organizeByStars(dishesFolder, starsystemFolder); + // Debug logging to ensure navigationLinks is defined and contains data + console.log("Navigation Links:", navigationLinks); + const navigationSection = `\n## Navigation\n\n${navigationLinks.join('\n')}`; + await writeFile( README_PATH, README_TEMPLATE @@ -118,9 +199,9 @@ async function main() { } } -async function getAllMarkdown(path) { +async function getAllMarkdown(dir) { const paths = []; - const files = await readdir(path); + const files = await readdir(dir); // chinese alphabetic order files.sort((a, b) => a.localeCompare(b, 'zh-CN')); @@ -131,7 +212,7 @@ async function getAllMarkdown(path) { // return aStat.mtime - bStat.mtime; // }); for (const file of files) { - const filePath = `${path}/${file}`; + const filePath = path.join(dir, file); if (ignorePaths.includes(file)) continue; const fileStat = await stat(filePath); if (fileStat.isFile() && file.endsWith('.md')) {