From 58d5d208ad839ef5034e0d07816fae6b7f0c0766 Mon Sep 17 00:00:00 2001 From: gameloader Date: Mon, 19 Jan 2026 18:29:59 +0800 Subject: [PATCH] feat(export): convert single-line display math to multi-line --- main.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index 2a7a8ad..3d8fd76 100644 --- a/main.js +++ b/main.js @@ -50,6 +50,49 @@ function upsertFrontmatter(content, frontmatterLines, keys) { return ["---", ...frontmatterLines, "---", "", content].join("\n"); } +function splitFrontmatter(content) { + const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n?/; + const match = content.match(frontmatterRegex); + if (!match) { + return { frontmatter: "", body: content }; + } + return { frontmatter: match[0], body: content.slice(match[0].length) }; +} + +function convertSingleLineDisplayMath(content) { + const { frontmatter, body } = splitFrontmatter(content); + const newline = content.includes("\r\n") ? "\r\n" : "\n"; + const lines = body.split(/\r?\n/); + let inFence = false; + let fenceMarker = ""; + let fenceLength = 0; + const converted = lines.flatMap((line) => { + const fenceMatch = line.match(/^\s*([`~]{3,})/); + if (fenceMatch) { + if (!inFence) { + inFence = true; + fenceMarker = fenceMatch[1][0]; + fenceLength = fenceMatch[1].length; + } else if (fenceMatch[1][0] === fenceMarker && fenceMatch[1].length >= fenceLength) { + inFence = false; + fenceMarker = ""; + fenceLength = 0; + } + return [line]; + } + + if (inFence) return [line]; + + const match = line.match(/^(\s*)\$\$(.+?)\$\$\s*$/); + if (!match) return [line]; + const indent = match[1] || ""; + const inner = match[2].trim(); + return [indent + "$$", indent + inner, indent + "$$"]; + }); + + return frontmatter + converted.join(newline); +} + class AstroPostExporter extends Plugin { async onload() { await this.loadSettings(); @@ -152,7 +195,8 @@ class AstroPostExporter extends Plugin { for (const file of files) { try { const content = await this.app.vault.read(file); - const output = this.applyFrontmatter(file, content); + const transformed = convertSingleLineDisplayMath(content); + const output = this.applyFrontmatter(file, transformed); const destinationPath = this.getDestinationPath(file, exportDir); await fs.promises.mkdir(path.dirname(destinationPath), { recursive: true }); await fs.promises.writeFile(destinationPath, output, "utf8");