forked from zigcc/zigcc.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_link_format.js
More file actions
107 lines (90 loc) · 3 KB
/
convert_link_format.js
File metadata and controls
107 lines (90 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// 配置
const config = {
sourceDir: './content/'
};
// 转换链接格式
function convertLinkFormat(content) {
// 将 $link.page('filename') 转换为 $link.sub('filename')
content = content.replace(/\$link\.page\('([^']+)'\)/g, (match, filename) => {
return `$link.sub('${filename}')`;
});
return content;
}
// 处理单个文件
function processFile(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const convertedContent = convertLinkFormat(content);
// 如果内容有变化,写回文件
if (content !== convertedContent) {
fs.writeFileSync(filePath, convertedContent, 'utf8');
console.log(`✓ Converted links in: ${filePath}`);
return true;
} else {
console.log(`- No changes needed: ${filePath}`);
return false;
}
} catch (error) {
console.error(`✗ Error processing ${filePath}:`, error.message);
return false;
}
}
// 递归处理目录
function processDirectory(dirPath) {
try {
const items = fs.readdirSync(dirPath);
let convertedCount = 0;
let totalCount = 0;
for (const item of items) {
const fullPath = path.join(dirPath, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
// 递归处理子目录
const result = processDirectory(fullPath);
convertedCount += result.converted;
totalCount += result.total;
} else if (item.endsWith('.smd')) {
// 处理 smd 文件
const converted = processFile(fullPath);
if (converted) convertedCount++;
totalCount++;
}
}
return { converted: convertedCount, total: totalCount };
} catch (error) {
console.error(`✗ Error processing directory ${dirPath}:`, error.message);
return { converted: 0, total: 0 };
}
}
// 主函数
function main() {
console.log('🔄 Starting link format conversion...');
console.log(`📁 Source directory: ${config.sourceDir}`);
console.log('🔄 Converting $link.page to $link.sub');
console.log('');
if (!fs.existsSync(config.sourceDir)) {
console.error(`✗ Source directory does not exist: ${config.sourceDir}`);
process.exit(1);
}
const result = processDirectory(config.sourceDir);
console.log('');
console.log('📊 Conversion Summary:');
console.log(`✓ Converted links in: ${result.converted}/${result.total} files`);
if (result.converted > 0) {
console.log('🎉 Link format conversion completed!');
} else {
console.log('ℹ️ No link format conversion needed');
}
}
// 运行脚本
if (require.main === module) {
main();
}
module.exports = {
convertLinkFormat,
processFile,
processDirectory
};