Skip to content

Commit dcfe996

Browse files
committed
fix(markdown): 优化 markdown 生成逻辑及格式处理
- 调整代码块的字符串拼接方式,避免多余换行 - 对段落(P)、无序列表(UL)、有序列表(OL)、引用块(BLOCKQUOTE)和标题(H1-H6) 节点内容进行修剪,避免生成多余空白行 - 在生成的 markdown 末尾清理多余空行,确保最多保留一个空行 - 移除 markdown 文本开头和结尾的空行,保证格式规范 - 确保文件末尾有一个换行符,符合标准 markdown 格式要求 - 升级 package.json 版本号至 1.5.12
1 parent 06817a7 commit dcfe996

4 files changed

Lines changed: 29 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
- refactor(share): 用 html2canvas 替换 snapdom 实现图片捕获与下载 #283
1+
- fix(markdown): 优化 markdown 生成逻辑及格式处理

entrypoints/SettingComponents/BasicSettings/MenuExportArticle.vue

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export default {
226226
if (node.tagName === "PRE") {
227227
const codeBlock = node.querySelector("code");
228228
if (codeBlock) {
229-
return `\n\`\`\`\n${codeBlock.textContent.replace(/\n$/, "")}\n\`\`\`\n\n`;
229+
return "\n```\n" + codeBlock.textContent.replace(/\n$/, "") + "\n```\n";
230230
}
231231
}
232232
if (node.tagName === "CODE") {
@@ -238,38 +238,35 @@ export default {
238238
return " \n";
239239
}
240240
if (node.tagName === "P") {
241-
return "\n" + html2md_children(node) + "\n";
241+
const content = html2md_children(node).trim();
242+
return content ? "\n" + content + "\n" : "";
242243
}
243244
if (node.tagName === "UL") {
244-
return (
245-
"\n" +
246-
Array.from(node.children)
247-
.map((li) => html2md(li))
248-
.join("") +
249-
"\n"
250-
);
245+
const items = Array.from(node.children)
246+
.map((li) => html2md(li))
247+
.join("");
248+
return items ? "\n" + items : "";
251249
}
252250
if (node.tagName === "OL") {
253251
let i = 1;
254-
return (
255-
"\n" +
256-
Array.from(node.children)
257-
.map((li) => `${i++}. ${html2md_children(li)}\n`)
258-
.join("") +
259-
"\n"
260-
);
252+
const items = Array.from(node.children)
253+
.map((li) => `${i++}. ${html2md_children(li)}\n`)
254+
.join("");
255+
return items ? "\n" + items : "";
261256
}
262257
if (node.tagName === "LI") {
263258
const prefix =
264259
node.parentElement && node.parentElement.tagName === "UL" ? "- " : "";
265260
return prefix + html2md_children(node) + "\n";
266261
}
267262
if (node.tagName === "BLOCKQUOTE") {
268-
return "\n> " + html2md_children(node).replace(/\n/g, "\n> ") + "\n";
263+
const content = html2md_children(node).trim();
264+
return content ? "\n> " + content.replace(/\n/g, "\n> ") + "\n" : "";
269265
}
270266
if (/^H[1-6]$/.test(node.tagName)) {
271267
const lv = node.tagName[1];
272-
return "\n" + "#".repeat(lv) + " " + html2md_children(node) + "\n";
268+
const content = html2md_children(node).trim();
269+
return content ? "\n" + "#".repeat(lv) + " " + content + "\n" : "";
273270
}
274271
if (node.tagName === "A") {
275272
// 如果 a 标签只包含一个 img,直接导出为图片
@@ -348,6 +345,14 @@ export default {
348345
}
349346
}
350347
348+
// 清理多余的空行,按照标准 markdown 格式优化
349+
// 1. 移除连续的多个空行,最多保留一个空行
350+
allText = allText.replace(/\n{3,}/g, "\n\n");
351+
// 2. 移除开头和结尾的空行
352+
allText = allText.trim();
353+
// 3. 确保文件末尾有一个换行符(标准 markdown 格式)
354+
allText += "\n";
355+
351356
// 使用页面标题作为文件名
352357
const safeFileName = fileName.replace(/[\\/:*?"<>|]/g, "-");
353358

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "linuxdo-scripts",
33
"description": "manifest.json description",
44
"private": true,
5-
"version": "1.5.11",
5+
"version": "1.5.12",
66
"type": "module",
77
"scripts": {
88
"dev": "wxt",

version-log.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.5.12
2+
3+
- fix(markdown): 优化 markdown 生成逻辑及格式处理
4+
15
## 1.5.11
26

37
- refactor(share): 用 html2canvas 替换 snapdom 实现图片捕获与下载 #283

0 commit comments

Comments
 (0)