Skip to content
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

Fix non-ASCII whitespace in inline code #424

Merged
merged 2 commits into from
Jul 15, 2019
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
2 changes: 1 addition & 1 deletion packages/remark-parse/lib/tokenize/code-inline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

var whitespace = require('is-whitespace-character')
var locate = require('../locate/code-inline')
var whitespace = require('../util/is-markdown-whitespace-character')

module.exports = inlineCode
inlineCode.locator = locate
Expand Down
27 changes: 27 additions & 0 deletions packages/remark-parse/lib/util/is-markdown-whitespace-character.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

module.exports = whitespace

var tab = 9 // '\t'
var lineFeed = 10 // '\n'
var lineTabulation = 11 // '\v'
var formFeed = 12 // '\f'
var carriageReturn = 13 // '\r'
var space = 32 // ' '

function whitespace(char) {
/* istanbul ignore next - `number` handling for future */
var code = typeof char === 'number' ? char : char.charCodeAt(0)

switch (code) {
case tab:
case lineFeed:
case lineTabulation:
case formFeed:
case carriageReturn:
case space:
return true
default:
return false
}
}
3 changes: 3 additions & 0 deletions test/fixtures/input/code-inline-whitespace.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Markdown strips regular whitespaces: ` hello `
Including tabs: ` hello `
However it preserves non-breaking whitespaces: `   hello   `
144 changes: 144 additions & 0 deletions test/fixtures/tree/code-inline-whitespace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Markdown strips regular whitespaces: ",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 38,
"offset": 37
},
"indent": []
}
},
{
"type": "inlineCode",
"value": "hello",
"position": {
"start": {
"line": 1,
"column": 38,
"offset": 37
},
"end": {
"line": 1,
"column": 51,
"offset": 50
},
"indent": []
}
},
{
"type": "text",
"value": "\nIncluding tabs: ",
"position": {
"start": {
"line": 1,
"column": 51,
"offset": 50
},
"end": {
"line": 2,
"column": 17,
"offset": 67
},
"indent": [
1
]
}
},
{
"type": "inlineCode",
"value": "hello",
"position": {
"start": {
"line": 2,
"column": 17,
"offset": 67
},
"end": {
"line": 2,
"column": 26,
"offset": 76
},
"indent": []
}
},
{
"type": "text",
"value": "\nHowever it preserves non-breaking whitespaces: ",
"position": {
"start": {
"line": 2,
"column": 26,
"offset": 76
},
"end": {
"line": 3,
"column": 48,
"offset": 124
},
"indent": [
1
]
}
},
{
"type": "inlineCode",
"value": "   hello   ",
"position": {
"start": {
"line": 3,
"column": 48,
"offset": 124
},
"end": {
"line": 3,
"column": 61,
"offset": 137
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 3,
"column": 61,
"offset": 137
},
"indent": [
1,
1
]
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 4,
"column": 1,
"offset": 138
}
}
}