Skip to content

Commit eed5115

Browse files
author
李文富
authored
Fix to not add superfluous spaces for code (text)
Related to syntax-tree/unist#46. Closes GH-36. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent d51bf74 commit eed5115

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

lib/handle/inline-code.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export function inlineCode(node, _, context) {
2727
// first or last character are a space, eol, or tick, then pad with spaces.
2828
if (
2929
/[^ \r\n]/.test(value) &&
30-
(/[ \r\n`]/.test(value.charAt(0)) ||
31-
/[ \r\n`]/.test(value.charAt(value.length - 1)))
30+
((/^[ \r\n]/.test(value) && /[ \r\n]$/.test(value)) || /^`|`$/.test(value))
3231
) {
3332
value = ' ' + value + ' '
3433
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
"index.js"
3939
],
4040
"dependencies": {
41-
"@types/mdast": "^3.0.0",
42-
"@types/unist": "^2.0.0",
41+
"@types/mdast": "3.0.3",
42+
"@types/unist": "2.0.4",
4343
"longest-streak": "^3.0.0",
4444
"mdast-util-to-string": "^3.0.0",
4545
"parse-entities": "^3.0.0",

test/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,16 +1436,22 @@ test('Code text', (t) => {
14361436
'should pad w/ a space if the value ends w/ a grave accent'
14371437
)
14381438

1439+
t.equal(
1440+
to({type: 'inlineCode', value: ' a '}),
1441+
'` a `\n',
1442+
'should pad w/ a space if the value starts and ends w/ a space'
1443+
)
1444+
14391445
t.equal(
14401446
to({type: 'inlineCode', value: ' a'}),
1441-
'` a `\n',
1442-
'should pad w/ a space if the value starts w/ a space'
1447+
'` a`\n',
1448+
'should not pad w/ spaces if the value ends w/ a non-space'
14431449
)
14441450

14451451
t.equal(
14461452
to({type: 'inlineCode', value: 'a '}),
1447-
'` a `\n',
1448-
'should pad w/ a space if the value ends w/ a space'
1453+
'`a `\n',
1454+
'should not pad w/ spaces if the value starts w/ a non-space'
14491455
)
14501456

14511457
t.equal(
@@ -1462,7 +1468,7 @@ test('Code text', (t) => {
14621468

14631469
t.equal(
14641470
to({type: 'inlineCode', value: 'a\n1. '}),
1465-
'` a 1. `\n',
1471+
'`a 1. `\n',
14661472
'should prevent breaking out of code (\\d\\.)'
14671473
)
14681474

0 commit comments

Comments
 (0)