mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-10 10:22:29 +08:00

as discussed in #1010 – quick & dirty fix by copying over the file into the server side of things Comment added to both files, about deduplication in the future fixes #1010
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
"use strict";
|
|
|
|
import { parse, Renderer, type Tokens } from "marked";
|
|
|
|
const renderer = new Renderer({ async: false });
|
|
renderer.code = ({text, lang, escaped}: Tokens.Code) => {
|
|
if (!text) {
|
|
return "";
|
|
}
|
|
|
|
const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang);
|
|
return `<pre><code class="language-${ckEditorLanguage}">${text}</code></pre>`;
|
|
};
|
|
|
|
import htmlSanitizer from "../html_sanitizer.js";
|
|
import importUtils from "./utils.js";
|
|
import { getMimeTypeFromHighlightJs, MIME_TYPE_AUTO, normalizeMimeTypeForCKEditor } from "./mime_type_definitions.js";
|
|
|
|
function renderToHtml(content: string, title: string) {
|
|
const html = parse(content, {
|
|
async: false,
|
|
renderer: renderer
|
|
}) as string;
|
|
const h1Handled = importUtils.handleH1(html, title); // h1 handling needs to come before sanitization
|
|
return htmlSanitizer.sanitize(h1Handled);
|
|
}
|
|
|
|
function getNormalizedMimeFromMarkdownLanguage(language: string | undefined) {
|
|
if (language) {
|
|
const highlightJsName = getMimeTypeFromHighlightJs(language);
|
|
if (highlightJsName) {
|
|
return normalizeMimeTypeForCKEditor(highlightJsName.mime);
|
|
}
|
|
}
|
|
|
|
return MIME_TYPE_AUTO;
|
|
}
|
|
|
|
export default {
|
|
renderToHtml
|
|
};
|