From e93727f3a966f6d8e0a4f32f8bcdaafc478c2829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Ska=C5=82ka?= Date: Fri, 22 Nov 2024 19:24:33 +0100 Subject: [PATCH] Fix html to markdown tests --- lib/ExpensiMark.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index 9f083bfc..32de59a6 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -1097,8 +1097,9 @@ export default class ExpensiMark { return; } + const nextItem = splitText?.[index + 1]; // Insert '\n' unless it ends with '\n' or '>' or it's the last element, or if it's a header ('# ') with a space. - if (text.match(/[\n|>][>]?[\s]?$/) || index === splitText.length - 1 || text === '# ') { + if ((nextItem && text.match(/>[\s]?$/) && !nextItem.startsWith('> ')) || text.match(/\n[\s]?$/) || index === splitText.length - 1 || text === '# ') { joinedText += text; } else { joinedText += `${text}\n`; @@ -1111,20 +1112,22 @@ export default class ExpensiMark { } unpackNestedQuotes(text: string): string { - let parsedText = text.replace(/(<\/blockquote>)+/g, (match) => { - return `${match.slice(0, match.lastIndexOf(''))}
`; + let parsedText = text.replace(/((<\/blockquote>)+(
)?)|(
)/g, (match) => { + return `${match}`; }); - const splittedText = parsedText.split('
'); + const splittedText = parsedText.split(''); if (splittedText.length > 0 && splittedText[splittedText.length - 1] === '') { splittedText.pop(); } let count = 0; parsedText = splittedText - .map((line, index, arr) => { - if (line === '') { + .map((l) => { + const hasBR = l.endsWith('
'); + if (l === '' && count === 0) { return ''; } + const line = l.replace(/(
)$/g, ''); if (line.startsWith('
')) { count += (line.match(/
/g) || []).length; @@ -1140,7 +1143,7 @@ export default class ExpensiMark { return `${line}${'
'}${'
'.repeat(count)}`; } - return line + (index < arr.length - 1 ? '
' : ''); + return line + (hasBR ? '
' : ''); }) .join('');