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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| #!/usr/bin/env node
const fs = require('fs'); const path = require('path');
const DRAFTS_DIR = path.join(process.cwd(), 'source', '_drafts'); const POSTS_DIR = path.join(process.cwd(), 'source', '_posts');
const alertTypeMap = { '提醒内容': 'info', '建议内容': 'success', '重要内容': 'primary', '警告内容': 'warning', '注意内容': 'danger', 'NOTE': 'info', 'TIP': 'success', 'IMPORTANT': 'primary', 'WARNING': 'warning', 'CAUTION': 'danger' };
function convertTyporaToHexoFluid(content) { let convertedContent = convertTyporaAlerts(content); convertedContent = convertTyporaTips(convertedContent); return convertedContent; }
function convertTyporaAlerts(content) { const typoraAlertRegex = />\s*\[!(NOTE|TIP|WARNING|IMPORTANT|CAUTION)\]([\s\S]*?)(?=>\s*\[!|\n\s*\n|$)/g; return content.replace(typoraAlertRegex, (match, alertType, alertContent) => { const noteType = alertTypeMap[alertType] || 'info'; const cleanedContent = alertContent .split('\n') .map(line => line.replace(/^\s*>\s?/, '')) .join('\n') .trim(); return `{% note ${noteType} %}\n${cleanedContent}\n{% endnote %}`; }); }
function convertTyporaTips(content) { const typoCnTipRegex = />\s*(提醒内容|建议内容|重要内容|警告内容|注意内容)[::]([\s\S]*?)(?=>\s*(?:提醒内容|建议内容|重要内容|警告内容|注意内容)[::]|\n\s*\n|$)/g; return content.replace(typoCnTipRegex, (match, tipType, tipContent) => { const noteType = alertTypeMap[tipType] || 'info'; const cleanedContent = tipContent .split('\n') .map(line => line.replace(/^\s*>\s?/, '')) .join('\n') .trim(); return `{% note ${noteType} %}\n${cleanedContent}\n{% endnote %}`; }); }
function ensureFrontMatter(content, fileName) { if (content.startsWith('---\n')) { return content; } const title = path.basename(fileName, path.extname(fileName)) .replace(/-/g, ' ') .replace(/\b\w/g, l => l.toUpperCase()); const now = new Date(); const dateStr = now.toISOString().split('T')[0]; const timeStr = now.toTimeString().split(' ')[0]; const frontMatter = `--- title: ${title} date: ${dateStr} ${timeStr} tags: categories: ---
`;
return frontMatter + content; }
function processDraft(fileName) { const draftPath = path.join(DRAFTS_DIR, fileName); const postPath = path.join(POSTS_DIR, fileName); try { console.log(`处理草稿: ${fileName}`); let content = fs.readFileSync(draftPath, 'utf8'); content = ensureFrontMatter(content, fileName); const convertedContent = convertTyporaToHexoFluid(content); if (!fs.existsSync(POSTS_DIR)) { fs.mkdirSync(POSTS_DIR, { recursive: true }); } fs.writeFileSync(postPath, convertedContent, 'utf8'); console.log(`✓ 已发布: ${fileName} (draft → post)`); return true; } catch (error) { console.error(`❌ 处理文件时出错: ${fileName}`); console.error(` - ${error.message}`); return false; } }
function ensureDirectoryExists(dir) { if (!fs.existsSync(dir)) { console.log(`创建目录: ${dir}`); fs.mkdirSync(dir, { recursive: true }); return true; } return false; }
function main() { ensureDirectoryExists(DRAFTS_DIR); ensureDirectoryExists(POSTS_DIR); const args = process.argv.slice(2); let targetFiles = []; if (args.length > 0) { targetFiles = args.map(file => { return file.endsWith('.md') ? file : `${file}.md`; }); } else { try { targetFiles = fs.readdirSync(DRAFTS_DIR) .filter(file => file.endsWith('.md')); } catch (error) { console.error(`❌ 无法读取草稿目录: ${DRAFTS_DIR}`); console.error(error); process.exit(1); } } if (targetFiles.length === 0) { console.log(`⚠️ 没有找到要处理的Markdown文件(在 ${DRAFTS_DIR})`); return; } console.log(''); console.log('🚀 开始将草稿转换为发布文章'); console.log(`📂 草稿目录: ${DRAFTS_DIR}`); console.log(`📂 发布目录: ${POSTS_DIR}`); console.log(`📄 要处理的文件: ${targetFiles.length}个`); console.log(''); let successCount = 0; let failCount = 0; targetFiles.forEach(fileName => { if (processDraft(fileName)) { successCount++; } else { failCount++; } }); console.log(''); console.log('✅ 转换完成!'); console.log(` - 成功: ${successCount}个文件`); if (failCount > 0) { console.log(` - 失败: ${failCount}个文件`); } console.log(''); console.log('💡 提示: 现在可以运行 "hexo g" 来生成站点'); console.log(''); }
main();
|