Skip to content

Commit

Permalink
Fix tilde, backtick in fenced code info string
Browse files Browse the repository at this point in the history
Related to mdx-js/mdx#630.
Closes GH-421.
Closes GH-422.

Reviewed-by: Marouane Fazouane <fazouanem3@gmail.com>
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: John Otander <johnotander@gmail.com>
  • Loading branch information
wooorm committed Jul 15, 2019
1 parent c73f409 commit cc2463d
Show file tree
Hide file tree
Showing 5 changed files with 933 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/remark-parse/lib/tokenize/code-fenced.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ function fencedCode(eat, value, silent) {

if (
character === lineFeed ||
character === tilde ||
character === graveAccent
(marker === graveAccent && character === marker)
) {
break
}
Expand Down
8 changes: 8 additions & 0 deletions packages/remark-stringify/lib/visitors/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports = code

var lineFeed = '\n'
var space = ' '
var tilde = '~'
var graveAccent = '`'

// Stringify code.
// Creates indented code when:
Expand Down Expand Up @@ -73,6 +75,12 @@ function code(node, parent) {
return pad(value, 1)
}

// Backticks in the info string don’t work with backtick fenced code.
// Backticks (and tildes) are fine in tilde fenced code.
if (marker === graveAccent && info.indexOf(graveAccent) !== -1) {
marker = tilde
}

fence = repeat(marker, Math.max(streak(value, marker) + 1, 3))

return fence + info + lineFeed + value + lineFeed + fence
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/input/fenced-code-info-with-marker.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
If the info string comes after a backtick fence, it may not contain any backtick
characters.
The reason for this restriction is that otherwise some inline code would be
incorrectly interpreted as the beginning of a fenced code block.

Tildes in info strings are fine:

```js filename=~/.bashrc
console.log(1)
```

Tildes in info strings are fine after tilde fences:

~~~js filename=~/.bashrc
console.log(1)
~~~

Backticks in info strings are fine after tilde fences:

~~~js title=`.bashrc`
console.log(1)
~~~

Backticks in info strings are **not** fine after backtick fences:

```js title=`.bashrc`
console.log(1)
```
Loading

0 comments on commit cc2463d

Please sign in to comment.