Skip to content

Commit e0111f8

Browse files
committed
Refactor to improve bundle size
1 parent 6adfc5f commit e0111f8

File tree

3 files changed

+97
-93
lines changed

3 files changed

+97
-93
lines changed

Diff for: index.js

+68-92
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
'use strict'
22

3-
var vfileLocation = require('vfile-location')
4-
var toString = require('nlcst-to-string')
5-
var position = require('unist-util-position')
6-
var phrasing = require('hast-util-phrasing')
73
var embedded = require('hast-util-embedded')
8-
var whitespace = require('hast-util-whitespace')
4+
var convert = require('hast-util-is-element/convert')
5+
var phrasing = require('hast-util-phrasing')
96
var textContent = require('hast-util-to-string')
10-
var is = require('hast-util-is-element')
7+
var whitespace = require('hast-util-whitespace')
8+
var toString = require('nlcst-to-string')
9+
var position = require('unist-util-position')
10+
var vfileLocation = require('vfile-location')
1111

1212
module.exports = toNlcst
1313

14-
var source = ['code']
15-
var ignore = ['script', 'style', 'svg', 'math', 'del']
16-
var explicit = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
14+
var push = [].push
1715

18-
var flowAccepting = [
16+
var source = convert(['code', dataNlcstSourced])
17+
var ignore = convert(['script', 'style', 'svg', 'math', 'del', dataNlcstIgnore])
18+
var explicit = convert(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
19+
20+
var flowAccepting = convert([
1921
'body',
2022
'article',
2123
'section',
@@ -39,7 +41,10 @@ var flowAccepting = [
3941
'fieldset',
4042
'details',
4143
'dialog'
42-
]
44+
])
45+
46+
// See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
47+
var unravelInParagraph = convert(['a', 'ins', 'del', 'map'])
4348

4449
// Transform `tree` to nlcst.
4550
function toNlcst(tree, file, Parser) {
@@ -66,8 +71,8 @@ function toNlcst(tree, file, Parser) {
6671
throw new Error('hast-util-to-nlcst expected position on nodes')
6772
}
6873

69-
location = vfileLocation(file)
7074
doc = String(file)
75+
location = vfileLocation(doc)
7176
parser = 'parse' in Parser ? Parser : new Parser()
7277

7378
// Transform hast to nlcst, and pass these into `parser.parse` to insert
@@ -79,55 +84,44 @@ function toNlcst(tree, file, Parser) {
7984
return {
8085
type: 'RootNode',
8186
children: results,
82-
position: {
83-
start: location.toPosition(0),
84-
end: location.toPosition(doc.length)
85-
}
87+
position: {start: location.toPoint(0), end: location.toPoint(doc.length)}
8688
}
8789

8890
function find(node) {
89-
var children = node.children
90-
9191
if (node.type === 'root') {
92-
findAll(children)
93-
} else if (is(node) && !ignored(node)) {
94-
if (is(node, explicit)) {
92+
findAll(node.children)
93+
} else if (node.type === 'element' && !ignore(node)) {
94+
if (explicit(node)) {
9595
// Explicit paragraph.
9696
add(node)
97-
} else if (is(node, flowAccepting)) {
97+
} else if (flowAccepting(node)) {
9898
// Slightly simplified version of: <https://html.spec.whatwg.org/#paragraphs>.
99-
implicit(flattenAll(children))
99+
implicit(flattenAll(node.children))
100100
} else {
101101
// Dig deeper.
102-
findAll(children)
102+
findAll(node.children)
103103
}
104104
}
105105
}
106106

107107
function findAll(children) {
108-
var length = children.length
109108
var index = -1
110109

111-
while (++index < length) {
110+
while (++index < children.length) {
112111
find(children[index])
113112
}
114113
}
115114

116-
function flatten(node) {
117-
if (is(node, ['a', 'ins', 'del', 'map'])) {
118-
return flattenAll(node.children)
119-
}
120-
121-
return node
122-
}
123-
124115
function flattenAll(children) {
125116
var results = []
126-
var length = children.length
127117
var index = -1
128118

129-
while (++index < length) {
130-
results = results.concat(flatten(children[index]))
119+
while (++index < children.length) {
120+
if (unravelInParagraph(children[index])) {
121+
push.apply(results, flattenAll(children[index].children))
122+
} else {
123+
results.push(children[index])
124+
}
131125
}
132126

133127
return results
@@ -136,25 +130,22 @@ function toNlcst(tree, file, Parser) {
136130
function add(node) {
137131
var result = ('length' in node ? all : one)(node)
138132

139-
if (result.length > 0) {
133+
if (result.length) {
140134
results.push(parser.tokenizeParagraph(result))
141135
}
142136
}
143137

144138
function implicit(children) {
145-
var length = children.length + 1
146139
var index = -1
147-
var viable = false
148140
var start = -1
141+
var viable
149142
var child
150143

151-
while (++index < length) {
144+
while (++index <= children.length) {
152145
child = children[index]
153146

154147
if (child && phrasing(child)) {
155-
if (start === -1) {
156-
start = index
157-
}
148+
if (start === -1) start = index
158149

159150
if (!viable && !embedded(child) && !whitespace(child)) {
160151
viable = true
@@ -169,60 +160,50 @@ function toNlcst(tree, file, Parser) {
169160
find(child)
170161
}
171162

172-
viable = false
163+
viable = null
173164
start = -1
174165
}
175166
}
176167
}
177168

178169
// Convert `node` (hast) to nlcst.
179170
function one(node) {
180-
var type = node.type
181-
var tagName = type === 'element' ? node.tagName : null
182-
var change
183171
var replacement
172+
var change
184173

185-
if (type === 'text') {
186-
change = true
174+
if (node.type === 'text') {
187175
replacement = parser.tokenize(node.value)
188-
} else if (tagName === 'wbr') {
189176
change = true
190-
replacement = [parser.tokenizeWhiteSpace(' ')]
191-
} else if (tagName === 'br') {
192-
change = true
193-
replacement = [parser.tokenizeWhiteSpace('\n')]
194-
} else if (sourced(node)) {
195-
change = true
196-
replacement = [parser.tokenizeSource(textContent(node))]
197-
} else if (type === 'root' || !ignored(node)) {
198-
replacement = all(node.children)
199-
} else {
200-
return
201-
}
202-
203-
if (!change) {
204-
return replacement
177+
} else if (node.type === 'element' && !ignore(node)) {
178+
if (node.tagName === 'wbr') {
179+
replacement = [parser.tokenizeWhiteSpace(' ')]
180+
change = true
181+
} else if (node.tagName === 'br') {
182+
replacement = [parser.tokenizeWhiteSpace('\n')]
183+
change = true
184+
} else if (source(node)) {
185+
replacement = [parser.tokenizeSource(textContent(node))]
186+
change = true
187+
} else {
188+
replacement = all(node.children)
189+
}
205190
}
206191

207-
return patch(replacement, location, location.toOffset(position.start(node)))
192+
return change
193+
? patch(replacement, location, location.toOffset(position.start(node)))
194+
: replacement
208195
}
209196

210197
// Convert all `children` (hast) to nlcst.
211198
function all(children) {
212-
var length = children && children.length
199+
var results = []
213200
var index = -1
214-
var result = []
215-
var child
216-
217-
while (++index < length) {
218-
child = one(children[index])
219201

220-
if (child) {
221-
result = result.concat(child)
222-
}
202+
while (++index < children.length) {
203+
push.apply(results, one(children[index]) || [])
223204
}
224205

225-
return result
206+
return results
226207
}
227208

228209
// Patch a position on each node in `nodes`.
@@ -231,26 +212,23 @@ function toNlcst(tree, file, Parser) {
231212
// Note that nlcst nodes are concrete, meaning that their starting and ending
232213
// positions can be inferred from their content.
233214
function patch(nodes, location, offset) {
234-
var length = nodes.length
235215
var index = -1
236216
var start = offset
237-
var children
238-
var node
239217
var end
218+
var node
240219

241-
while (++index < length) {
220+
while (++index < nodes.length) {
242221
node = nodes[index]
243-
children = node.children
244222

245-
if (children) {
246-
patch(children, location, start)
223+
if (node.children) {
224+
patch(node.children, location, start)
247225
}
248226

249227
end = start + toString(node).length
250228

251229
node.position = {
252-
start: location.toPosition(start),
253-
end: location.toPosition(end)
230+
start: location.toPoint(start),
231+
end: location.toPoint(end)
254232
}
255233

256234
start = end
@@ -260,12 +238,10 @@ function toNlcst(tree, file, Parser) {
260238
}
261239
}
262240

263-
function sourced(node) {
264-
var props = node.properties
265-
return is(node) && (is(node, source) || props.dataNlcst === 'source')
241+
function dataNlcstSourced(node) {
242+
return node.properties.dataNlcst === 'source'
266243
}
267244

268-
function ignored(node) {
269-
var props = node.properties
270-
return is(node) && (is(node, ignore) || props.dataNlcst === 'ignore')
245+
function dataNlcstIgnore(node) {
246+
return node.properties.dataNlcst === 'ignore'
271247
}

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"hast-util-whitespace": "^1.0.0",
3838
"nlcst-to-string": "^2.0.0",
3939
"unist-util-position": "^3.0.0",
40-
"vfile-location": "^3.0.0"
40+
"vfile-location": "^3.1.0"
4141
},
4242
"devDependencies": {
4343
"browserify": "^17.0.0",
@@ -83,6 +83,7 @@
8383
"prettier": true,
8484
"esnext": false,
8585
"rules": {
86+
"unicorn/explicit-length-check": "off",
8687
"unicorn/no-fn-reference-in-iterator": "off",
8788
"unicorn/prefer-optional-catch-binding": "off"
8889
},

Diff for: test/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,33 @@ test('hast-util-to-nlcst', function (t) {
135135
st.end()
136136
})
137137

138+
t.test('should accept comments', function (st) {
139+
var node = toNlcst(
140+
{
141+
type: 'comment',
142+
value: 'a',
143+
position: {start: {line: 1, column: 1}, end: {line: 1, column: 9}}
144+
},
145+
vfile('<!--a-->'),
146+
Latin
147+
)
148+
149+
st.deepEqual(
150+
node,
151+
{
152+
type: 'RootNode',
153+
children: [],
154+
position: {
155+
start: {line: 1, column: 1, offset: 0},
156+
end: {line: 1, column: 9, offset: 8}
157+
}
158+
},
159+
'should support comments'
160+
)
161+
162+
st.end()
163+
})
164+
138165
t.end()
139166
})
140167

0 commit comments

Comments
 (0)