Skip to content

Commit

Permalink
Merge pull request #695 from skyweb331/main
Browse files Browse the repository at this point in the history
Improve markdown italicizing when following tags
  • Loading branch information
thienlnam authored May 29, 2024
2 parents 05c03d4 + 4ef55d7 commit 315f810
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
10 changes: 10 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ test('Test italic markdown replacement', () => {
expect(parser.replace(italicTestStartString)).toBe(italicTestReplacedString);
});

test('Test italic markdown replacement', () => {
const italicTestStartString = 'Note that _this is correctly italicized_\n'
+ 'Note that `_this is correctly not italicized_` and _following inline contents are italicized_'

const italicTestReplacedString = 'Note that <em>this is correctly italicized</em><br />'
+ 'Note that <code>_this is correctly not italicized_</code> and <em>following inline contents are italicized</em>'

expect(parser.replace(italicTestStartString)).toBe(italicTestReplacedString);
});

// Multi-line text wrapped in _ is successfully replaced with <em></em>
test('Test multi-line italic markdown replacement', () => {
const testString = '_Here is a multi-line\ncomment that should\nbe italic_ \n_\n_test\n_';
Expand Down
25 changes: 13 additions & 12 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,25 @@ export default class ExpensiMark {
return `<blockquote>${isStartingWithSpace ? ' ' : ''}${replacedText}</blockquote>`;
},
},
/**
* Use \b in this case because it will match on words, letters,
* and _: https://www.rexegg.com/regex-boundaries.html#wordboundary
* Use [\s\S]* instead of .* to match newline
*/
{
/**
* Use \b in this case because it will match on words, letters,
* and _: https://www.rexegg.com/regex-boundaries.html#wordboundary
* The !_blank is to prevent the `target="_blank">` section of the
* link replacement from being captured Additionally, something like
* `\b\_([^<>]*?)\_\b` doesn't work because it won't replace
* `_https://www.test.com_`
* Use [\s\S]* instead of .* to match newline
*/
name: 'italic',
regex: /(?<!<[^>]*)(\b_+|\b)(?!_blank")_((?![\s_])[\s\S]*?[^\s_](?<!\s))_(?![^\W_])(?![^<]*>)(?![^<]*(<\/pre>|<\/code>|<\/a>|<\/mention-user>|_blank))/g,
regex: /(<(pre|code|a|mention-user)[^>]*>(.*?)<\/\2>)|((\b_+|\b)_((?![\s_])[\s\S]*?[^\s_](?<!\s))_(?![^\W_])(?![^<]*>)(?![^<]*(<\/pre>|<\/code>|<\/a>|<\/mention-user>)))/g,
replacement: (match, html, tag, content, text, extraLeadingUnderscores, textWithinUnderscores) => {
// Skip any <pre>, <code>, <a>, <mention-user> tag contents
if (html) {
return html;
}

// We want to add extraLeadingUnderscores back before the <em> tag unless textWithinUnderscores starts with valid email
replacement: (match, extraLeadingUnderscores, textWithinUnderscores) => {
// If any tags are included inside underscores, ignore it. ie. _abc <pre>pre tag</pre> abc_
if (textWithinUnderscores.includes('</pre>') || this.containsNonPairTag(textWithinUnderscores)) {
return match;
}

if (String(textWithinUnderscores).match(`^${Constants.CONST.REG_EXP.MARKDOWN_EMAIL}`)) {
return `<em>${extraLeadingUnderscores}${textWithinUnderscores}</em>`;
}
Expand Down

0 comments on commit 315f810

Please sign in to comment.