国庆修改
This commit is contained in:
@@ -8,6 +8,8 @@ const __dirname = path.dirname(__filename);
|
||||
// 路径配置
|
||||
const NOTES_SOURCE_PATH = path.join(__dirname, '../public/mengyanote');
|
||||
const OUTPUT_PATH = path.join(__dirname, '../src/data');
|
||||
const PUBLIC_OUTPUT_PATH = path.join(__dirname, '../public/data');
|
||||
const IGNORE_FILE_PATH = path.join(NOTES_SOURCE_PATH, 'ignore.json');
|
||||
|
||||
// 文件节点类型
|
||||
const NODE_TYPES = {
|
||||
@@ -15,15 +17,31 @@ const NODE_TYPES = {
|
||||
FILE: 'file'
|
||||
};
|
||||
|
||||
// 读取忽略配置
|
||||
function loadIgnoreConfig() {
|
||||
try {
|
||||
if (fs.existsSync(IGNORE_FILE_PATH)) {
|
||||
const ignoreContent = fs.readFileSync(IGNORE_FILE_PATH, 'utf-8');
|
||||
const ignoreConfig = JSON.parse(ignoreContent);
|
||||
console.log('✓ 已加载忽略配置:', ignoreConfig.ignore);
|
||||
return ignoreConfig.ignore || [];
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('⚠️ 读取ignore.json失败,使用默认配置:', error.message);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
// 检查是否为Markdown文件
|
||||
function isMarkdownFile(filename) {
|
||||
return filename.toLowerCase().endsWith('.md');
|
||||
}
|
||||
|
||||
// 检查是否应该忽略的文件/文件夹
|
||||
function shouldIgnore(name) {
|
||||
const ignoredItems = ['.obsidian', '.trash', '.git', 'node_modules'];
|
||||
return ignoredItems.includes(name) || name.startsWith('.');
|
||||
function shouldIgnore(name, customIgnoreList = []) {
|
||||
const defaultIgnoredItems = ['.obsidian', '.trash', '.git', 'node_modules'];
|
||||
const allIgnoredItems = [...defaultIgnoredItems, ...customIgnoreList];
|
||||
return allIgnoredItems.includes(name) || name.startsWith('.');
|
||||
}
|
||||
|
||||
// 创建文件树节点
|
||||
@@ -39,13 +57,13 @@ function createNode(name, type, path, children = []) {
|
||||
}
|
||||
|
||||
// 递归读取目录结构
|
||||
function readDirectoryTree(dirPath, basePath = NOTES_SOURCE_PATH) {
|
||||
function readDirectoryTree(dirPath, basePath = NOTES_SOURCE_PATH, ignoreList = []) {
|
||||
try {
|
||||
const items = fs.readdirSync(dirPath, { withFileTypes: true });
|
||||
const children = [];
|
||||
|
||||
for (const item of items) {
|
||||
if (shouldIgnore(item.name)) {
|
||||
if (shouldIgnore(item.name, ignoreList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -53,7 +71,7 @@ function readDirectoryTree(dirPath, basePath = NOTES_SOURCE_PATH) {
|
||||
const relativePath = path.relative(basePath, itemPath).replace(/\\/g, '/');
|
||||
|
||||
if (item.isDirectory()) {
|
||||
const subChildren = readDirectoryTree(itemPath, basePath);
|
||||
const subChildren = readDirectoryTree(itemPath, basePath, ignoreList);
|
||||
if (subChildren.length > 0) {
|
||||
children.push(createNode(item.name, NODE_TYPES.FOLDER, relativePath, subChildren));
|
||||
}
|
||||
@@ -78,12 +96,12 @@ function readDirectoryTree(dirPath, basePath = NOTES_SOURCE_PATH) {
|
||||
}
|
||||
|
||||
// 读取所有Markdown文件内容
|
||||
function readAllMarkdownFiles(dirPath, basePath = NOTES_SOURCE_PATH, fileContents = {}) {
|
||||
function readAllMarkdownFiles(dirPath, basePath = NOTES_SOURCE_PATH, fileContents = {}, ignoreList = []) {
|
||||
try {
|
||||
const items = fs.readdirSync(dirPath, { withFileTypes: true });
|
||||
|
||||
for (const item of items) {
|
||||
if (shouldIgnore(item.name)) {
|
||||
if (shouldIgnore(item.name, ignoreList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -91,7 +109,7 @@ function readAllMarkdownFiles(dirPath, basePath = NOTES_SOURCE_PATH, fileContent
|
||||
const relativePath = path.relative(basePath, itemPath).replace(/\\/g, '/');
|
||||
|
||||
if (item.isDirectory()) {
|
||||
readAllMarkdownFiles(itemPath, basePath, fileContents);
|
||||
readAllMarkdownFiles(itemPath, basePath, fileContents, ignoreList);
|
||||
} else if (isMarkdownFile(item.name)) {
|
||||
try {
|
||||
const content = fs.readFileSync(itemPath, 'utf-8');
|
||||
@@ -124,22 +142,19 @@ function generateData() {
|
||||
if (!fs.existsSync(OUTPUT_PATH)) {
|
||||
fs.mkdirSync(OUTPUT_PATH, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(PUBLIC_OUTPUT_PATH)) {
|
||||
fs.mkdirSync(PUBLIC_OUTPUT_PATH, { recursive: true });
|
||||
}
|
||||
|
||||
// 加载忽略配置
|
||||
console.log('📋 加载忽略配置...');
|
||||
const ignoreList = loadIgnoreConfig();
|
||||
|
||||
console.log('📁 读取目录结构...');
|
||||
const directoryTree = readDirectoryTree(NOTES_SOURCE_PATH);
|
||||
const directoryTree = readDirectoryTree(NOTES_SOURCE_PATH, NOTES_SOURCE_PATH, ignoreList);
|
||||
|
||||
console.log('📄 读取Markdown文件内容...');
|
||||
const fileContents = readAllMarkdownFiles(NOTES_SOURCE_PATH);
|
||||
|
||||
// 生成目录树文件
|
||||
const treeOutputPath = path.join(OUTPUT_PATH, 'directoryTree.json');
|
||||
fs.writeFileSync(treeOutputPath, JSON.stringify(directoryTree, null, 2), 'utf-8');
|
||||
console.log(`✓ 目录树已生成: ${treeOutputPath}`);
|
||||
|
||||
// 生成文件内容文件
|
||||
const contentOutputPath = path.join(OUTPUT_PATH, 'fileContents.json');
|
||||
fs.writeFileSync(contentOutputPath, JSON.stringify(fileContents, null, 2), 'utf-8');
|
||||
console.log(`✓ 文件内容已生成: ${contentOutputPath}`);
|
||||
const fileContents = readAllMarkdownFiles(NOTES_SOURCE_PATH, NOTES_SOURCE_PATH, {}, ignoreList);
|
||||
|
||||
// 生成统计信息
|
||||
const stats = {
|
||||
@@ -149,10 +164,32 @@ function generateData() {
|
||||
sourceDirectory: NOTES_SOURCE_PATH
|
||||
};
|
||||
|
||||
// 生成到 src/data 目录
|
||||
const treeOutputPath = path.join(OUTPUT_PATH, 'directoryTree.json');
|
||||
const contentOutputPath = path.join(OUTPUT_PATH, 'fileContents.json');
|
||||
const statsOutputPath = path.join(OUTPUT_PATH, 'stats.json');
|
||||
|
||||
fs.writeFileSync(treeOutputPath, JSON.stringify(directoryTree, null, 2), 'utf-8');
|
||||
fs.writeFileSync(contentOutputPath, JSON.stringify(fileContents, null, 2), 'utf-8');
|
||||
fs.writeFileSync(statsOutputPath, JSON.stringify(stats, null, 2), 'utf-8');
|
||||
|
||||
console.log(`✓ 目录树已生成: ${treeOutputPath}`);
|
||||
console.log(`✓ 文件内容已生成: ${contentOutputPath}`);
|
||||
console.log(`✓ 统计信息已生成: ${statsOutputPath}`);
|
||||
|
||||
// 生成到 public/data 目录
|
||||
const publicTreeOutputPath = path.join(PUBLIC_OUTPUT_PATH, 'directoryTree.json');
|
||||
const publicContentOutputPath = path.join(PUBLIC_OUTPUT_PATH, 'fileContents.json');
|
||||
const publicStatsOutputPath = path.join(PUBLIC_OUTPUT_PATH, 'stats.json');
|
||||
|
||||
fs.writeFileSync(publicTreeOutputPath, JSON.stringify(directoryTree, null, 2), 'utf-8');
|
||||
fs.writeFileSync(publicContentOutputPath, JSON.stringify(fileContents, null, 2), 'utf-8');
|
||||
fs.writeFileSync(publicStatsOutputPath, JSON.stringify(stats, null, 2), 'utf-8');
|
||||
|
||||
console.log(`✓ 目录树已生成到public: ${publicTreeOutputPath}`);
|
||||
console.log(`✓ 文件内容已生成到public: ${publicContentOutputPath}`);
|
||||
console.log(`✓ 统计信息已生成到public: ${publicStatsOutputPath}`);
|
||||
|
||||
console.log('🎉 数据生成完成!');
|
||||
console.log(`📊 统计信息:`);
|
||||
console.log(` - 文件数量: ${stats.totalFiles}`);
|
||||
|
||||
Reference in New Issue
Block a user