Skip to content

Commit

Permalink
掘金下载 markdown 代码块解析优化
Browse files Browse the repository at this point in the history
  • Loading branch information
027xiguapi committed Oct 9, 2024
1 parent e53db6c commit 129b371
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "code-box",
"displayName": "__MSG_extensionName__",
"version": "0.7.5",
"version": "0.7.6",
"description": "__MSG_extensionDescription__",
"author": "027xiguapi. <458813868@qq.com>",
"scripts": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"dayjs": "^1.11.12",
"file-saver": "^2.0.5",
"html2canvas": "^1.4.1",
"jspdf": "^2.5.1",
"jspdf": "^2.5.2",
"jssha": "^3.3.1",
"plasmo": "0.89.2",
"react": "18.2.0",
Expand Down
46 changes: 3 additions & 43 deletions patches/jspdf.patch

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions utils/turndown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,32 @@ export default function Turndown() {
// turndownService.keep(["h1", "h2"])
turndownService.remove(["script", "style"])

turndownService.addRule("fencedCodeBlock", {
filter: function (node, options) {
return (
options.codeBlockStyle === "fenced" &&
node.nodeName === "PRE" &&
node.querySelector("code")
)
},

replacement: function (content, node, options) {
const className = node.querySelector("code").getAttribute("class") || ""
const language = (className.match(/lang-(\S+)/) ||
className.match(/language-(\S+)/) || [null, ""])[1]

return (
"\n\n" +
options.fence +
language +
"\n" +
node.querySelector("code").textContent +
"\n" +
options.fence +
"\n\n"
)
}
})

return turndownService
}

0 comments on commit 129b371

Please sign in to comment.