diff --git a/src/Tokenizer.js b/src/Tokenizer.js index 9cb3eedebe..c507538aab 100644 --- a/src/Tokenizer.js +++ b/src/Tokenizer.js @@ -481,9 +481,9 @@ module.exports = class Tokenizer { codespan(src) { const cap = this.rules.inline.code.exec(src); if (cap) { - let text = cap[2]; - const hasNonSpaceChars = /\S/.test(text); - const hasSpaceCharsOnBothEnds = /^\s/.test(text) && /\s$/.test(text); + let text = cap[2].replace(/\n/g, ' '); + const hasNonSpaceChars = /[^ ]/.test(text); + const hasSpaceCharsOnBothEnds = text.startsWith(' ') && text.endsWith(' '); if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) { text = text.substring(1, text.length - 1); } @@ -491,9 +491,7 @@ module.exports = class Tokenizer { return { type: 'codespan', raw: cap[0], - text: !this.options.pedantic - ? text.replace(/\n/g, ' ') - : text + text }; } } diff --git a/test/unit/Lexer-spec.js b/test/unit/Lexer-spec.js index 79e5f8ecf9..9b2b5d7612 100644 --- a/test/unit/Lexer-spec.js +++ b/test/unit/Lexer-spec.js @@ -762,7 +762,7 @@ a | b }); }); - it('only spaces', () => { + it('only spaces not stripped', () => { expectInlineTokens({ md: '` `', tokens: [ @@ -771,7 +771,7 @@ a | b }); }); - it('beginning space', () => { + it('beginning space only not stripped', () => { expectInlineTokens({ md: '` a`', tokens: [ @@ -780,7 +780,7 @@ a | b }); }); - it('end space', () => { + it('end space only not stripped', () => { expectInlineTokens({ md: '`a `', tokens: [ @@ -789,7 +789,7 @@ a | b }); }); - it('begin and end space', () => { + it('begin and end spaces are stripped', () => { expectInlineTokens({ md: '` a `', tokens: [ @@ -798,7 +798,34 @@ a | b }); }); - it('begin and end multiple space', () => { + it('begin and end newlines are stripped', () => { + expectInlineTokens({ + md: '`\na\n`', + tokens: [ + { type: 'codespan', raw: '`\na\n`', text: 'a' } + ] + }); + }); + + it('begin and end tabs are not stripped', () => { + expectInlineTokens({ + md: '`\ta\t`', + tokens: [ + { type: 'codespan', raw: '`\ta\t`', text: '\ta\t' } + ] + }); + }); + + it('begin and end newlines', () => { + expectInlineTokens({ + md: '`\na\n`', + tokens: [ + { type: 'codespan', raw: '`\na\n`', text: 'a' } + ] + }); + }); + + it('begin and end multiple spaces only one stripped', () => { expectInlineTokens({ md: '` a `', tokens: [