Skip to content

Commit

Permalink
Fix HTML to markdown parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid committed Nov 22, 2024
1 parent 4665020 commit a1511ab
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('</blockquote>'))}</blockquote><br />`;
});
const splittedText = parsedText.split('<br />');
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('<blockquote>')) {
count += (line.match(/<blockquote>/g) || []).length;
}
if (line.endsWith('</blockquote>')) {
count -= (line.match(/<\/blockquote>/g) || []).length;
if (count > 0) {
return `${line}${'<blockquote>'.repeat(count)}`;
}
}

if (count > 0) {
return `${line}${'</blockquote>'}${'<blockquote>'.repeat(count)}`;
}

return line + (index < arr.length - 1 ? '<br />' : '');
})
.join('');

return parsedText;
}

/**
* Replaces HTML with markdown
*/
Expand All @@ -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
Expand Down

0 comments on commit a1511ab

Please sign in to comment.