Enhance markdownToHTML to handle empty lines and content separation
This commit is contained in:
@@ -192,10 +192,24 @@ export function markdownToHTML(text) {
|
|||||||
const linesWithHtml = html.split("\n");
|
const linesWithHtml = html.split("\n");
|
||||||
const htmlLines = [];
|
const htmlLines = [];
|
||||||
let paragraph = [];
|
let paragraph = [];
|
||||||
|
let emptyCount = 0;
|
||||||
|
let seenContent = false;
|
||||||
const flushParagraph = () => {
|
const flushParagraph = () => {
|
||||||
if (paragraph.length === 0) return;
|
if (paragraph.length === 0) return;
|
||||||
htmlLines.push(`<p class="md-paragraph">${paragraph.join("<br />")}</p>`);
|
htmlLines.push(`<p class="md-paragraph">${paragraph.join("<br />")}</p>`);
|
||||||
paragraph = [];
|
paragraph = [];
|
||||||
|
seenContent = true;
|
||||||
|
};
|
||||||
|
const pushExtraBreaks = () => {
|
||||||
|
if (!seenContent) {
|
||||||
|
emptyCount = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const extraBreaks = Math.max(0, emptyCount - 1);
|
||||||
|
for (let j = 0; j < extraBreaks; j += 1) {
|
||||||
|
htmlLines.push("<br />");
|
||||||
|
}
|
||||||
|
emptyCount = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
const isBlockLine = (value) => {
|
const isBlockLine = (value) => {
|
||||||
@@ -211,14 +225,24 @@ export function markdownToHTML(text) {
|
|||||||
for (let i = 0; i < linesWithHtml.length; i += 1) {
|
for (let i = 0; i < linesWithHtml.length; i += 1) {
|
||||||
const line = linesWithHtml[i] ?? "";
|
const line = linesWithHtml[i] ?? "";
|
||||||
if (line.trim().length === 0) {
|
if (line.trim().length === 0) {
|
||||||
flushParagraph();
|
if (paragraph.length > 0) {
|
||||||
|
flushParagraph();
|
||||||
|
}
|
||||||
|
emptyCount += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (isBlockLine(line)) {
|
if (isBlockLine(line)) {
|
||||||
flushParagraph();
|
if (paragraph.length > 0) {
|
||||||
|
flushParagraph();
|
||||||
|
}
|
||||||
|
pushExtraBreaks();
|
||||||
htmlLines.push(line);
|
htmlLines.push(line);
|
||||||
|
seenContent = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (emptyCount > 0) {
|
||||||
|
pushExtraBreaks();
|
||||||
|
}
|
||||||
paragraph.push(line);
|
paragraph.push(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user