diff --git a/src/services/import/markdown.ts b/src/services/import/markdown.ts index e42846404..bff72e592 100644 --- a/src/services/import/markdown.ts +++ b/src/services/import/markdown.ts @@ -1,36 +1,49 @@ "use strict"; import { parse, Renderer, type Tokens } from "marked"; -import { minify as minifyHtml } from "html-minifier"; + +class CustomMarkdownRenderer extends Renderer { + + 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) { + return ""; + } + + const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang); + return `
${text}
`; + } + + blockquote({ tokens }: Tokens.Blockquote): string { + const body = renderer.parser.parse(tokens); + + const admonitionMatch = /^

\[\!([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(/^

\[\!([A-Z]+)\]/, "

") + .replace(/^

<\/p>/, ""); // Having a heading will generate an empty paragraph that we need to remove. + + return `

\n`; + } + } + + return `
\n${body}
\n`; + } + +} // Keep renderer code up to date with https://github.com/markedjs/marked/blob/master/src/Renderer.ts. -const renderer = new Renderer({ async: false }); -renderer.code = ({ text, lang, escaped }: Tokens.Code) => { - if (!text) { - return ""; - } - - const ckEditorLanguage = getNormalizedMimeFromMarkdownLanguage(lang); - return `
${text}
`; -}; -renderer.blockquote = ({ tokens }: Tokens.Blockquote) => { - const body = renderer.parser.parse(tokens); - - const admonitionMatch = /^

\[\!([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(/^

\[\!([A-Z]+)\]/, "

") - .replace(/^

<\/p>/, ""); // Having a heading will generate an empty paragraph that we need to remove. - - return `

\n`; - } - } - - return `
\n${body}
\n`; -}; +const renderer = new CustomMarkdownRenderer({ async: false }); import htmlSanitizer from "../html_sanitizer.js"; import importUtils from "./utils.js"; @@ -46,9 +59,6 @@ function renderToHtml(content: string, title: string) { // h1 handling needs to come before sanitization html = importUtils.handleH1(html, title); html = htmlSanitizer.sanitize(html); - html = minifyHtml(html, { - collapseWhitespace: true - }); return html; }