2019-09-30 20:56:32 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:37:45 +03:00
|
|
|
import TurndownService from "turndown";
|
2025-02-08 22:59:28 +02:00
|
|
|
import turndownPluginGfm from "@joplin/turndown-plugin-gfm";
|
2019-09-30 20:56:32 +02:00
|
|
|
|
2024-02-19 21:59:40 +02:00
|
|
|
let instance: TurndownService | null = null;
|
2019-09-30 20:56:32 +02:00
|
|
|
|
2024-12-17 23:45:37 +02:00
|
|
|
const fencedCodeBlockFilter: TurndownService.Rule = {
|
2025-01-09 18:07:02 +02:00
|
|
|
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;
|
|
|
|
}
|
2024-12-17 23:45:37 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const className = node.firstChild.getAttribute("class") || "";
|
|
|
|
const language = rewriteLanguageTag((className.match(/language-(\S+)/) || [null, ""])[1]);
|
2024-12-17 23:45:37 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return "\n\n" + options.fence + language + "\n" + node.firstChild.textContent + "\n" + options.fence + "\n\n";
|
|
|
|
}
|
2024-12-17 23:45:37 +02:00
|
|
|
};
|
|
|
|
|
2024-02-19 21:59:40 +02:00
|
|
|
function toMarkdown(content: string) {
|
2019-09-30 20:56:32 +02:00
|
|
|
if (instance === null) {
|
2025-02-22 12:45:21 +02:00
|
|
|
instance = new TurndownService({
|
|
|
|
headingStyle: "atx",
|
|
|
|
codeBlockStyle: "fenced"
|
|
|
|
});
|
2024-12-22 15:42:15 +02:00
|
|
|
// Filter is heavily based on: https://github.com/mixmark-io/turndown/issues/274#issuecomment-458730974
|
2025-01-09 18:07:02 +02:00
|
|
|
instance.addRule("fencedCodeBlock", fencedCodeBlockFilter);
|
2025-03-11 17:12:48 +02:00
|
|
|
instance.addRule("img", buildImageFilter());
|
2019-09-30 20:56:32 +02:00
|
|
|
instance.use(turndownPluginGfm.gfm);
|
2025-03-14 17:59:50 +02:00
|
|
|
instance.keep([ "kbd" ]);
|
2019-09-30 20:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return instance.turndown(content);
|
|
|
|
}
|
|
|
|
|
2024-12-17 23:35:08 +02:00
|
|
|
function rewriteLanguageTag(source: string) {
|
2024-12-17 23:45:37 +02:00
|
|
|
if (!source) {
|
2025-01-09 18:07:02 +02:00
|
|
|
return source;
|
2024-12-17 23:45:37 +02:00
|
|
|
}
|
|
|
|
|
2025-01-11 14:11:18 +02:00
|
|
|
switch (source) {
|
|
|
|
case "text-x-trilium-auto":
|
|
|
|
return "";
|
|
|
|
case "application-javascript-env-frontend":
|
|
|
|
case "application-javascript-env-backend":
|
|
|
|
return "javascript";
|
2025-03-11 21:05:55 +02:00
|
|
|
case "text-x-nginx-conf":
|
|
|
|
return "nginx";
|
2025-01-11 14:11:18 +02:00
|
|
|
default:
|
|
|
|
return source.split("-").at(-1);
|
2024-12-17 23:40:39 +02:00
|
|
|
}
|
2024-12-17 23:35:08 +02:00
|
|
|
}
|
|
|
|
|
2025-03-11 17:12:48 +02:00
|
|
|
// TODO: Remove once upstream delivers a fix for https://github.com/mixmark-io/turndown/issues/467.
|
|
|
|
function buildImageFilter() {
|
|
|
|
const ESCAPE_PATTERNS = {
|
|
|
|
before: /([\\*`[\]_]|(?:^[-+>])|(?:^~~~)|(?:^#{1-6}))/g,
|
|
|
|
after: /((?:^\d+(?=\.)))/
|
|
|
|
}
|
|
|
|
|
|
|
|
const escapePattern = new RegExp('(?:' + ESCAPE_PATTERNS.before.source + '|' + ESCAPE_PATTERNS.after.source + ')', 'g');
|
|
|
|
|
|
|
|
function escapeMarkdown (content: string) {
|
|
|
|
return content.replace(escapePattern, function (match, before, after) {
|
|
|
|
return before ? '\\' + before : after + '\\'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeLinkDestination(destination: string) {
|
|
|
|
return destination
|
|
|
|
.replace(/([()])/g, '\\$1')
|
|
|
|
.replace(/ /g, "%20");
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeLinkTitle (title: string) {
|
|
|
|
return title.replace(/"/g, '\\"')
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanAttribute (attribute: string) {
|
|
|
|
return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : ''
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageFilter: TurndownService.Rule = {
|
|
|
|
filter: "img",
|
|
|
|
replacement(content, node) {
|
|
|
|
const untypedNode = (node as any);
|
|
|
|
const alt = escapeMarkdown(cleanAttribute(untypedNode.getAttribute('alt')))
|
|
|
|
const src = escapeLinkDestination(untypedNode.getAttribute('src') || '')
|
|
|
|
const title = cleanAttribute(untypedNode.getAttribute('title'))
|
|
|
|
const titlePart = title ? ' "' + escapeLinkTitle(title) + '"' : ''
|
|
|
|
|
|
|
|
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return imageFilter;
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2019-09-30 20:56:32 +02:00
|
|
|
toMarkdown
|
2021-02-20 20:10:45 +01:00
|
|
|
};
|