Skip to content

Commit

Permalink
Merge pull request #693 from dragnoir/fix-40571
Browse files Browse the repository at this point in the history
Fix: multi level blockquote HTML to Markdown
  • Loading branch information
MonilBhavsar authored May 29, 2024
2 parents b805fed + 32e14db commit 6cfc572
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
12 changes: 12 additions & 0 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@ test('map div with quotes', () => {
expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

test('double quotes in same line', () => {
const testString = '<blockquote><blockquote>line 1</blockquote></blockquote>';
const resultString = '>> line 1';
expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

test('triple quotes in same line', () => {
const testString = '<blockquote><blockquote><blockquote>line 1</blockquote></blockquote></blockquote>';
const resultString = '>>> line 1';
expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

test('map table to newline', () => {
const testString = '<tbody><tr>line 1</tr><tr>line 2</tr></tbody>';
const resultString = 'line 1\nline 2';
Expand Down
28 changes: 26 additions & 2 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,32 @@ export default class ExpensiMark {
.trim()
.split('\n');

const prependGreaterSign = (m) => `> ${m}`;
resultString = _.map(resultString, prependGreaterSign).join('\n');
// Wrap each string in the array with <blockquote> and </blockquote>
// Define a named function to wrap each line with blockquote
function wrapWithBlockquote(line) {
return `<blockquote>${line}</blockquote>`;
}

// Use _.map with the named function
resultString = _.map(resultString, wrapWithBlockquote);

function processString(m) {
// Recursive function to replace nested <blockquote> with ">"
function replaceBlockquotes(text) {
let modifiedText = text;
let depth;
do {
depth = (modifiedText.match(/<blockquote>/gi) || []).length;
modifiedText = modifiedText.replace(/<blockquote>/gi, '');
modifiedText = modifiedText.replace(/<\/blockquote>/gi, '');
} while (/<blockquote>/i.test(modifiedText));
return `${'>'.repeat(depth)} ${modifiedText}`;
}
return replaceBlockquotes(m);
}

resultString = _.map(resultString, processString).join('\n');

// We want to keep <blockquote> tag here and let method replaceBlockElementWithNewLine to handle the line break later
return `<blockquote>${resultString}</blockquote>`;
},
Expand Down

0 comments on commit 6cfc572

Please sign in to comment.