Skip to content

Fix to not add superfluous spaces for code (text) #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/handle/inline-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export function inlineCode(node, _, context) {
// first or last character are a space, eol, or tick, then pad with spaces.
if (
/[^ \r\n]/.test(value) &&
(/[ \r\n`]/.test(value.charAt(0)) ||
/[ \r\n`]/.test(value.charAt(value.length - 1)))
((/^[ \r\n]/.test(value) && /[ \r\n]$/.test(value)) || /^`|`$/.test(value))
) {
value = ' ' + value + ' '
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"index.js"
],
"dependencies": {
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"@types/mdast": "3.0.3",
"@types/unist": "2.0.4",
"longest-streak": "^3.0.0",
"mdast-util-to-string": "^3.0.0",
"parse-entities": "^3.0.0",
Expand Down
16 changes: 11 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1436,16 +1436,22 @@ test('Code text', (t) => {
'should pad w/ a space if the value ends w/ a grave accent'
)

t.equal(
to({type: 'inlineCode', value: ' a '}),
'` a `\n',
'should pad w/ a space if the value starts and ends w/ a space'
)

t.equal(
to({type: 'inlineCode', value: ' a'}),
'` a `\n',
'should pad w/ a space if the value starts w/ a space'
'` a`\n',
'should not pad w/ spaces if the value ends w/ a non-space'
)

t.equal(
to({type: 'inlineCode', value: 'a '}),
'` a `\n',
'should pad w/ a space if the value ends w/ a space'
'`a `\n',
'should not pad w/ spaces if the value starts w/ a non-space'
)

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

t.equal(
to({type: 'inlineCode', value: 'a\n1. '}),
'` a 1. `\n',
'`a 1. `\n',
'should prevent breaking out of code (\\d\\.)'
)

Expand Down