Skip to content

Commit

Permalink
add support for nested quotes up to 3
Browse files Browse the repository at this point in the history
  • Loading branch information
robertKozik committed Feb 19, 2024
1 parent 83ae619 commit f13385d
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,22 @@ export default class ExpensiMark {
// To do this we need to parse body of the quote without first space
let isStartingWithSpace = false;
const textToReplace = g1.replace(/^>( )?/gm, (match, g2) => {
isStartingWithSpace = !!g2;
return '';
if (shouldKeepRawInput) {
isStartingWithSpace = !!g2;
return '';
}
return match;
});
const replacedText = this.replace(textToReplace, {filterRules: ['heading1'], shouldEscapeText: false, shouldKeepRawInput});
const filterRules = ['heading1'];

// if we don't reach the max quote depth we allow the recursive call to process possible quote
if (this.currentQuoteDepth < this.maxQuoteDepth) {
filterRules.push('quote');
this.currentQuoteDepth++;
}

const replacedText = this.replace(textToReplace, {filterRules, shouldEscapeText: false, shouldKeepRawInput});
this.currentQuoteDepth = 1;
return `<blockquote>${isStartingWithSpace ? ' ' : ''}${replacedText}</blockquote>`;
},
},
Expand Down Expand Up @@ -456,7 +468,19 @@ export default class ExpensiMark {
* The list of rules that have to be applied when shouldKeepWhitespace flag is true.
* @type {Object[]}
*/
this.shouldKeepWhitespaceRules = this.rules.filter(rule => !this.whitespaceRulesToDisable.includes(rule.name)).map(rule => rule.name);
this.shouldKeepWhitespaceRules = this.rules.filter(rule => !this.whitespaceRulesToDisable.includes(rule.name));

/**
* maxQuoteDepth is the maximum depth of nested quotes that we want to support.
* @type {Number}
*/
this.maxQuoteDepth = 2;

/**
* currentQuoteDepth is the current depth of nested quotes that we are processing.
* @type {Number}
*/
this.currentQuoteDepth = 0;
}

/**
Expand All @@ -473,8 +497,8 @@ export default class ExpensiMark {
replace(text, {filterRules = [], shouldEscapeText = true, shouldKeepRawInput = false} = {}) {
// This ensures that any html the user puts into the comment field shows as raw html
let replacedText = shouldEscapeText ? _.escape(text) : text;
const excludeRules = shouldKeepRawInput ? _.union(this.shouldKeepWhitespaceRules, filterRules) : filterRules;
const rules = _.isEmpty(excludeRules) ? this.rules : _.filter(this.rules, rule => _.contains(excludeRules, rule.name));
const enabledRules = shouldKeepRawInput ? this.shouldKeepWhitespaceRules : this.rules;
const rules = _.isEmpty(filterRules) ? enabledRules : _.filter(this.rules, rule => _.contains(filterRules, rule.name));

try {
rules.forEach((rule) => {
Expand Down

0 comments on commit f13385d

Please sign in to comment.