Skip to content

Commit

Permalink
Fix initial, final newlines in fenced code
Browse files Browse the repository at this point in the history
Closes GH-351.
  • Loading branch information
wooorm committed Jul 15, 2019
1 parent a47c3c9 commit 8d8582c
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 9 deletions.
15 changes: 7 additions & 8 deletions packages/remark-parse/lib/tokenize/code-fenced.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var trim = require('trim-trailing-lines')

module.exports = fencedCode

var lineFeed = '\n'
Expand Down Expand Up @@ -142,6 +140,7 @@ function fencedCode(eat, value, silent) {
exdentedClosing = ''
content = ''
exdentedContent = ''
var skip = true

// Eat content.
while (index < length) {
Expand All @@ -158,13 +157,13 @@ function fencedCode(eat, value, silent) {
continue
}

// Add the newline to `subvalue` if its the first character. Otherwise,
// add it to the `closing` queue.
if (content) {
// The first line feed is ignored. Others aren’t.
if (skip) {
subvalue += character
skip = false
} else {
closing += character
exdentedClosing += character
} else {
subvalue += character
}

queue = ''
Expand Down Expand Up @@ -250,6 +249,6 @@ function fencedCode(eat, value, silent) {
type: 'code',
lang: lang || flag || null,
meta: meta || null,
value: trim(exdentedContent)
value: exdentedContent
})
}
8 changes: 7 additions & 1 deletion packages/remark-stringify/lib/visitors/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ function code(node, parent) {
info = self.encode(self.escape(info, node))

// Without (needed) fences.
if (!info && !options.fences && value) {
if (
!info &&
!options.fences &&
value &&
value.charAt(0) !== lineFeed &&
value.charAt(value.length - 1) !== lineFeed
) {
// Throw when pedantic, in a list item which isn’t compiled using a tab.
if (
parent &&
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/input/fenced-code-initial-final-newlines.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Initial:

```


123
```

Final:

```
123


```

Both:

```


123


```
196 changes: 196 additions & 0 deletions test/fixtures/tree/fenced-code-initial-final-newlines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Initial:",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 9,
"offset": 8
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 9,
"offset": 8
},
"indent": []
}
},
{
"type": "code",
"lang": null,
"meta": null,
"value": "\n\n123",
"position": {
"start": {
"line": 3,
"column": 1,
"offset": 10
},
"end": {
"line": 7,
"column": 4,
"offset": 23
},
"indent": [
1,
1,
1,
1
]
}
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Final:",
"position": {
"start": {
"line": 9,
"column": 1,
"offset": 25
},
"end": {
"line": 9,
"column": 7,
"offset": 31
},
"indent": []
}
}
],
"position": {
"start": {
"line": 9,
"column": 1,
"offset": 25
},
"end": {
"line": 9,
"column": 7,
"offset": 31
},
"indent": []
}
},
{
"type": "code",
"lang": null,
"meta": null,
"value": "123\n\n",
"position": {
"start": {
"line": 11,
"column": 1,
"offset": 33
},
"end": {
"line": 15,
"column": 4,
"offset": 46
},
"indent": [
1,
1,
1,
1
]
}
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Both:",
"position": {
"start": {
"line": 17,
"column": 1,
"offset": 48
},
"end": {
"line": 17,
"column": 6,
"offset": 53
},
"indent": []
}
}
],
"position": {
"start": {
"line": 17,
"column": 1,
"offset": 48
},
"end": {
"line": 17,
"column": 6,
"offset": 53
},
"indent": []
}
},
{
"type": "code",
"lang": null,
"meta": null,
"value": "\n\n123\n\n",
"position": {
"start": {
"line": 19,
"column": 1,
"offset": 55
},
"end": {
"line": 25,
"column": 4,
"offset": 70
},
"indent": [
1,
1,
1,
1,
1,
1
]
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 26,
"column": 1,
"offset": 71
}
}
}

0 comments on commit 8d8582c

Please sign in to comment.