mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-10-03 03:11:31 +08:00
refactor(export): simplify code
This commit is contained in:
parent
f02cca7385
commit
907a27ae9d
@ -5,35 +5,37 @@ import turndownPluginGfm from "joplin-turndown-plugin-gfm";
|
|||||||
|
|
||||||
let instance: TurndownService | null = null;
|
let instance: TurndownService | null = null;
|
||||||
|
|
||||||
|
const fencedCodeBlockFilter: TurndownService.Rule = {
|
||||||
|
filter: function (node, options) {
|
||||||
|
return (
|
||||||
|
options.codeBlockStyle === 'fenced' &&
|
||||||
|
node.nodeName === 'PRE' &&
|
||||||
|
node.firstChild !== null &&
|
||||||
|
node.firstChild.nodeName === 'CODE'
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
|
replacement: function (content, node, options) {
|
||||||
|
if (!node.firstChild || !("getAttribute" in node.firstChild) || typeof node.firstChild.getAttribute !== "function") {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
const className = node.firstChild.getAttribute('class') || ''
|
||||||
|
const language = rewriteLanguageTag((className.match(/language-(\S+)/) || [null, ''])[1]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
'\n\n' + options.fence + language + '\n' +
|
||||||
|
node.firstChild.textContent +
|
||||||
|
'\n' + options.fence + '\n\n'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function toMarkdown(content: string) {
|
function toMarkdown(content: string) {
|
||||||
if (instance === null) {
|
if (instance === null) {
|
||||||
instance = new TurndownService({ codeBlockStyle: 'fenced' });
|
instance = new TurndownService({ codeBlockStyle: 'fenced' });
|
||||||
instance.addRule('fencedCodeBlock', {
|
// Filter is heavily based on: https://github.com/mixmark-io/turndown/issues/274#issuecomment-458730974
|
||||||
filter: function (node, options) {
|
instance.addRule('fencedCodeBlock', fencedCodeBlockFilter);
|
||||||
return (
|
|
||||||
options.codeBlockStyle === 'fenced' &&
|
|
||||||
node.nodeName === 'PRE' &&
|
|
||||||
node.firstChild !== null &&
|
|
||||||
node.firstChild.nodeName === 'CODE'
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
replacement: function (content, node, options) {
|
|
||||||
if (!node.firstChild || !("getAttribute" in node.firstChild) || typeof node.firstChild.getAttribute !== "function") {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
var className = node.firstChild.getAttribute('class') || ''
|
|
||||||
var language = (className.match(/language-(\S+)/) || [null, ''])[1];
|
|
||||||
language = rewriteLanguageTag(language);
|
|
||||||
|
|
||||||
return (
|
|
||||||
'\n\n' + options.fence + language + '\n' +
|
|
||||||
node.firstChild.textContent +
|
|
||||||
'\n' + options.fence + '\n\n'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
instance.use(turndownPluginGfm.gfm);
|
instance.use(turndownPluginGfm.gfm);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +43,10 @@ function toMarkdown(content: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function rewriteLanguageTag(source: string) {
|
function rewriteLanguageTag(source: string) {
|
||||||
|
if (!source) {
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
if (source === "text-x-trilium-auto") {
|
if (source === "text-x-trilium-auto") {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user