Improve markdown to HTML conversion by trimming breaks around block elements

This commit is contained in:
2026-01-31 16:30:01 +01:00
parent 03deb8de1c
commit fa8d2807e8

View File

@@ -191,6 +191,19 @@ export function markdownToHTML(text) {
// 6) Convert line-breaks to <br /> for NON-code content (preserve blank lines)
html = html.replace(/\n/g, '<br />');
// 6.1) Trim breaks around block elements (headings/lists/etc.) to avoid double spacing
const brPattern = "<br\\s*\\/?>";
const blockPattern = "(?:h[1-4]|hr|table|ul|ol|blockquote)";
html = html
.replace(
new RegExp(`(${brPattern}\\s*)+(<(?:${blockPattern})\\b[^>]*>)`, "g"),
"$2"
)
.replace(
new RegExp(`(<\\/(?:${blockPattern})>)(?:\\s*${brPattern})+`, "g"),
"$1"
);
// 7) Restore code blocks with header + copy button
html = html.replace(/@@CODEBLOCK(\d+)@@/g, (_, idx) => {
const { lang, code } = codeblocks[+idx];