2022-02-26 19:05:53 +09:00
|
|
|
const { readdir, writeFile, stat } = require('fs/promises');
|
2022-03-07 03:28:45 +08:00
|
|
|
const fs = require('fs').promises;
|
2024-06-06 18:02:26 +01:00
|
|
|
const path = require('path');
|
2022-02-26 19:05:53 +09:00
|
|
|
|
2022-03-04 22:23:17 +09:00
|
|
|
const README_PATH = './README.md';
|
|
|
|
const MKDOCS_PATH = 'mkdocs.yml';
|
2024-06-06 18:02:26 +01:00
|
|
|
const dishesFolder = 'dishes';
|
|
|
|
const starsystemFolder = 'starsystem';
|
2022-03-04 22:23:17 +09:00
|
|
|
|
2022-02-26 19:05:53 +09:00
|
|
|
const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github'];
|
|
|
|
|
|
|
|
const categories = {
|
2022-03-04 22:23:17 +09:00
|
|
|
vegetable_dish: {
|
2022-03-02 08:08:33 +08:00
|
|
|
title: '素菜',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 19:05:53 +09:00
|
|
|
},
|
2022-03-04 22:23:17 +09:00
|
|
|
meat_dish: {
|
2022-03-02 08:08:33 +08:00
|
|
|
title: '荤菜',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-03-02 08:08:33 +08:00
|
|
|
},
|
2022-03-05 00:19:54 +08:00
|
|
|
aquatic: {
|
|
|
|
title: '水产',
|
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
|
|
|
},
|
2022-02-26 19:05:53 +09:00
|
|
|
breakfast: {
|
|
|
|
title: '早餐',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 19:05:53 +09:00
|
|
|
},
|
|
|
|
staple: {
|
|
|
|
title: '主食',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 19:05:53 +09:00
|
|
|
},
|
|
|
|
'semi-finished': {
|
|
|
|
title: '半成品加工',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 19:05:53 +09:00
|
|
|
},
|
|
|
|
soup: {
|
|
|
|
title: '汤与粥',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 19:05:53 +09:00
|
|
|
},
|
|
|
|
drink: {
|
|
|
|
title: '饮料',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 20:56:53 +09:00
|
|
|
},
|
|
|
|
condiment: {
|
|
|
|
title: '酱料和其它材料',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 19:05:53 +09:00
|
|
|
},
|
|
|
|
dessert: {
|
|
|
|
title: '甜品',
|
2022-03-04 22:23:17 +09:00
|
|
|
readme: '',
|
|
|
|
mkdocs: '',
|
2022-02-26 19:05:53 +09:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-06-06 18:02:26 +01:00
|
|
|
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);
|
|
|
|
|
2024-06-06 17:12:40 +00:00
|
|
|
const starRatings = Array.from(new Set(Object.values(dishes))).sort((a, b) => a - b);
|
2024-06-06 18:02:26 +01:00
|
|
|
const navigationLinks = [];
|
|
|
|
|
|
|
|
for (const stars of starRatings) {
|
|
|
|
const starsFile = path.join(starsystemFolderAbs, `${stars}Star.md`);
|
2024-06-06 17:12:40 +00:00
|
|
|
const content = [`# ${stars} 星难度菜品`, ''];
|
2024-06-06 18:02:26 +01:00
|
|
|
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');
|
2024-06-06 17:12:40 +00:00
|
|
|
navigationLinks.push(`- [${stars} 星难度](${path.relative(path.dirname(README_PATH), starsFile).replace(/\\/g, '/')})`);
|
2024-06-06 18:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return navigationLinks;
|
|
|
|
}
|
|
|
|
|
2022-02-26 19:05:53 +09:00
|
|
|
async function main() {
|
|
|
|
try {
|
2024-06-06 18:02:26 +01:00
|
|
|
let README_BEFORE = '', README_MAIN = '', README_AFTER = '';
|
|
|
|
let MKDOCS_BEFORE = '', MKDOCS_MAIN = '', MKDOCS_AFTER = '';
|
2022-02-26 19:05:53 +09:00
|
|
|
const markdownObj = await getAllMarkdown('.');
|
2024-06-06 18:02:26 +01:00
|
|
|
|
|
|
|
// Debug logging to understand the structure of markdownObj
|
|
|
|
console.log("Markdown Object Structure:", JSON.stringify(markdownObj, null, 2));
|
|
|
|
|
2022-02-26 19:05:53 +09:00
|
|
|
for (const markdown of markdownObj) {
|
2024-06-06 18:02:26 +01:00
|
|
|
console.log("Processing markdown:", markdown);
|
2022-02-26 19:05:53 +09:00
|
|
|
if (markdown.path.includes('tips/advanced')) {
|
2022-03-04 22:23:17 +09:00
|
|
|
README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path);
|
|
|
|
MKDOCS_AFTER += inlineMkdocsTemplate(markdown.file, markdown.path);
|
2022-02-26 19:05:53 +09:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-04 22:23:17 +09:00
|
|
|
|
2022-02-26 19:05:53 +09:00
|
|
|
if (markdown.path.includes('tips')) {
|
2022-03-04 22:23:17 +09:00
|
|
|
README_BEFORE += inlineReadmeTemplate(markdown.file, markdown.path);
|
|
|
|
MKDOCS_BEFORE += inlineMkdocsTemplate(markdown.file, markdown.path);
|
2022-02-26 19:05:53 +09:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const category of Object.keys(categories)) {
|
2022-03-04 22:23:17 +09:00
|
|
|
if (!markdown.path.includes(category)) continue;
|
|
|
|
categories[category].readme += inlineReadmeTemplate(markdown.file, markdown.path);
|
|
|
|
categories[category].mkdocs += inlineMkdocsTemplate(
|
|
|
|
markdown.file,
|
|
|
|
markdown.path,
|
|
|
|
true,
|
|
|
|
);
|
2022-02-26 19:05:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const category of Object.values(categories)) {
|
2022-03-04 22:23:17 +09:00
|
|
|
README_MAIN += categoryReadmeTemplate(category.title, category.readme);
|
|
|
|
MKDOCS_MAIN += categoryMkdocsTemplate(category.title, category.mkdocs);
|
2022-02-26 19:05:53 +09:00
|
|
|
}
|
2022-02-26 19:31:36 +09:00
|
|
|
|
2024-06-06 18:02:26 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-06-06 17:12:40 +00:00
|
|
|
console.log("难度索引", navigationLinks);
|
|
|
|
const navigationSection = `\n### 按难度索引\n\n${navigationLinks.join('\n')}`;
|
2022-03-07 03:28:45 +08:00
|
|
|
|
2022-02-27 15:51:37 +09:00
|
|
|
await writeFile(
|
2022-03-04 22:23:17 +09:00
|
|
|
README_PATH,
|
2022-03-07 03:28:45 +08:00
|
|
|
README_TEMPLATE
|
|
|
|
.replace('{{before}}', README_BEFORE.trim())
|
2024-06-06 17:12:40 +00:00
|
|
|
.replace('{{index_stars}}', navigationSection.trim())
|
2022-03-07 03:28:45 +08:00
|
|
|
.replace('{{main}}', README_MAIN.trim())
|
2024-06-06 17:12:40 +00:00
|
|
|
.replace('{{after}}', README_AFTER.trim()),
|
2022-03-04 22:23:17 +09:00
|
|
|
);
|
|
|
|
|
|
|
|
await writeFile(
|
|
|
|
MKDOCS_PATH,
|
2022-03-07 03:28:45 +08:00
|
|
|
MKDOCS_TEMPLATE
|
|
|
|
.replace('{{before}}', MKDOCS_BEFORE)
|
2022-03-04 22:23:17 +09:00
|
|
|
.replace('{{main}}', MKDOCS_MAIN)
|
|
|
|
.replace('{{after}}', MKDOCS_AFTER),
|
2022-02-27 15:51:37 +09:00
|
|
|
);
|
2024-06-06 18:02:26 +01:00
|
|
|
|
|
|
|
// Organize files by star rating
|
|
|
|
//await organizeByStars(dishesFolder, starsystemFolder);
|
2022-02-26 19:05:53 +09:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 18:02:26 +01:00
|
|
|
async function getAllMarkdown(dir) {
|
2022-02-26 19:05:53 +09:00
|
|
|
const paths = [];
|
2024-06-06 18:02:26 +01:00
|
|
|
const files = await readdir(dir);
|
2022-02-27 15:51:37 +09:00
|
|
|
files.sort((a, b) => a.localeCompare(b, 'zh-CN'));
|
|
|
|
|
2022-02-26 19:05:53 +09:00
|
|
|
for (const file of files) {
|
2024-06-06 18:02:26 +01:00
|
|
|
const filePath = path.join(dir, file);
|
2022-02-26 19:05:53 +09:00
|
|
|
if (ignorePaths.includes(file)) continue;
|
|
|
|
const fileStat = await stat(filePath);
|
|
|
|
if (fileStat.isFile() && file.endsWith('.md')) {
|
2024-06-06 18:02:26 +01:00
|
|
|
paths.push({ path: dir, file });
|
2022-02-26 19:05:53 +09:00
|
|
|
} else if (fileStat.isDirectory()) {
|
|
|
|
const subFiles = await getAllMarkdown(filePath);
|
|
|
|
paths.push(...subFiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2022-03-04 22:23:17 +09:00
|
|
|
function inlineReadmeTemplate(file, path) {
|
2022-03-04 00:39:06 +08:00
|
|
|
return `- [${file.replace('.md', '')}](${path}/${file})\n`;
|
2022-02-26 19:05:53 +09:00
|
|
|
}
|
|
|
|
|
2022-03-04 22:23:17 +09:00
|
|
|
function categoryReadmeTemplate(title, inlineStr) {
|
2022-02-26 19:05:53 +09:00
|
|
|
return `\n### ${title}\n\n${inlineStr}`;
|
|
|
|
}
|
|
|
|
|
2022-03-04 22:23:17 +09:00
|
|
|
function inlineMkdocsTemplate(file, path, isDish = false) {
|
|
|
|
return `${' '.repeat(isDish ? 10 : 6)}- ${file.replace('.md', '')}: ${path}/${file}\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function categoryMkdocsTemplate(title, inlineStr) {
|
|
|
|
return `\n${' '.repeat(6)}- ${title}:\n${inlineStr}`;
|
|
|
|
}
|
|
|
|
|
2022-02-26 19:05:53 +09:00
|
|
|
main();
|