Skip to content

Commit

Permalink
Changes in quotes and added tests for quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carolina lucini authored and Carolina lucini committed Jun 15, 2021
1 parent a963e2c commit 4bbf18a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 47 deletions.
32 changes: 32 additions & 0 deletions __tests__/ExpensiMark-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,35 @@ test('Test markdown and url links with inconsistent starting and closing parens'

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

test('Test quotes markdown replacement with text matching inside and outside codefence without spaces', () => {
const testString = 'The next line should be quoted\n>Hello,I’mtext\n```\nThe next line should not be quoted\n>Hello,I’mtext\nsince its inside a codefence```';

const resultString = 'The next line should be quoted<br><blockquote>Hello,I’mtext</blockquote><pre>The&#32;next&#32;line&#32;should&#32;not&#32;be&#32;quoted<br>&gt;Hello,I’mtext<br>since&#32;its&#32;inside&#32;a&#32;codefence</pre>';

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

test('Test quotes markdown replacement with text matching inside and outside codefence at the same line', () => {
const testString = 'The next line should be quoted\n&gt;Hello,I’mtext\nThe next line should not be quoted\n```&gt;Hello,I’mtext```\nsince its inside a codefence';

const resultString = 'The next line should be quoted<br><blockquote>Hello,I’mtext</blockquote>The next line should not be quoted<br><pre>&gt;Hello,I’mtext</pre><br>since its inside a codefence';

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

test('Test quotes markdown replacement with text matching inside and outside codefence at the end of the text', () => {
const testString = 'The next line should be quoted\n&gt;Hello,I’mtext\nThe next line should not be quoted\n```&gt;Hello,I’mtext```';

const resultString = 'The next line should be quoted<br><blockquote>Hello,I’mtext</blockquote>The next line should not be quoted<br><pre>&gt;Hello,I’mtext</pre>';

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

test('Test quotes markdown replacement with text matching inside and outside codefence with quotes at the end of the text', () => {
const testString = 'The next line should be quoted\n```&gt;Hello,I’mtext```\nThe next line should not be quoted\n&gt;Hello,I’mtext';

const resultString = 'The next line should be quoted<br><pre>&gt;Hello,I’mtext</pre><br>The next line should not be quoted<br><blockquote>Hello,I’mtext</blockquote>';

expect(parser.replace(testString)).toBe(resultString);
});
86 changes: 39 additions & 47 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ export default class ExpensiMark {
// 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) => {
process: (textToProcess, replacement) => {
const regex = new RegExp(
/\n?^&gt; ?(?![^<]*(?:<\/pre>|<\/code>))([^\v\n\r]+)\n?/gm
);
return this.modifyTextForQuote(regex, textToProcess);
return this.modifyTextForQuote(regex, textToProcess, replacement);
},
replacement: g1 => (
`<blockquote>${g1}</blockquote>`
),
},
{
name: 'newline',
Expand Down Expand Up @@ -260,90 +263,79 @@ export default class ExpensiMark {
*
* @param {RegExp} regex
* @param {String} textToCheck
* @param {Function} replacement
*
* @returns {String}
*/

modifyTextForQuote(regex, textToCheck) {
modifyTextForQuote(regex, textToCheck, replacement) {
let replacedText = '';
let TextToFormat = '';
const codefenceSplitted = [];
let textToFormat = '';
const match = textToCheck.match(regex);
const match_codefence = textToCheck.match(/<pre>(\s|\S)+<\/pre>/gm);

// if there's matches we need to modify the quotes
if (match !== null) {
// split all the codefences in lines
for (let i = 0; i < match_codefence.length; i++) {
codefenceSplitted[i] = match_codefence[i].split('\n');
}
let insideCodefence = false;

// split the textToCheck in lines
const textSplitted = textToCheck.split('\n');

for (let i = 0; i < textSplitted.length; i++) {
let isCodeFence = false;
for (let j = 0; j < codefenceSplitted.length; j++) {
for (let k = 0; k < codefenceSplitted[j].length; k++) {
// we need to know when is codefenced
if (textSplitted[i] === codefenceSplitted[j][k]) {
isCodeFence = true;
}
}
if (!insideCodefence) {
// we need to know when there is a start of codefence so we dont quote
insideCodefence = Str.contains(textSplitted[i], '<pre>');
}

// we only want to modify lines starting with &gt that is not codefence;
if (textSplitted[i].substr(0, 4) === '&gt;' && !isCodeFence) {
// we only want to modify lines starting with &gt; that is not codefence
if (Str.startsWith(textSplitted[i], '&gt;') && !insideCodefence) {
// avoid blank lines starting with &gt;
if (textSplitted[i].trim() !== '&gt;') {
textSplitted[i] = textSplitted[i].substr(4, textSplitted[i].length);
textSplitted[i] = textSplitted[i].substr(4).trim();
textSplitted[i] = textSplitted[i].trim();
TextToFormat += `${textSplitted[i]}\n`;
textToFormat += `${textSplitted[i]}\n`;

// we need ensure that index i-1 >= 0
} else if (i > 0) {
// certificate that we will have only one blank row
if (textSplitted[i - 1].trim() !== '') {
textSplitted[i] = textSplitted[i].substr(4, textSplitted[i].length);
textSplitted[i] = textSplitted[i].trim();
TextToFormat += `${textSplitted[i]}\n`;
} else {
// doesnt append to textToFormat if current index row and current index row -1 was blank
textSplitted[i] = textSplitted[i].substr(4, textSplitted[i].length);
textSplitted[i] = textSplitted[i].substr(4).trim();
textSplitted[i] = textSplitted[i].trim();
textToFormat += `${textSplitted[i]}\n`;
}
}

// we dont want a \n after the textSplitted if it is an end of codefence(it will do the same as the else, but without \n)
} else if (isCodeFence && textSplitted[i].substr(textSplitted[i].length - 6, textSplitted[i].length) === '</pre>') {
// make sure we will only modify if we have Text needed to be formatted for quote
if (TextToFormat !== '') {
TextToFormat = TextToFormat.replace(/\n$/, '');
replacedText += `<blockquote>${TextToFormat}</blockquote>`;
TextToFormat = '';
}
replacedText += `${textSplitted[i]}`;
} else {
// make sure we will only modify if we have Text needed to be formatted for quote
if (TextToFormat !== '') {
TextToFormat = TextToFormat.replace(/\n$/, '');
replacedText += `<blockquote>${TextToFormat}</blockquote>`;
TextToFormat = '';
if (textToFormat !== '') {
textToFormat = textToFormat.replace(/\n$/, '');
replacedText += replacement(textToFormat);
textToFormat = '';
}

// if index === last row
if (i === textSplitted.length - 1) {
// we dont want a \n after the textSplitted if it is the last row
replacedText += `${textSplitted[i]}`;
} else {
replacedText += `${textSplitted[i]}\n`;
}

// we need to know when we are not inside codefence anymore
if (insideCodefence) {
insideCodefence = !Str.contains(textSplitted[i], '</pre>');
}
replacedText += `${textSplitted[i]}\n`;
}
}

// when loop ends we need the last quote to be formatted if we have quotes at last rows
if (TextToFormat !== '') {
TextToFormat = TextToFormat.replace(/\n$/, '');
replacedText += `<blockquote>${TextToFormat}</blockquote>`;
if (textToFormat !== '') {
textToFormat = textToFormat.replace(/\n$/, '');
replacedText += replacement(textToFormat);
}

// replace all the blank lines between quotes, if there are only blank lines between them
replacedText = replacedText.replace(/(<\/blockquote>((\s*)+)<blockquote>)/g, '</blockquote><blockquote>');
} else {
// if we doesnt have matches make sure the function will return the same textToCheck
// if we doesnt have matches make sure the function will return the same textToCheck
replacedText = textToCheck;
}
return replacedText;
Expand Down

0 comments on commit 4bbf18a

Please sign in to comment.