Skip to content

Commit 129b371

Browse files
committed
掘金下载 markdown 代码块解析优化
1 parent e53db6c commit 129b371

File tree

3 files changed

+32
-45
lines changed

3 files changed

+32
-45
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-box",
33
"displayName": "__MSG_extensionName__",
4-
"version": "0.7.5",
4+
"version": "0.7.6",
55
"description": "__MSG_extensionDescription__",
66
"author": "027xiguapi. <458813868@qq.com>",
77
"scripts": {
@@ -33,7 +33,7 @@
3333
"dayjs": "^1.11.12",
3434
"file-saver": "^2.0.5",
3535
"html2canvas": "^1.4.1",
36-
"jspdf": "^2.5.1",
36+
"jspdf": "^2.5.2",
3737
"jssha": "^3.3.1",
3838
"plasmo": "0.89.2",
3939
"react": "18.2.0",

patches/jspdf.patch

Lines changed: 3 additions & 43 deletions
Large diffs are not rendered by default.

utils/turndown.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,32 @@ export default function Turndown() {
1717
// turndownService.keep(["h1", "h2"])
1818
turndownService.remove(["script", "style"])
1919

20+
turndownService.addRule("fencedCodeBlock", {
21+
filter: function (node, options) {
22+
return (
23+
options.codeBlockStyle === "fenced" &&
24+
node.nodeName === "PRE" &&
25+
node.querySelector("code")
26+
)
27+
},
28+
29+
replacement: function (content, node, options) {
30+
const className = node.querySelector("code").getAttribute("class") || ""
31+
const language = (className.match(/lang-(\S+)/) ||
32+
className.match(/language-(\S+)/) || [null, ""])[1]
33+
34+
return (
35+
"\n\n" +
36+
options.fence +
37+
language +
38+
"\n" +
39+
node.querySelector("code").textContent +
40+
"\n" +
41+
options.fence +
42+
"\n\n"
43+
)
44+
}
45+
})
46+
2047
return turndownService
2148
}

0 commit comments

Comments
 (0)