mirror of
https://github.com/Anduin2017/HowToCook.git
synced 2026-05-18 13:21:27 +08:00
refactor: streamline README generation by removing star index section and unused mkdocs references
This commit is contained in:
120
.github/readme-generate.js
vendored
120
.github/readme-generate.js
vendored
@@ -3,174 +3,79 @@ const fs = require('fs').promises;
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const README_PATH = './README.md';
|
const README_PATH = './README.md';
|
||||||
const MKDOCS_PATH = 'properdocs.yml';
|
|
||||||
const dishesFolder = 'dishes';
|
|
||||||
const starsystemFolder = 'starsystem';
|
|
||||||
|
|
||||||
const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github', 'en', 'site'];
|
const ignorePaths = ['.git', 'README.md', 'node_modules', 'CONTRIBUTING.md', '.github', 'en', 'site'];
|
||||||
|
|
||||||
const categories = {
|
const categories = {
|
||||||
vegetable_dish: {
|
vegetable_dish: {
|
||||||
title: '素菜',
|
title: '素菜',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
meat_dish: {
|
meat_dish: {
|
||||||
title: '荤菜',
|
title: '荤菜',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
aquatic: {
|
aquatic: {
|
||||||
title: '水产',
|
title: '水产',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
breakfast: {
|
breakfast: {
|
||||||
title: '早餐',
|
title: '早餐',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
staple: {
|
staple: {
|
||||||
title: '主食',
|
title: '主食',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
'semi-finished': {
|
'semi-finished': {
|
||||||
title: '半成品加工',
|
title: '半成品加工',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
soup: {
|
soup: {
|
||||||
title: '汤与粥',
|
title: '汤与粥',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
drink: {
|
drink: {
|
||||||
title: '饮料',
|
title: '饮料',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
condiment: {
|
condiment: {
|
||||||
title: '酱料和其它材料',
|
title: '酱料和其它材料',
|
||||||
readme: '',
|
readme: '',
|
||||||
mkdocs: '',
|
|
||||||
},
|
},
|
||||||
dessert: {
|
dessert: {
|
||||||
title: '甜品',
|
title: '甜品',
|
||||||
readme: '',
|
readme: '',
|
||||||
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) => a - b);
|
|
||||||
const navigationLinks = [];
|
|
||||||
|
|
||||||
for (const stars of starRatings) {
|
|
||||||
const starsFile = path.join(starsystemFolderAbs, `${stars}Star.md`);
|
|
||||||
const content = [`# ${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} 星难度](${path.relative(path.dirname(README_PATH), starsFile).replace(/\\/g, '/')})`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return navigationLinks;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
let README_BEFORE = '', README_MAIN = '', README_AFTER = '';
|
let README_BEFORE = '', README_MAIN = '', README_AFTER = '';
|
||||||
let MKDOCS_BEFORE = '', MKDOCS_MAIN = '', MKDOCS_AFTER = '';
|
|
||||||
const markdownObj = await getAllMarkdown('.');
|
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) {
|
for (const markdown of markdownObj) {
|
||||||
console.log("Processing markdown:", markdown);
|
|
||||||
if (markdown.path.includes('tips/advanced')) {
|
if (markdown.path.includes('tips/advanced')) {
|
||||||
README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path);
|
README_AFTER += inlineReadmeTemplate(markdown.file, markdown.path);
|
||||||
MKDOCS_AFTER += inlineMkdocsTemplate(markdown.file, markdown.path);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (markdown.path.includes('tips')) {
|
if (markdown.path.includes('tips')) {
|
||||||
README_BEFORE += inlineReadmeTemplate(markdown.file, markdown.path);
|
README_BEFORE += inlineReadmeTemplate(markdown.file, markdown.path);
|
||||||
MKDOCS_BEFORE += inlineMkdocsTemplate(markdown.file, markdown.path);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const category of Object.keys(categories)) {
|
for (const category of Object.keys(categories)) {
|
||||||
if (!markdown.path.includes(category)) continue;
|
if (!markdown.path.includes(category)) continue;
|
||||||
categories[category].readme += inlineReadmeTemplate(markdown.file, markdown.path);
|
categories[category].readme += inlineReadmeTemplate(markdown.file, markdown.path);
|
||||||
categories[category].mkdocs += inlineMkdocsTemplate(
|
|
||||||
markdown.file,
|
|
||||||
markdown.path,
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const category of Object.values(categories)) {
|
for (const category of Object.values(categories)) {
|
||||||
README_MAIN += categoryReadmeTemplate(category.title, category.readme);
|
README_MAIN += categoryReadmeTemplate(category.title, category.readme);
|
||||||
MKDOCS_MAIN += categoryMkdocsTemplate(category.title, category.mkdocs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let MKDOCS_TEMPLATE;
|
|
||||||
let README_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 {
|
try {
|
||||||
README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8");
|
README_TEMPLATE = await fs.readFile("./.github/templates/readme_template.md", "utf-8");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -178,30 +83,13 @@ async function main() {
|
|||||||
console.warn("readme_template.md not found, using default template");
|
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("难度索引", navigationLinks);
|
|
||||||
const navigationSection = `\n### 按难度索引\n\n${navigationLinks.join('\n')}`;
|
|
||||||
|
|
||||||
await writeFile(
|
await writeFile(
|
||||||
README_PATH,
|
README_PATH,
|
||||||
README_TEMPLATE
|
README_TEMPLATE
|
||||||
.replace('{{before}}', README_BEFORE.trim())
|
.replace('{{before}}', README_BEFORE.trim())
|
||||||
.replace('{{index_stars}}', navigationSection.trim())
|
|
||||||
.replace('{{main}}', README_MAIN.trim())
|
.replace('{{main}}', README_MAIN.trim())
|
||||||
.replace('{{after}}', README_AFTER.trim()),
|
.replace('{{after}}', README_AFTER.trim()),
|
||||||
);
|
);
|
||||||
|
|
||||||
await writeFile(
|
|
||||||
MKDOCS_PATH,
|
|
||||||
MKDOCS_TEMPLATE
|
|
||||||
.replace('{{before}}', MKDOCS_BEFORE)
|
|
||||||
.replace('{{main}}', MKDOCS_MAIN)
|
|
||||||
.replace('{{after}}', MKDOCS_AFTER),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Organize files by star rating
|
|
||||||
//await organizeByStars(dishesFolder, starsystemFolder);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
@@ -234,12 +122,4 @@ function categoryReadmeTemplate(title, inlineStr) {
|
|||||||
return `\n### ${title}\n\n${inlineStr}`;
|
return `\n### ${title}\n\n${inlineStr}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function inlineMkdocsTemplate(file, path, isDish = false) {
|
|
||||||
return `${' '.repeat(isDish ? 10 : 6)}- ${path}/${file}\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function categoryMkdocsTemplate(title, inlineStr) {
|
|
||||||
return `\n${' '.repeat(6)}- ${title}:\n${inlineStr}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|||||||
2
.github/templates/readme_template.md
vendored
2
.github/templates/readme_template.md
vendored
@@ -33,8 +33,6 @@ docker run -d -p 5000:5000 aiursoft/howtocookviewer
|
|||||||
|
|
||||||
## 菜谱
|
## 菜谱
|
||||||
|
|
||||||
{{index_stars}}
|
|
||||||
|
|
||||||
{{main}}
|
{{main}}
|
||||||
|
|
||||||
## 进阶知识学习
|
## 进阶知识学习
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,4 +6,3 @@ site/
|
|||||||
|
|
||||||
.idea
|
.idea
|
||||||
*.iml
|
*.iml
|
||||||
mkdocs.yml
|
|
||||||
@@ -46,14 +46,6 @@ docker run -d -p 5000:5000 aiursoft/howtocookviewer
|
|||||||
|
|
||||||
## 菜谱
|
## 菜谱
|
||||||
|
|
||||||
### 按难度索引
|
|
||||||
|
|
||||||
- [1 星难度](starsystem/1Star.md)
|
|
||||||
- [2 星难度](starsystem/2Star.md)
|
|
||||||
- [3 星难度](starsystem/3Star.md)
|
|
||||||
- [4 星难度](starsystem/4Star.md)
|
|
||||||
- [5 星难度](starsystem/5Star.md)
|
|
||||||
|
|
||||||
### 素菜
|
### 素菜
|
||||||
|
|
||||||
- [拔丝土豆](dishes/vegetable_dish/拔丝土豆/拔丝土豆.md)
|
- [拔丝土豆](dishes/vegetable_dish/拔丝土豆/拔丝土豆.md)
|
||||||
|
|||||||
Reference in New Issue
Block a user