Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve markdown italicizing when following tags #695

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 14 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sobitneupane This is done by my vscode prettier... seems like no meaningful change.

replacement: '<mention-report>$1</mention-report>',
},

Expand Down Expand Up @@ -282,24 +282,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
Loading