86 lines
2.3 KiB
JavaScript
86 lines
2.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* 自动更新章节索引
|
|
* 用法: node update-index.js
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const DATA_DIR = path.join(__dirname, 'data');
|
|
const INDEX_FILE = path.join(DATA_DIR, 'chapters-index.json');
|
|
|
|
function main() {
|
|
const chapters = [];
|
|
let id = 1;
|
|
|
|
// 加载所有章节
|
|
while (true) {
|
|
const paddedId = id.toString().padStart(2, '0');
|
|
const files = [
|
|
path.join(DATA_DIR, `chapter-${id}.json`),
|
|
path.join(DATA_DIR, `chapter-${paddedId}.json`)
|
|
];
|
|
|
|
let data = null;
|
|
for (const file of files) {
|
|
try {
|
|
const content = fs.readFileSync(file, 'utf8');
|
|
data = JSON.parse(content);
|
|
break;
|
|
} catch (e) {}
|
|
}
|
|
|
|
if (!data) break;
|
|
|
|
// 计算字数(排除空格和换行)
|
|
const wordCount = (data.content || '').replace(/[\s\n]/g, '').length;
|
|
|
|
chapters.push({
|
|
id: data.id || id,
|
|
title: data.title || `第${id}章`,
|
|
subtitle: data.subtitle || '',
|
|
desc: data.desc || (data.content ? data.content.substring(0, 100) + '...' : ''),
|
|
wordCount: wordCount,
|
|
status: data.status || '已完结',
|
|
date: data.date || new Date().toISOString().split('T')[0]
|
|
});
|
|
|
|
id++;
|
|
}
|
|
|
|
// 检查小数章节
|
|
for (let i = 1; i <= id + 10; i++) {
|
|
const decimalId = i + 0.5;
|
|
const file = path.join(DATA_DIR, `chapter-${decimalId}.json`);
|
|
try {
|
|
const content = fs.readFileSync(file, 'utf8');
|
|
const data = JSON.parse(content);
|
|
const wordCount = (data.content || '').replace(/[\s\n]/g, '').length;
|
|
chapters.push({
|
|
id: data.id || decimalId,
|
|
title: data.title || `第${decimalId}章`,
|
|
subtitle: data.subtitle || '',
|
|
desc: data.desc || (data.content ? data.content.substring(0, 100) + '...' : ''),
|
|
wordCount: wordCount,
|
|
status: data.status || '已完结',
|
|
date: data.date || new Date().toISOString().split('T')[0]
|
|
});
|
|
} catch (e) {}
|
|
}
|
|
|
|
// 按id排序
|
|
chapters.sort((a, b) => parseFloat(a.id) - parseFloat(b.id));
|
|
|
|
// 写入索引文件
|
|
const index = {
|
|
total: chapters.length,
|
|
generated: new Date().toISOString(),
|
|
chapters: chapters
|
|
};
|
|
|
|
fs.writeFileSync(INDEX_FILE, JSON.stringify(index, null, 2));
|
|
console.log(`✅ 索引更新完成:${chapters.length} 章`);
|
|
}
|
|
|
|
main(); |