Skip to content

Commit 06eba4b

Browse files
committed
Fix to remove unneeded spaces around line endings
* Remove initial and final spaces around line endings * Collapse text nodes * Fix to use `''` for empty `alt` instead of `null` * Update dev-dependencies * Update fixtures for new remark formatter (notably, commonmark breaks, better email links, `()` instead of `(<>)` for missing URLs on links and images)
1 parent 8fd303d commit 06eba4b

File tree

22 files changed

+158
-108
lines changed

22 files changed

+158
-108
lines changed

index.js

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
module.exports = toMdast
44

55
var minify = require('rehype-minify-whitespace')
6+
var convert = require('unist-util-is/convert')
67
var visit = require('unist-util-visit')
78
var xtend = require('xtend')
89
var one = require('./lib/one')
910
var handlers = require('./lib/handlers')
1011

12+
var block = convert(['heading', 'paragraph', 'root'])
13+
1114
function toMdast(tree, options) {
1215
var settings = options || {}
1316
var byId = {}
17+
var mdast
1418

1519
h.nodeById = byId
1620
h.baseFound = false
@@ -26,11 +30,15 @@ function toMdast(tree, options) {
2630
h.unchecked = settings.unchecked || '[ ]'
2731
h.quotes = settings.quotes || ['"']
2832

29-
visit(tree, onvisit)
33+
visit(tree, 'element', onelement)
3034

3135
minify({newlines: settings.newlines === true})(tree)
3236

33-
return one(h, tree, null)
37+
mdast = one(h, tree, null)
38+
39+
visit(mdast, 'text', ontext)
40+
41+
return mdast
3442

3543
function h(node, type, props, children) {
3644
var result
@@ -65,12 +73,47 @@ function toMdast(tree, options) {
6573
return right
6674
}
6775

68-
function onvisit(node) {
69-
var props = node.properties || {}
70-
var id = props.id
76+
function onelement(node) {
77+
var id = node.properties.id
7178

72-
if (id && !(id in node)) {
79+
if (id && !(id in byId)) {
7380
byId[id] = node
7481
}
7582
}
83+
84+
// Collapse text nodes, and fix whitespace.
85+
// Most of this is taken care of by `rehype-minify-whitespace`, but
86+
// we’re generating some whitespace too, and some nodes are in the end
87+
// ignored.
88+
// So clean up:
89+
function ontext(node, index, parent) {
90+
var previous = parent.children[index - 1]
91+
92+
if (previous && node.type === previous.type) {
93+
previous.value += node.value
94+
95+
parent.children.splice(index, 1)
96+
97+
if (previous.position && node.position) {
98+
previous.position.end = node.position.end
99+
}
100+
101+
// Iterate over the previous node again, to handle its total value.
102+
return index - 1
103+
}
104+
105+
node.value = node.value.replace(/[\t ]*(\r?\n|\r)[\t ]*/, '$1')
106+
107+
// We don’t care about other phrasing nodes in between (e.g., `[ asd ]()`),
108+
// as there the whitespace matters.
109+
if (block(parent)) {
110+
if (!index) {
111+
node.value = node.value.replace(/^[\t ]+/, '')
112+
}
113+
114+
if (index === parent.children.length - 1) {
115+
node.value = node.value.replace(/[\t ]+$/, '')
116+
}
117+
}
118+
}
76119
}

lib/handlers/dl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function dataList(h, node) {
5252
group = groups[index]
5353
group = handle(h, group.titles).concat(handle(h, group.definitions))
5454

55-
if (group.length !== 0) {
55+
if (group.length > 0) {
5656
content.push({
5757
type: 'listItem',
5858
spread: group.length > 1,
@@ -63,7 +63,7 @@ function dataList(h, node) {
6363
}
6464

6565
// Create a list if there are items.
66-
if (content.length !== 0) {
66+
if (content.length > 0) {
6767
return h(
6868
node,
6969
'list',

lib/handlers/image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function image(h, node) {
88
var props = {
99
url: resolve(h, node.properties.src),
1010
title: node.properties.title || null,
11-
alt: node.properties.alt || null
11+
alt: node.properties.alt || ''
1212
}
1313

1414
return h(node, 'image', props)

lib/handlers/paragraph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function paragraph(h, node) {
88
var children = node.children
99
var nodes = all(h, node)
1010

11-
if (children && children.length !== 0 && nodes.length === 0) {
11+
if (children && children.length > 0 && nodes.length === 0) {
1212
return
1313
}
1414

lib/handlers/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function select(h, node) {
1717
results.push(value[1] ? value[1] + ' (' + value[0] + ')' : value[0])
1818
}
1919

20-
if (results.length !== 0) {
20+
if (results.length > 0) {
2121
return h(node, 'text', wrapText(h, results.join(', ')))
2222
}
2323
}

package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,31 @@
4040
"rehype-minify-whitespace": "^4.0.3",
4141
"repeat-string": "^1.6.1",
4242
"trim-trailing-lines": "^1.1.0",
43+
"unist-util-is": "^4.0.0",
4344
"unist-util-visit": "^2.0.0",
4445
"xtend": "^4.0.1"
4546
},
4647
"devDependencies": {
47-
"hastscript": "^5.0.0",
48+
"hastscript": "^6.0.0",
4849
"is-hidden": "^1.0.0",
4950
"mdast-util-assert": "^3.0.0",
5051
"negate": "^1.0.0",
5152
"nyc": "^15.0.0",
5253
"prettier": "^2.0.0",
5354
"rehype-parse": "^7.0.0",
54-
"remark-cli": "^8.0.0",
55-
"remark-parse": "^8.0.0",
56-
"remark-preset-wooorm": "^7.0.0",
57-
"remark-stringify": "^8.0.0",
55+
"remark-cli": "^9.0.0",
56+
"remark-gfm": "^1.0.0",
57+
"remark-parse": "^9.0.0",
58+
"remark-preset-wooorm": "^8.0.0",
59+
"remark-stringify": "^9.0.0",
5860
"tape": "^5.0.0",
5961
"unified": "^9.0.0",
6062
"unist-builder": "^2.0.0",
61-
"unist-util-remove-position": "^2.0.0",
62-
"xo": "^0.32.0"
63+
"unist-util-remove-position": "^3.0.0",
64+
"xo": "^0.34.0"
6365
},
6466
"scripts": {
65-
"format": "remark . -qfo && prettier . --write && xo --fix",
67+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6668
"test-api": "node test",
6769
"test-coverage": "nyc --reporter lcov tape test/index.js",
6870
"test": "npm run format && npm run test-coverage"

test/fixtures/a/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
[example](http://example.com)
44

5-
[example](<>)
5+
[example]()
66

7-
[](<>)
7+
[]()

test/fixtures/address/index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
You can contact author at [www.somedomain.com](http://www.somedomain.com/contact).··
2-
If you see any bugs, please [contact webmaster](mailto:[email protected]).··
3-
You may also want to visit us:··
4-
Mozilla Foundation··
5-
1981 Landings Drive··
6-
Building K··
7-
Mountain View, CA 94043-0801··
1+
You can contact author at [www.somedomain.com](http://www.somedomain.com/contact).\
2+
If you see any bugs, please [contact webmaster](mailto:[email protected]).\
3+
You may also want to visit us:\
4+
Mozilla Foundation\
5+
1981 Landings Drive\
6+
Building K\
7+
Mountain View, CA 94043-0801\
88
USA

test/fixtures/base-invalid/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
[example](../relative.html)
88

9-
[example](<>)
9+
[example]()

test/fixtures/base/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
[example](http://example.com/relative.html)
88

9-
[example](<>)
9+
[example]()

0 commit comments

Comments
 (0)