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-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 {
|
|
|
|
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 {
|
|
|
|
return super.paragraph(data).trimEnd();
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return super.list(token)
|
|
|
|
.replace("\n", "") // we replace the first one only.
|
|
|
|
.trimEnd();
|
|
|
|
}
|
2025-03-15 21:07:02 +02:00
|
|
|
|
2025-03-15 21:20:44 +02:00
|
|
|
listitem(item: Tokens.ListItem): string {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const renderer = new CustomMarkdownRenderer({ async: false });
|
2025-01-11 15:21:32 +02:00
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import htmlSanitizer from "../html_sanitizer.js";
|
|
|
|
import importUtils from "./utils.js";
|
2025-01-20 20:15:39 +01:00
|
|
|
import { getMimeTypeFromHighlightJs, MIME_TYPE_AUTO, normalizeMimeTypeForCKEditor } from "./mime_type_definitions.js";
|
2025-03-15 11:58:11 +02:00
|
|
|
import { ADMONITION_TYPE_MAPPINGS } from "../export/markdown.js";
|
2025-03-29 13:47:02 +02:00
|
|
|
import utils from "../utils.js";
|
2024-02-25 08:12:07 +02:00
|
|
|
|
|
|
|
function renderToHtml(content: string, title: string) {
|
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-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-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) {
|
|
|
|
const highlightJsName = getMimeTypeFromHighlightJs(language);
|
|
|
|
if (highlightJsName) {
|
|
|
|
return normalizeMimeTypeForCKEditor(highlightJsName.mime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MIME_TYPE_AUTO;
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2024-02-25 08:12:07 +02:00
|
|
|
renderToHtml
|
|
|
|
};
|