mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-10-05 12:42:04 +08:00
refactor(import/markdown): change renderer instead of applying an uglifier
This commit is contained in:
parent
36fa0af706
commit
30593eeeac
@ -1,19 +1,27 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import { parse, Renderer, type Tokens } from "marked";
|
import { parse, Renderer, type Tokens } from "marked";
|
||||||
import { minify as minifyHtml } from "html-minifier";
|
|
||||||
|
|
||||||
// Keep renderer code up to date with https://github.com/markedjs/marked/blob/master/src/Renderer.ts.
|
class CustomMarkdownRenderer extends Renderer {
|
||||||
const renderer = new Renderer({ async: false });
|
|
||||||
renderer.code = ({ text, lang, escaped }: Tokens.Code) => {
|
heading(data: Tokens.Heading): string {
|
||||||
|
return super.heading(data).trimEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
paragraph(data: Tokens.Paragraph): string {
|
||||||
|
return super.paragraph(data).trimEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
code({ text, lang, escaped }: Tokens.Code): string {
|
||||||
if (!text) {
|
if (!text) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang);
|
const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang);
|
||||||
return `<pre><code class="language-${ckEditorLanguage}">${text}</code></pre>`;
|
return `<pre><code class="language-${ckEditorLanguage}">${text}</code></pre>`;
|
||||||
};
|
}
|
||||||
renderer.blockquote = ({ tokens }: Tokens.Blockquote) => {
|
|
||||||
|
blockquote({ tokens }: Tokens.Blockquote): string {
|
||||||
const body = renderer.parser.parse(tokens);
|
const body = renderer.parser.parse(tokens);
|
||||||
|
|
||||||
const admonitionMatch = /^<p>\[\!([A-Z]+)\]/.exec(body);
|
const admonitionMatch = /^<p>\[\!([A-Z]+)\]/.exec(body);
|
||||||
@ -30,7 +38,12 @@ renderer.blockquote = ({ tokens }: Tokens.Blockquote) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return `<blockquote>\n${body}</blockquote>\n`;
|
return `<blockquote>\n${body}</blockquote>\n`;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep renderer code up to date with https://github.com/markedjs/marked/blob/master/src/Renderer.ts.
|
||||||
|
const renderer = new CustomMarkdownRenderer({ async: false });
|
||||||
|
|
||||||
import htmlSanitizer from "../html_sanitizer.js";
|
import htmlSanitizer from "../html_sanitizer.js";
|
||||||
import importUtils from "./utils.js";
|
import importUtils from "./utils.js";
|
||||||
@ -46,9 +59,6 @@ function renderToHtml(content: string, title: string) {
|
|||||||
// h1 handling needs to come before sanitization
|
// h1 handling needs to come before sanitization
|
||||||
html = importUtils.handleH1(html, title);
|
html = importUtils.handleH1(html, title);
|
||||||
html = htmlSanitizer.sanitize(html);
|
html = htmlSanitizer.sanitize(html);
|
||||||
html = minifyHtml(html, {
|
|
||||||
collapseWhitespace: true
|
|
||||||
});
|
|
||||||
|
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user