diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index ed6cfaa9..9f083bfc 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -1110,6 +1110,43 @@ export default class ExpensiMark { return joinedText; } + unpackNestedQuotes(text: string): string { + let parsedText = text.replace(/(<\/blockquote>)+/g, (match) => { + return `${match.slice(0, match.lastIndexOf(''))}
`; + }); + 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 === '') { + return ''; + } + + if (line.startsWith('
')) { + count += (line.match(/
/g) || []).length; + } + if (line.endsWith('
')) { + count -= (line.match(/<\/blockquote>/g) || []).length; + if (count > 0) { + return `${line}${'
'.repeat(count)}`; + } + } + + if (count > 0) { + return `${line}${'
'}${'
'.repeat(count)}`; + } + + return line + (index < arr.length - 1 ? '
' : ''); + }) + .join(''); + + return parsedText; + } + /** * Replaces HTML with markdown */ @@ -1122,6 +1159,7 @@ export default class ExpensiMark { if (parseBodyTag) { generatedMarkdown = parseBodyTag[2]; } + generatedMarkdown = this.unpackNestedQuotes(generatedMarkdown); const processRule = (rule: RuleWithRegex) => { // Pre-processes input HTML before applying regex