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-14 19:50:26 +02:00
|
|
|
import { minify as minifyHtml } from "html-minifier";
|
2025-01-11 15:21:32 +02:00
|
|
|
|
2025-03-15 11:58:11 +02:00
|
|
|
// Keep renderer code up to date with https://github.com/markedjs/marked/blob/master/src/Renderer.ts.
|
2025-01-11 15:21:32 +02:00
|
|
|
const renderer = new Renderer({ async: false });
|
2025-03-02 20:47:57 +01:00
|
|
|
renderer.code = ({ text, lang, escaped }: Tokens.Code) => {
|
2025-01-11 15:21:32 +02:00
|
|
|
if (!text) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang);
|
|
|
|
return `<pre><code class="language-${ckEditorLanguage}">${text}</code></pre>`;
|
|
|
|
};
|
2025-03-15 11:58:11 +02:00
|
|
|
renderer.blockquote = ({ tokens }: Tokens.Blockquote) => {
|
|
|
|
const body = renderer.parser.parse(tokens);
|
|
|
|
|
|
|
|
const admonitionMatch = /^<p>\[\!([A-Z]+)\]/.exec(body);
|
|
|
|
if (Array.isArray(admonitionMatch) && admonitionMatch.length === 2) {
|
|
|
|
const type = admonitionMatch[1].toLowerCase();
|
|
|
|
|
|
|
|
if (ADMONITION_TYPE_MAPPINGS[type]) {
|
|
|
|
const bodyWithoutHeader = body
|
|
|
|
.replace(/^<p>\[\!([A-Z]+)\]/, "<p>")
|
|
|
|
.replace(/^<p><\/p>/, ""); // Having a heading will generate an empty paragraph that we need to remove.
|
|
|
|
|
|
|
|
return `<aside class="admonition ${type}">\n${bodyWithoutHeader}</aside>\n`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<blockquote>\n${body}</blockquote>\n`;
|
|
|
|
};
|
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";
|
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);
|
|
|
|
html = htmlSanitizer.sanitize(html);
|
|
|
|
html = minifyHtml(html, {
|
|
|
|
collapseWhitespace: true
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|