Skip to content

Commit

Permalink
improve markdown italicizing when following tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyweb331 committed May 29, 2024
1 parent 923dd7f commit c1cfdae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 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
22 changes: 9 additions & 13 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default class ExpensiMark {
{
name: 'reportMentions',

regex: /(?<![^ \n*~_])(#[\p{Ll}0-9-]{1,80})/gmiu,
regex: /(?<![^ \n*~_])(#[\p{Ll}0-9-]{1,80})/gimu,
replacement: '<mention-report>$1</mention-report>',
},

Expand Down Expand Up @@ -283,23 +283,19 @@ export default class ExpensiMark {
},
},
{
/**
* 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 c1cfdae

Please sign in to comment.