diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 2d7be7ca..17f235c5 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -1542,6 +1542,13 @@ describe('edit mode', () => { expect(parser.replace(quoteTestStartString, {shouldKeepWhitespace: true})).toBe(quoteTestReplacedString); }); + test('nested quote and heading with many spaces after #', () => { + const quoteTestStartString = '># Hello world'; + const quoteTestReplacedString = '

Hello world

'; + + expect(parser.replace(quoteTestStartString, {shouldKeepWhitespace: true})).toBe(quoteTestReplacedString); + }); + describe('trailing whitespace', () => { test('nothing', () => { const quoteTestStartString = '>Hello world!'; diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index ea496fe2..894ec385 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -74,7 +74,12 @@ export default class ExpensiMark { { name: 'heading1', - regex: /^# +(?! )((?:(?!
|\n|\r\n).)+)/gm,
+                process: (textToProcess, replacement, shouldKeepWhitespace = false) => {
+                    if (shouldKeepWhitespace) {
+                        return textToProcess.replace(/^# ( *(?! )(?:(?!
|\n|\r\n).)+)/gm, replacement);
+                    }
+                    return textToProcess.replace(/^# +(?! )((?:(?!
|\n|\r\n).)+)/gm, replacement);
+                },
                 replacement: '

$1

', }, @@ -200,11 +205,11 @@ export default class ExpensiMark { /^> *(?! )(?![^<]*(?:<\/pre>|<\/code>))([^\v\n\r]+)/gm, ); if (shouldKeepWhitespace) { - return textToProcess.replace(regex, replacement); + return textToProcess.replace(regex, g1 => replacement(g1, shouldKeepWhitespace)); } return this.modifyTextForQuote(regex, textToProcess, replacement); }, - replacement: (g1) => { + 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; @@ -212,7 +217,7 @@ export default class ExpensiMark { isStartingWithSpace = !!g2; return ''; }); - const replacedText = this.replace(textToReplace, {filterRules: ['heading1'], shouldEscapeText: false}); + const replacedText = this.replace(textToReplace, {filterRules: ['heading1'], shouldEscapeText: false, shouldKeepWhitespace}); return `
${isStartingWithSpace ? ' ' : ''}${replacedText}
`; }, },