Skip to content

Commit

Permalink
Merge pull request #618 from byte1012/fix-#26941
Browse files Browse the repository at this point in the history
Do not apply Italic marckdown for multiline-quote message
  • Loading branch information
johnmlee101 authored Dec 19, 2023
2 parents b7f2a13 + ab2c300 commit c6bb3cf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
17 changes: 17 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ test('Test italic markdown replacement with word boundary and undercores', () =>
expect(parser.replace(testString)).toBe(replacedString);
});

test('Test quote markdown combined multi-line italic/bold/strikethrough markdown replacement', () => {
let testString = '_test\n> test_';
let replacedString = '_test<br /><blockquote>test_</blockquote>';

expect(parser.replace(testString)).toBe(replacedString);

testString = '*test\n> test*';
replacedString = '*test<br /><blockquote>test*</blockquote>';

expect(parser.replace(testString)).toBe(replacedString);

testString = '~test\n> test~';
replacedString = '~test<br /><blockquote>test~</blockquote>';

expect(parser.replace(testString)).toBe(replacedString);
});

// Words wrapped in ~ successfully replaced with <del></del>
test('Test strikethrough markdown replacement', () => {
const strikethroughTestStartString = 'This is a ~sentence,~ and it has some ~punctuation, words, and spaces~. ~test~ ~ testing~ test~test~test. ~ testing ~ ~testing ~';
Expand Down
57 changes: 29 additions & 28 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,34 @@ export default class ExpensiMark {
return `${g1}<a href="${href}" target="_blank" rel="noreferrer noopener">${g2}</a>${g1}`;
},
},

{
name: 'quote',

// We also want to capture a blank line before or after the quote so that we do not add extra spaces.
// block quotes naturally appear on their own line. Blockquotes should not appear in code fences or
// inline code blocks. A single prepending space should be stripped if it exists
process: (textToProcess, replacement, shouldKeepWhitespace = false) => {
const regex = new RegExp(
/^&gt; *(?! )(?![^<]*(?:<\/pre>|<\/code>))([^\v\n\r]+)/gm,
);
if (shouldKeepWhitespace) {
return textToProcess.replace(regex, g1 => replacement(g1, shouldKeepWhitespace));
}
return this.modifyTextForQuote(regex, textToProcess, replacement);
},
replacement: (g1, shouldKeepWhitespace = false) => {
// We want to enable 2 options of nested heading inside the blockquote: "># heading" and "> # heading".
// To do this we need to parse body of the quote without first space
let isStartingWithSpace = false;
const textToReplace = g1.replace(/^&gt;( )?/gm, (match, g2) => {
isStartingWithSpace = !!g2;
return '';
});
const replacedText = this.replace(textToReplace, {filterRules: ['heading1'], shouldEscapeText: false, shouldKeepWhitespace});
return `<blockquote>${isStartingWithSpace ? ' ' : ''}${replacedText}</blockquote>`;
},
},
{
/**
* Use \b in this case because it will match on words, letters,
Expand Down Expand Up @@ -196,39 +224,12 @@ export default class ExpensiMark {
{
name: 'autoEmail',
regex: new RegExp(
`([^\\w'#%+-]|^)${CONST.REG_EXP.MARKDOWN_EMAIL}(?!((?:(?!<a).)+)?<\\/a>|[^<>]*<\\/(?!em|h1))`,
`([^\\w'#%+-]|^)${CONST.REG_EXP.MARKDOWN_EMAIL}(?!((?:(?!<a).)+)?<\\/a>|[^<>]*<\\/(?!em|h1|blockquote))`,
'gim',
),
replacement: '$1<a href="mailto:$2">$2</a>',
},

{
name: 'quote',

// We also want to capture a blank line before or after the quote so that we do not add extra spaces.
// block quotes naturally appear on their own line. Blockquotes should not appear in code fences or
// inline code blocks. A single prepending space should be stripped if it exists
process: (textToProcess, replacement, shouldKeepWhitespace = false) => {
const regex = new RegExp(
/^&gt; *(?! )(?![^<]*(?:<\/pre>|<\/code>))([^\v\n\r]+)/gm,
);
if (shouldKeepWhitespace) {
return textToProcess.replace(regex, g1 => replacement(g1, shouldKeepWhitespace));
}
return this.modifyTextForQuote(regex, textToProcess, replacement);
},
replacement: (g1, shouldKeepWhitespace = false) => {
// We want to enable 2 options of nested heading inside the blockquote: "># heading" and "> # heading".
// To do this we need to parse body of the quote without first space
let isStartingWithSpace = false;
const textToReplace = g1.replace(/^&gt;( )?/gm, (match, g2) => {
isStartingWithSpace = !!g2;
return '';
});
const replacedText = this.replace(textToReplace, {filterRules: ['heading1'], shouldEscapeText: false, shouldKeepWhitespace});
return `<blockquote>${isStartingWithSpace ? ' ' : ''}${replacedText}</blockquote>`;
},
},
{
// Use \B in this case because \b doesn't match * or ~.
// \B will match everything that \b doesn't, so it works
Expand Down

0 comments on commit c6bb3cf

Please sign in to comment.