refactor(export): simplify code

This commit is contained in:
Elian Doran 2024-12-17 23:45:37 +02:00
parent f02cca7385
commit 907a27ae9d
No known key found for this signature in database

View File

@ -5,10 +5,7 @@ import turndownPluginGfm from "joplin-turndown-plugin-gfm";
let instance: TurndownService | null = null; let instance: TurndownService | null = null;
function toMarkdown(content: string) { const fencedCodeBlockFilter: TurndownService.Rule = {
if (instance === null) {
instance = new TurndownService({ codeBlockStyle: 'fenced' });
instance.addRule('fencedCodeBlock', {
filter: function (node, options) { filter: function (node, options) {
return ( return (
options.codeBlockStyle === 'fenced' && options.codeBlockStyle === 'fenced' &&
@ -23,9 +20,8 @@ function toMarkdown(content: string) {
return content; return content;
} }
var className = node.firstChild.getAttribute('class') || '' const className = node.firstChild.getAttribute('class') || ''
var language = (className.match(/language-(\S+)/) || [null, ''])[1]; const language = rewriteLanguageTag((className.match(/language-(\S+)/) || [null, ''])[1]);
language = rewriteLanguageTag(language);
return ( return (
'\n\n' + options.fence + language + '\n' + '\n\n' + options.fence + language + '\n' +
@ -33,7 +29,13 @@ function toMarkdown(content: string) {
'\n' + options.fence + '\n\n' '\n' + options.fence + '\n\n'
) )
} }
}) };
function toMarkdown(content: string) {
if (instance === null) {
instance = new TurndownService({ codeBlockStyle: 'fenced' });
// Filter is heavily based on: https://github.com/mixmark-io/turndown/issues/274#issuecomment-458730974
instance.addRule('fencedCodeBlock', fencedCodeBlockFilter);
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 "";
} }