Skip to content

Commit

Permalink
fix adding spaces after # in edit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid committed Nov 14, 2023
1 parent 8459b1b commit 901302c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<blockquote><h1> Hello world</h1></blockquote>';

expect(parser.replace(quoteTestStartString, {shouldKeepWhitespace: true})).toBe(quoteTestReplacedString);
});

describe('trailing whitespace', () => {
test('nothing', () => {
const quoteTestStartString = '>Hello world!';
Expand Down
13 changes: 9 additions & 4 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export default class ExpensiMark {

{
name: 'heading1',
regex: /^# +(?! )((?:(?!<pre>|\n|\r\n).)+)/gm,
process: (textToProcess, replacement, shouldKeepWhitespace = false) => {
if (shouldKeepWhitespace) {
return textToProcess.replace(/^# ( *(?! )(?:(?!<pre>|\n|\r\n).)+)/gm, replacement);
}
return textToProcess.replace(/^# +(?! )((?:(?!<pre>|\n|\r\n).)+)/gm, replacement);
},
replacement: '<h1>$1</h1>',
},

Expand Down Expand Up @@ -200,19 +205,19 @@ export default class ExpensiMark {
/^&gt; *(?! )(?![^<]*(?:<\/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;
const textToReplace = g1.replace(/^&gt;( )?/gm, (match, g2) => {
isStartingWithSpace = !!g2;
return '';
});
const replacedText = this.replace(textToReplace, {filterRules: ['heading1'], shouldEscapeText: false});
const replacedText = this.replace(textToReplace, {filterRules: ['heading1'], shouldEscapeText: false, shouldKeepWhitespace});
return `<blockquote>${isStartingWithSpace ? ' ' : ''}${replacedText}</blockquote>`;
},
},
Expand Down

0 comments on commit 901302c

Please sign in to comment.