feat: init
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
// 格式化 md 内容
|
||||
// 1. 如果内容开头不是一级标题,或者二级标题,或者是一级,二级标题,那么就获取到文件名,然后在 md 内容开头添加一个二级标题,例如:"## 文件名" + 换行 + 正文内容
|
||||
// 2. 如果内容开头是一级标题,或者二级标题,那么就不做任何处理
|
||||
// 3. 脚本接受相对目录参数,例如:node scripts/formatMdContent.js ./docs/guide,递归处理所有 .md
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function formatMdContent(content, filePath) {
|
||||
// 检查内容是否以一级或二级标题开始
|
||||
const firstLine = content.trim().split("\n")[0];
|
||||
const hasTitle = /^#\s|^##\s/.test(firstLine);
|
||||
|
||||
if (!hasTitle) {
|
||||
// 获取文件名(不含扩展名)
|
||||
const fileName = path.basename(filePath, path.extname(filePath));
|
||||
// 添加二级标题
|
||||
return `## ${fileName}\n\n${content}`;
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
function processDirectory(dirPath) {
|
||||
// 获取绝对路径
|
||||
const absolutePath = path.resolve(process.cwd(), dirPath);
|
||||
|
||||
// 读取目录下的所有文件
|
||||
const files = fs.readdirSync(absolutePath);
|
||||
|
||||
files.forEach((file) => {
|
||||
const filePath = path.join(absolutePath, file);
|
||||
const stat = fs.statSync(filePath);
|
||||
|
||||
if (stat.isFile() && path.extname(file) === ".md") {
|
||||
// 读取并处理 markdown 文件
|
||||
const content = fs.readFileSync(filePath, "utf8");
|
||||
const formattedContent = formatMdContent(content, filePath);
|
||||
|
||||
// 写回文件
|
||||
fs.writeFileSync(filePath, formattedContent, "utf8");
|
||||
console.log(`已处理: ${file}`);
|
||||
} else if (stat.isDirectory()) {
|
||||
// 递归处理子目录
|
||||
processDirectory(filePath);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 获取命令行参数
|
||||
const targetDir = process.argv[2];
|
||||
|
||||
if (!targetDir) {
|
||||
console.error("请提供目标目录路径");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
processDirectory(targetDir);
|
||||
console.log("所有 Markdown 文件处理完成");
|
||||
} catch (error) {
|
||||
console.error("处理过程中出错:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -22,7 +22,7 @@ function genReadme(directory) {
|
||||
// 递归生成 Markdown 内容
|
||||
function generateContent(directory, dirName) {
|
||||
let content = `# ${dirName}\n\n`;
|
||||
content += `> 八股文一网打尽,更多面试题请看[程序员面试刷题神器 - 面试鸭](https://www.mianshiya.com/)\n\n`;
|
||||
content += `> 你全面的 AI 知识库,一网打尽最新 AI 资讯,都在 [https://ai.codefather.cn](https://ai.codefather.cn)\n\n`;
|
||||
|
||||
// 处理当前目录下的 Markdown 文件
|
||||
const files = getFilesInDirectory(directory);
|
||||
@@ -58,7 +58,7 @@ function generateContent(directory, dirName) {
|
||||
}
|
||||
|
||||
// 添加底部内容
|
||||
content += `> 八股文一网打尽,更多面试题请看[程序员面试刷题神器 - 面试鸭](https://www.mianshiya.com/)\n\n`;
|
||||
content += `> 你全面的 AI 知识库,一网打尽最新 AI 资讯,都在 [https://ai.codefather.cn](https://ai.codefather.cn)\n\n`;
|
||||
|
||||
return content;
|
||||
}
|
||||
@@ -69,12 +69,22 @@ function getSubDirectories(directory) {
|
||||
return items.filter((item) => item.isDirectory()).map((dir) => path.join(directory, dir.name));
|
||||
}
|
||||
|
||||
// 获取目录下的所有 Markdown 文件
|
||||
// 递归获取目录下的所有 Markdown 文件
|
||||
function getFilesInDirectory(directory) {
|
||||
const items = fs.readdirSync(directory, { withFileTypes: true });
|
||||
return items
|
||||
.filter((item) => item.isFile() && path.extname(item.name) === ".md")
|
||||
.map((file) => path.join(directory, file.name));
|
||||
let files = [];
|
||||
|
||||
for (const item of items) {
|
||||
const fullPath = path.join(directory, item.name);
|
||||
if (item.isDirectory()) {
|
||||
// 递归获取子目录中的文件
|
||||
files = files.concat(getFilesInDirectory(fullPath));
|
||||
} else if (item.isFile() && path.extname(item.name) === ".md") {
|
||||
files.push(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
// 从命令行参数获取目标目录
|
||||
|
||||
Reference in New Issue
Block a user