diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index d31b5dee..b9d47c12 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -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 this is correctly italicized
' + + 'Note that _this is correctly not italicized_ and following inline contents are italicized' + + expect(parser.replace(italicTestStartString)).toBe(italicTestReplacedString); +}); + // Multi-line text wrapped in _ is successfully replaced with test('Test multi-line italic markdown replacement', () => { const testString = '_Here is a multi-line\ncomment that should\nbe italic_ \n_\n_test\n_'; diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index 4b8a7eb2..90728fd8 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -177,7 +177,7 @@ export default class ExpensiMark { { name: 'reportMentions', - regex: /(?$1', }, @@ -282,24 +282,25 @@ export default class ExpensiMark { return `
${isStartingWithSpace ? ' ' : ''}${replacedText}
`; }, }, + /** + * 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_](?)(?![^<]*(<\/pre>|<\/code>|<\/a>|<\/mention-user>|_blank))/g, + regex: /(<(pre|code|a|mention-user)[^>]*>(.*?)<\/\2>)|((\b_+|\b)_((?![\s_])[\s\S]*?[^\s_](?)(?![^<]*(<\/pre>|<\/code>|<\/a>|<\/mention-user>)))/g, + replacement: (match, html, tag, content, text, extraLeadingUnderscores, textWithinUnderscores) => { + // Skip any
, , ,  tag contents
+                    if (html) {
+                        return html;
+                    }
 
-                // We want to add extraLeadingUnderscores back before the  tag unless textWithinUnderscores starts with valid email
-                replacement: (match, extraLeadingUnderscores, textWithinUnderscores) => {
+                    // If any tags are included inside underscores, ignore it. ie. _abc 
pre tag
abc_ if (textWithinUnderscores.includes('
') || this.containsNonPairTag(textWithinUnderscores)) { return match; } + if (String(textWithinUnderscores).match(`^${Constants.CONST.REG_EXP.MARKDOWN_EMAIL}`)) { return `${extraLeadingUnderscores}${textWithinUnderscores}`; }