feat(export): convert single-line display math to multi-line
This commit is contained in:
46
main.js
46
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");
|
||||
|
||||
Reference in New Issue
Block a user