From 6e549d6b494b6bfb28d131d43e1d88bd5e7ced80 Mon Sep 17 00:00:00 2001 From: Frost-ZX Date: Mon, 13 Oct 2025 00:23:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E6=80=9D=E6=BA=90?= =?UTF-8?q?=E7=AC=94=E8=AE=B0=E8=BD=AC=E6=8D=A2=E8=84=9A=E6=9C=AC=EF=BC=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=20Markdown=20=E6=96=87=E4=BB=B6=E7=9A=84=20f?= =?UTF-8?q?rontmatter=EF=BC=8C=E7=BB=99=20title=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=BC=95=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/convert-siyuan-notes.mjs | 79 +++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/scripts/convert-siyuan-notes.mjs b/scripts/convert-siyuan-notes.mjs index 9d402a0..057f232 100644 --- a/scripts/convert-siyuan-notes.mjs +++ b/scripts/convert-siyuan-notes.mjs @@ -5,6 +5,82 @@ import { fileURLToPath } from 'node:url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); +/** + * @description 处理 Markdown 文件的 frontmatter + * - 给 title 属性值加上引号 + * @param {string} filePath Markdown 文件路径 + */ +function convertFrontmatter(filePath) { + try { + + // 读取文件内容 + let contentOld = readFileSync(filePath, { encoding: 'utf-8' }); + + // 匹配 frontmatter(介于 --- 之间的内容) + let frontmatterRegExp = /^---\n([\s\S]*?)\n---/; + let frontmatterMatched = contentOld.match(frontmatterRegExp); + let frontmatterStr = frontmatterMatched ? frontmatterMatched[1] : ''; + + if (!frontmatterStr) { + console.warn(`文件 ${filePath} 没有找到 frontmatter`); + return; + } + + /** @type {Record} */ + let frontmatterInfo = {}; + + let frontmatterLines = frontmatterStr.split('\n').map((line) => { + return line.trim(); + }).filter((line) => { + return line !== ''; + }); + + for (let line of frontmatterLines) { + + // 跳过注释行 + if (line.startsWith('#')) { + continue; + } + + // 匹配 key: value 格式 + let matched = line.match(/^(\w+):\s*(.*)$/); + + if (!matched) { + continue; + } + + let key = matched[1]; + let value = matched[2]; + + frontmatterInfo[key] = value; + + } + + let titleInfo = frontmatterInfo['title']; + + // 标题添加引号 + if (!titleInfo.startsWith('"') && !titleInfo.endsWith('"')) { + frontmatterInfo['title'] = `"${titleInfo.replace(/"/g, '\\"')}"`; + } + + let frontmatterNew = '---\n' + Object.keys(frontmatterInfo).map((key) => { + return `${key}: ${frontmatterInfo[key]}`; + }).join('\n') + '\n---'; + + // 替换整个 frontmatter + let contentNew = contentOld.replace(frontmatterRegExp, frontmatterNew); + + // 写回文件 + writeFileSync(filePath, contentNew, { encoding: 'utf-8', flag: 'w' }); + + console.info('处理文件 frontmatter 完成'); + + } catch (error) { + console.error(`处理文件 ${filePath} 时出错:`); + console.error(String(error)); + } +} + /** 转换 Markdown 表格为 JSON */ function markdownTableToJson(markdownTable = '') { @@ -104,8 +180,9 @@ function markdownTableToJson(markdownTable = '') { continue; } - console.warn(`重命名文件:${oldName} -> ${newName}`); + console.info(`重命名文件:${oldName} -> ${newName}`); renameSync(oldPath, newPath); + convertFrontmatter(newPath); }