forked from zigcc/zigcc.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_md_to_smd.js
More file actions
157 lines (130 loc) · 4.15 KB
/
convert_md_to_smd.js
File metadata and controls
157 lines (130 loc) · 4.15 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
// 配置
const config = {
sourceDir: "./content/monthly",
author: "ZigCC",
time: "2024",
layout: "monthly.shtml",
};
// 转换 markdown frontmatter 到 smd 格式
function convertFrontmatter(content) {
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n/);
if (!frontmatterMatch) {
return content;
}
const frontmatter = frontmatterMatch[1];
const body = content.replace(/^---\n[\s\S]*?\n---\n/, "");
// 解析现有的 frontmatter
const lines = frontmatter.split("\n");
const metadata = {};
for (const line of lines) {
const match = line.match(/^(\w+):\s*(.+)$/);
if (match) {
metadata[match[1]] = match[2].replace(/^["']|["']$/g, ""); // 移除引号
}
}
let hasCustomFrontmatter = Object.keys(metadata).length > 3;
// 构建新的 smd frontmatter
const newFrontmatter = [
"---",
`.title = "${metadata.title || "Untitled"}",`,
`.date = @date("${metadata.date || config.time}"),`,
`.author = "${metadata.author || config.author}",`,
`.layout = "${config.layout}",`,
`.draft = false,`,
hasCustomFrontmatter?`.custom = {\n${Object.entries(metadata)
.filter(([k]) => !["title", "date", "author"].includes(k))
.map(([k, v]) => ` .${k} = "${v}",`)
.join("\n")}\n},\n---`:"---",
"",
].join("\n");
return newFrontmatter + body;
}
// 转换 markdown 链接格式
function convertLinks(content) {
// 转换 {{< ref "filename.md" >}} 格式到相对链接
content = content.replace(/\{\{<\s*ref\s+"([^"]+\.md)"\s*>\}\}/g, "[$1]($1)");
// 转换其他可能的 markdown 链接格式
// 这里可以根据需要添加更多转换规则
return content;
}
// 处理单个文件
function processFile(filePath) {
try {
const content = fs.readFileSync(filePath, "utf8");
const convertedContent = convertLinks(convertFrontmatter(content));
// 创建新的 smd 文件路径
const dir = path.dirname(filePath);
const basename = path.basename(filePath, ".md");
const newPath = path.join(dir, `${basename}.smd`);
// 写入新文件
fs.writeFileSync(newPath, convertedContent, "utf8");
console.log(`✓ Converted: ${filePath} -> ${newPath}`);
return true;
} catch (error) {
console.error(`✗ Error processing ${filePath}:`, error.message);
return false;
}
}
// 递归处理目录
function processDirectory(dirPath) {
try {
const items = fs.readdirSync(dirPath);
let successCount = 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);
successCount += result.success;
totalCount += result.total;
} else if (item.endsWith(".md")) {
// 处理 markdown 文件
const success = processFile(fullPath);
if (success) successCount++;
totalCount++;
}
}
return { success: successCount, total: totalCount };
} catch (error) {
console.error(`✗ Error processing directory ${dirPath}:`, error.message);
return { success: 0, total: 0 };
}
}
// 主函数
function main() {
console.log("🚀 Starting markdown to smd conversion...");
console.log(`📁 Source directory: ${config.sourceDir}`);
console.log(`👤 Author: ${config.author}`);
console.log(`📅 Time: ${config.time}`);
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(
`✓ Successfully converted: ${result.success}/${result.total} files`
);
if (result.success === result.total) {
console.log("🎉 All files converted successfully!");
} else {
console.log("⚠️ Some files failed to convert");
}
}
// 运行脚本
if (require.main === module) {
main();
}
module.exports = {
convertFrontmatter,
convertLinks,
processFile,
processDirectory,
};