Skip to content

Commit

Permalink
only strip spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Apr 24, 2020
1 parent 6ad20ac commit c2d6d2f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/Tokenizer.js
Expand Up @@ -481,19 +481,17 @@ 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);
}
text = escape(text, true);
return {
type: 'codespan',
raw: cap[0],
text: !this.options.pedantic
? text.replace(/\n/g, ' ')
: text
text
};
}
}
Expand Down
37 changes: 32 additions & 5 deletions test/unit/Lexer-spec.js
Expand Up @@ -762,7 +762,7 @@ a | b
});
});

it('only spaces', () => {
it('only spaces not stripped', () => {
expectInlineTokens({
md: '` `',
tokens: [
Expand All @@ -771,7 +771,7 @@ a | b
});
});

it('beginning space', () => {
it('beginning space only not stripped', () => {
expectInlineTokens({
md: '` a`',
tokens: [
Expand All @@ -780,7 +780,7 @@ a | b
});
});

it('end space', () => {
it('end space only not stripped', () => {
expectInlineTokens({
md: '`a `',
tokens: [
Expand All @@ -789,7 +789,7 @@ a | b
});
});

it('begin and end space', () => {
it('begin and end spaces are stripped', () => {
expectInlineTokens({
md: '` a `',
tokens: [
Expand All @@ -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: [
Expand Down

0 comments on commit c2d6d2f

Please sign in to comment.