2024-02-25 08:12:07 +02:00
|
|
|
"use strict";
|
|
|
|
|
2025-01-11 15:21:32 +02:00
|
|
|
import { parse, Renderer, type Tokens } from "marked";
|
2025-05-18 19:33:11 +03:00
|
|
|
import htmlSanitizer from "../html_sanitizer.js";
|
|
|
|
import importUtils from "./utils.js";
|
2025-05-18 20:22:32 +03:00
|
|
|
import { getMimeTypeFromMarkdownName, MIME_TYPE_AUTO } from "@triliumnext/commons";
|
2025-05-18 19:33:11 +03:00
|
|
|
import { ADMONITION_TYPE_MAPPINGS } from "../export/markdown.js";
|
|
|
|
import utils from "../utils.js";
|
|
|
|
import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
|
2025-01-11 15:21:32 +02:00
|
|
|
|
2025-03-15 21:20:44 +02:00
|
|
|
/**
|
|
|
|
* Keep renderer code up to date with https://github.com/markedjs/marked/blob/master/src/Renderer.ts.
|
|
|
|
*/
|
2025-03-15 21:07:02 +02:00
|
|
|
class CustomMarkdownRenderer extends Renderer {
|
|
|
|
|
|
|
|
heading(data: Tokens.Heading): string {
|
2025-04-12 12:46:00 +03:00
|
|
|
// Treat h1 as raw text.
|
|
|
|
if (data.depth === 1) {
|
|
|
|
return `<h1>${data.text}</h1>`;
|
|
|
|
}
|
|
|
|
|
2025-03-15 21:07:02 +02:00
|
|
|
return super.heading(data).trimEnd();
|
2025-01-11 15:21:32 +02:00
|
|
|
}
|
|
|
|
|
2025-03-15 21:07:02 +02:00
|
|
|
paragraph(data: Tokens.Paragraph): string {
|
2025-04-05 09:57:44 +03:00
|
|
|
let text = super.paragraph(data).trimEnd();
|
|
|
|
|
2025-04-05 09:59:42 +03:00
|
|
|
if (text.includes("$")) {
|
|
|
|
// Display math
|
2025-04-05 10:46:33 +03:00
|
|
|
text = text.replaceAll(/(?<!\\)\$\$(.+)\$\$/g,
|
2025-04-05 09:59:42 +03:00
|
|
|
`<span class="math-tex">\\\[$1\\\]</span>`);
|
|
|
|
|
|
|
|
// Inline math
|
2025-05-10 22:56:36 +08:00
|
|
|
text = text.replaceAll(/(?<!\\)\$(.+?)\$/g,
|
2025-04-05 09:59:42 +03:00
|
|
|
`<span class="math-tex">\\\($1\\\)</span>`);
|
|
|
|
}
|
2025-04-05 09:59:10 +03:00
|
|
|
|
2025-04-05 09:57:44 +03:00
|
|
|
return text;
|
2025-03-15 21:07:02 +02:00
|
|
|
}
|
|
|
|
|
2025-03-15 21:20:44 +02:00
|
|
|
code({ text, lang }: Tokens.Code): string {
|
2025-03-15 21:07:02 +02:00
|
|
|
if (!text) {
|
2025-03-15 21:20:44 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2025-03-29 13:47:02 +02:00
|
|
|
// Escape the HTML.
|
|
|
|
text = utils.escapeHtml(text);
|
|
|
|
|
|
|
|
// Unescape "
|
|
|
|
text = text.replace(/"/g, '"');
|
|
|
|
|
2025-03-15 21:20:44 +02:00
|
|
|
const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang);
|
|
|
|
return `<pre><code class="language-${ckEditorLanguage}">${text}</code></pre>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
list(token: Tokens.List): string {
|
2025-04-17 18:34:40 +03:00
|
|
|
let result = super.list(token)
|
2025-03-15 21:20:44 +02:00
|
|
|
.replace("\n", "") // we replace the first one only.
|
|
|
|
.trimEnd();
|
2025-04-17 18:34:40 +03:00
|
|
|
|
|
|
|
// Handle todo-list in the CKEditor format.
|
|
|
|
if (token.items.some(item => item.task)) {
|
|
|
|
result = result.replace(/^<ul>/, "<ul class=\"todo-list\">");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkbox({ checked }: Tokens.Checkbox): string {
|
|
|
|
return '<input type="checkbox"'
|
|
|
|
+ (checked ? 'checked="checked" ' : '')
|
|
|
|
+ 'disabled="disabled">';
|
2025-03-15 21:20:44 +02:00
|
|
|
}
|
2025-03-15 21:07:02 +02:00
|
|
|
|
2025-03-15 21:20:44 +02:00
|
|
|
listitem(item: Tokens.ListItem): string {
|
2025-04-17 18:34:40 +03:00
|
|
|
// Handle todo-list in the CKEditor format.
|
|
|
|
if (item.task) {
|
|
|
|
let itemBody = '';
|
|
|
|
const checkbox = this.checkbox({ checked: !!item.checked });
|
|
|
|
if (item.loose) {
|
|
|
|
if (item.tokens[0]?.type === 'paragraph') {
|
|
|
|
item.tokens[0].text = checkbox + item.tokens[0].text;
|
|
|
|
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
|
|
|
|
item.tokens[0].tokens[0].text = checkbox + escape(item.tokens[0].tokens[0].text);
|
|
|
|
item.tokens[0].tokens[0].escaped = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
item.tokens.unshift({
|
|
|
|
type: 'text',
|
|
|
|
raw: checkbox,
|
|
|
|
text: checkbox,
|
|
|
|
escaped: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
itemBody += checkbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemBody += `<span class="todo-list__label__description">${this.parser.parse(item.tokens, !!item.loose)}</span>`;
|
|
|
|
return `<li><label class="todo-list__label">${itemBody}</label></li>`;
|
|
|
|
}
|
|
|
|
|
2025-03-15 21:20:44 +02:00
|
|
|
return super.listitem(item).trimEnd();
|
2025-03-15 21:07:02 +02:00
|
|
|
}
|
2025-03-15 11:58:11 +02:00
|
|
|
|
2025-03-16 13:58:31 +02:00
|
|
|
image(token: Tokens.Image): string {
|
|
|
|
return super.image(token)
|
|
|
|
.replace(` alt=""`, "");
|
|
|
|
}
|
|
|
|
|
2025-03-15 21:07:02 +02:00
|
|
|
blockquote({ tokens }: Tokens.Blockquote): string {
|
|
|
|
const body = renderer.parser.parse(tokens);
|
2025-03-15 11:58:11 +02:00
|
|
|
|
2025-03-15 21:07:02 +02:00
|
|
|
const admonitionMatch = /^<p>\[\!([A-Z]+)\]/.exec(body);
|
|
|
|
if (Array.isArray(admonitionMatch) && admonitionMatch.length === 2) {
|
|
|
|
const type = admonitionMatch[1].toLowerCase();
|
2025-03-15 11:58:11 +02:00
|
|
|
|
2025-03-15 21:07:02 +02:00
|
|
|
if (ADMONITION_TYPE_MAPPINGS[type]) {
|
|
|
|
const bodyWithoutHeader = body
|
2025-03-15 22:39:33 +02:00
|
|
|
.replace(/^<p>\[\!([A-Z]+)\]\s*/, "<p>")
|
2025-03-15 21:07:02 +02:00
|
|
|
.replace(/^<p><\/p>/, ""); // Having a heading will generate an empty paragraph that we need to remove.
|
|
|
|
|
2025-03-15 22:39:33 +02:00
|
|
|
return `<aside class="admonition ${type}">${bodyWithoutHeader.trim()}</aside>`;
|
2025-03-15 21:07:02 +02:00
|
|
|
}
|
2025-03-15 11:58:11 +02:00
|
|
|
}
|
2025-03-15 21:07:02 +02:00
|
|
|
|
2025-03-15 22:39:33 +02:00
|
|
|
return `<blockquote>${body}</blockquote>`;
|
2025-03-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
2025-03-15 21:07:02 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 08:12:07 +02:00
|
|
|
function renderToHtml(content: string, title: string) {
|
2025-04-05 10:46:33 +03:00
|
|
|
// Double-escape slashes in math expression because they are otherwise consumed by the parser somewhere.
|
|
|
|
content = content.replaceAll("\\$", "\\\\$");
|
|
|
|
|
2025-03-14 19:50:26 +02:00
|
|
|
let html = parse(content, {
|
2025-01-11 15:21:32 +02:00
|
|
|
async: false,
|
|
|
|
renderer: renderer
|
2024-04-13 17:30:48 +03:00
|
|
|
}) as string;
|
2025-03-14 19:50:26 +02:00
|
|
|
|
|
|
|
// h1 handling needs to come before sanitization
|
|
|
|
html = importUtils.handleH1(html, title);
|
2025-04-02 23:30:35 +03:00
|
|
|
html = htmlSanitizer.sanitize(html);
|
2025-03-14 19:50:26 +02:00
|
|
|
|
2025-04-05 12:31:02 +03:00
|
|
|
// Add a trailing semicolon to CSS styles.
|
2025-04-05 21:13:12 +03:00
|
|
|
html = html.replaceAll(/(<(img|figure|col).*?style=".*?)"/g, "$1;\"");
|
2025-04-05 12:31:02 +03:00
|
|
|
|
2025-03-16 13:58:31 +02:00
|
|
|
// Remove slash for self-closing tags to match CKEditor's approach.
|
|
|
|
html = html.replace(/<(\w+)([^>]*)\s+\/>/g, "<$1$2>");
|
|
|
|
|
2025-04-03 19:29:51 +03:00
|
|
|
// Normalize non-breaking spaces to entity.
|
|
|
|
html = html.replaceAll("\u00a0", " ");
|
|
|
|
|
2025-03-14 19:50:26 +02:00
|
|
|
return html;
|
2024-02-25 08:12:07 +02:00
|
|
|
}
|
|
|
|
|
2025-01-11 15:21:32 +02:00
|
|
|
function getNormalizedMimeFromMarkdownLanguage(language: string | undefined) {
|
|
|
|
if (language) {
|
2025-05-18 20:22:32 +03:00
|
|
|
const mimeDefinition = getMimeTypeFromMarkdownName(language);
|
|
|
|
if (mimeDefinition) {
|
|
|
|
return normalizeMimeTypeForCKEditor(mimeDefinition.mime);
|
2025-01-11 15:21:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MIME_TYPE_AUTO;
|
|
|
|
}
|
|
|
|
|
2025-05-18 19:33:11 +03:00
|
|
|
const renderer = new CustomMarkdownRenderer({ async: false });
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2024-02-25 08:12:07 +02:00
|
|
|
renderToHtml
|
|
|
|
};
|