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 {
|
|
|
|
|
2025-05-29 13:34:42 +03:00
|
|
|
override 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-05-29 13:34:42 +03:00
|
|
|
override paragraph(data: Tokens.Paragraph): string {
|
2025-06-20 21:00:39 +03:00
|
|
|
return super.paragraph(data).trimEnd();
|
2025-03-15 21:07:02 +02:00
|
|
|
}
|
|
|
|
|
2025-05-29 13:34:42 +03:00
|
|
|
override 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>`;
|
|
|
|
}
|
|
|
|
|
2025-05-29 13:34:42 +03:00
|
|
|
override 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;
|
|
|
|
}
|
|
|
|
|
2025-05-29 13:34:42 +03:00
|
|
|
override checkbox({ checked }: Tokens.Checkbox): string {
|
2025-04-17 18:34:40 +03:00
|
|
|
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-05-29 13:34:42 +03:00
|
|
|
override 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-05-29 13:34:42 +03:00
|
|
|
override image(token: Tokens.Image): string {
|
2025-03-16 13:58:31 +02:00
|
|
|
return super.image(token)
|
|
|
|
.replace(` alt=""`, "");
|
|
|
|
}
|
|
|
|
|
2025-05-29 13:34:42 +03:00
|
|
|
override blockquote({ tokens }: Tokens.Blockquote): string {
|
2025-03-15 21:07:02 +02:00
|
|
|
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-06-20 21:00:39 +03:00
|
|
|
text(token: Tokens.Text | Tokens.Escape): string {
|
|
|
|
let text = super.text(token);
|
|
|
|
text = processWikiLinks(text);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
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-05-21 17:15:54 +08:00
|
|
|
|
2025-05-20 22:03:40 +08:00
|
|
|
// Extract formulas and replace them with placeholders to prevent interference from Markdown rendering
|
2025-05-21 17:15:54 +08:00
|
|
|
const { processedText, placeholderMap: formulaMap } = extractFormulas(content);
|
2025-04-05 10:46:33 +03:00
|
|
|
|
2025-05-20 22:03:40 +08:00
|
|
|
let html = parse(processedText, {
|
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
|
|
|
|
2025-05-20 22:03:40 +08:00
|
|
|
// After rendering, replace placeholders back with the formula HTML
|
2025-05-21 17:15:54 +08:00
|
|
|
html = restoreFromMap(html, formulaMap);
|
2025-05-20 22:03:40 +08:00
|
|
|
|
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-21 17:15:54 +08:00
|
|
|
function extractCodeBlocks(text: string): { processedText: string, placeholderMap: Map<string, string> } {
|
|
|
|
const codeMap = new Map<string, string>();
|
|
|
|
let id = 0;
|
|
|
|
const timestamp = Date.now();
|
2025-05-20 22:03:40 +08:00
|
|
|
|
2025-05-21 17:15:54 +08:00
|
|
|
// Multi-line code block and Inline code
|
|
|
|
text = text.replace(/```[\s\S]*?```/g, (m) => {
|
|
|
|
const key = `<!--CODE_BLOCK_${timestamp}_${id++}-->`;
|
|
|
|
codeMap.set(key, m);
|
|
|
|
return key;
|
|
|
|
}).replace(/`[^`\n]+`/g, (m) => {
|
|
|
|
const key = `<!--INLINE_CODE_${timestamp}_${id++}-->`;
|
|
|
|
codeMap.set(key, m);
|
2025-05-20 22:03:40 +08:00
|
|
|
return key;
|
|
|
|
});
|
|
|
|
|
2025-05-21 17:15:54 +08:00
|
|
|
return { processedText: text, placeholderMap: codeMap };
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractFormulas(text: string): { processedText: string, placeholderMap: Map<string, string> } {
|
|
|
|
// Protect the $ signs inside code blocks from being recognized as formulas.
|
|
|
|
const { processedText: noCodeText, placeholderMap: codeMap } = extractCodeBlocks(text);
|
|
|
|
|
|
|
|
const formulaMap = new Map<string, string>();
|
|
|
|
let id = 0;
|
|
|
|
const timestamp = Date.now();
|
|
|
|
|
|
|
|
// Display math and Inline math
|
|
|
|
let processedText = noCodeText.replace(/(?<!\\)\$\$((?:(?!\n{2,})[\s\S])+?)\$\$/g, (_, formula) => {
|
|
|
|
const key = `<!--FORMULA_BLOCK_${timestamp}_${id++}-->`;
|
|
|
|
const rendered = `<span class="math-tex">\\[${formula}\\]</span>`;
|
|
|
|
formulaMap.set(key, rendered);
|
|
|
|
return key;
|
|
|
|
}).replace(/(?<!\\)\$(.+?)\$/g, (_, formula) => {
|
|
|
|
const key = `<!--FORMULA_INLINE_${timestamp}_${id++}-->`;
|
|
|
|
const rendered = `<span class="math-tex">\\(${formula}\\)</span>`;
|
|
|
|
formulaMap.set(key, rendered);
|
2025-05-20 22:03:40 +08:00
|
|
|
return key;
|
|
|
|
});
|
2025-05-21 17:15:54 +08:00
|
|
|
|
|
|
|
processedText = restoreFromMap(processedText, codeMap);
|
|
|
|
|
|
|
|
return { processedText, placeholderMap: formulaMap };
|
2025-05-20 22:03:40 +08:00
|
|
|
}
|
|
|
|
|
2025-05-21 17:15:54 +08:00
|
|
|
function restoreFromMap(text: string, map: Map<string, string>): string {
|
|
|
|
if (map.size === 0) return text;
|
|
|
|
const pattern = [...map.keys()]
|
|
|
|
.map(k => k.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
|
|
.join('|');
|
|
|
|
return text.replace(new RegExp(pattern, 'g'), match => map.get(match) ?? match);
|
2025-05-20 22:03:40 +08:00
|
|
|
}
|
|
|
|
|
2025-06-20 18:28:08 +03:00
|
|
|
function processWikiLinks(paragraph: string) {
|
2025-06-20 20:56:25 +03:00
|
|
|
paragraph = paragraph.replaceAll(/\[\[([^\[\]]+)\]\]/g, `<a class="reference-link" href="/$1">$1</a>`);
|
2025-06-20 18:28:08 +03:00
|
|
|
return paragraph;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|