Skip to content

Commit

Permalink
Fix: multi level blockquote HTML to Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
dragnoir committed May 9, 2024
1 parent a018512 commit 0a1e26e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,28 @@ export default class ExpensiMark {
regex: /<(blockquote|q)(?:"[^"]*"|'[^']*'|[^'">])*>([\s\S]*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))/gi,
replacement: (match, g1, g2) => {

Check failure on line 424 in lib/ExpensiMark.js

View workflow job for this annotation

GitHub Actions / lint

'g1' is defined but never used

Check failure on line 424 in lib/ExpensiMark.js

View workflow job for this annotation

GitHub Actions / lint

'g2' is defined but never used
// We remove the line break before heading inside quote to avoid adding extra line
let resultString = g2
let resultString = match
.replace(/\n?(<h1># )/g, '$1')
.replace(/(<h1>|<\/h1>)+/g, '\n')
.trim()
.split('\n');

const prependGreaterSign = (m) => `> ${m}`;
resultString = _.map(resultString, prependGreaterSign).join('\n');
resultString = _.map(resultString, (m) => {

Check failure on line 432 in lib/ExpensiMark.js

View workflow job for this annotation

GitHub Actions / lint

Prefer named functions
// Recursive function to replace nested <blockquote> with ">"
function replaceBlockquotes(text) {
while (/<blockquote>/i.test(text)) {

Check failure on line 435 in lib/ExpensiMark.js

View workflow job for this annotation

GitHub Actions / lint

Invalid loop. Its body allows only one iteration
// Count how many <blockquote> tags
let depth = (text.match(/<blockquote>/gi) || []).length;

Check failure on line 437 in lib/ExpensiMark.js

View workflow job for this annotation

GitHub Actions / lint

'depth' is never reassigned. Use 'const' instead
// Replace all blockquote tags and add ">" per depth level
text = text.replace(/<blockquote>/gi, '');

Check failure on line 439 in lib/ExpensiMark.js

View workflow job for this annotation

GitHub Actions / lint

Assignment to function parameter 'text'
text = text.replace(/<\/blockquote>/gi, '');

Check failure on line 440 in lib/ExpensiMark.js

View workflow job for this annotation

GitHub Actions / lint

Assignment to function parameter 'text'
return `${'>'.repeat(depth)} ${text}`;
}
return text;
}
return replaceBlockquotes(m);
}).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 0a1e26e

Please sign in to comment.