Skip to content

Commit bd0e69d

Browse files
committed
Refactor code-style
1 parent 0d1dd1b commit bd0e69d

File tree

1 file changed

+80
-33
lines changed

1 file changed

+80
-33
lines changed

lib/index.js

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
/**
2-
* @typedef {import('unist').Point} Point
3-
*
42
* @typedef {import('nlcst').Root} NlcstRoot
53
* @typedef {import('nlcst').Paragraph} NlcstParagraph
6-
* @typedef {import('nlcst').WhiteSpace} NlcstWhiteSpace
74
* @typedef {import('nlcst').Sentence} NlcstSentence
8-
* @typedef {import('nlcst').Source} NlcstSource
95
* @typedef {import('nlcst').Content} NlcstContent
106
* @typedef {import('nlcst').SentenceContent} NlcstSentenceContent
11-
* @typedef {NlcstRoot|NlcstContent} NlcstNode
12-
* @typedef {Extract<NlcstNode, import('unist').Parent>} NlcstParent
13-
*
147
* @typedef {import('hast').Root} HastRoot
158
* @typedef {import('hast').Element} HastElement
169
* @typedef {import('hast').Content} HastContent
1710
* @typedef {import('hast').ElementContent} HastElementContent
18-
* @typedef {HastRoot|HastContent} HastNode
19-
* @typedef {Extract<HastNode, import('unist').Parent>} HastParent
20-
*
2111
* @typedef {import('vfile').VFile} VFile
12+
*/
13+
14+
/**
15+
* @typedef {NlcstRoot | NlcstContent} NlcstNode
16+
* @typedef {Extract<NlcstNode, import('unist').Parent>} NlcstParent
17+
* @typedef {HastRoot | HastContent} HastNode
18+
*
2219
*
2320
* @typedef {{
2421
* tokenizeSentencePlugins: Array<(node: NlcstSentence) => void>,
@@ -81,12 +78,16 @@ const flowAccepting = convertElement([
8178
const terminalMarker = /^([!.?\u2026\u203D]+)$/
8279

8380
/**
84-
* Transform `tree` to nlcst.
81+
* Turn a hast tree into an nlcst tree.
8582
*
8683
* @param {HastNode} tree
84+
* hast tree to transform.
8785
* @param {VFile} file
88-
* @param {ParserInstance|ParserConstructor} Parser
86+
* Virtual file.
87+
* @param {ParserInstance | ParserConstructor} Parser
88+
* Parser to use.
8989
* @returns {NlcstRoot}
90+
* nlcst tree.
9091
*/
9192
export function toNlcst(tree, file, Parser) {
9293
// Warn for invalid parameters.
@@ -122,7 +123,12 @@ export function toNlcst(tree, file, Parser) {
122123
}
123124

124125
/**
126+
* Transform a hast node.
127+
*
125128
* @param {HastNode} node
129+
* hast node.
130+
* @returns {void}
131+
* Nothing.
126132
*/
127133
function find(node) {
128134
if (node.type === 'root') {
@@ -132,7 +138,8 @@ export function toNlcst(tree, file, Parser) {
132138
// Explicit paragraph.
133139
add(node)
134140
} else if (flowAccepting(node)) {
135-
// Slightly simplified version of: <https://html.spec.whatwg.org/#paragraphs>.
141+
// Slightly simplified version of:
142+
// <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>.
136143
implicit(flattenAll(node.children))
137144
} else {
138145
// Dig deeper.
@@ -142,7 +149,12 @@ export function toNlcst(tree, file, Parser) {
142149
}
143150

144151
/**
152+
* Transform hast children.
153+
*
145154
* @param {Array<HastContent>} children
155+
* hast children.
156+
* @returns {void}
157+
* Nothing.
146158
*/
147159
function findAll(children) {
148160
let index = -1
@@ -153,8 +165,12 @@ export function toNlcst(tree, file, Parser) {
153165
}
154166

155167
/**
168+
* Flatten hast children: this unravels `a`, `ins`, `del`, and `map` elements.
169+
*
156170
* @param {Array<HastElementContent>} children
171+
* Children.
157172
* @returns {Array<HastElementContent>}
173+
* Flattened children.
158174
*/
159175
function flattenAll(children) {
160176
/** @type {Array<HastElementContent>} */
@@ -182,7 +198,12 @@ export function toNlcst(tree, file, Parser) {
182198
}
183199

184200
/**
185-
* @param {HastElementContent|Array<HastElementContent>} node
201+
* Add one or more nodes.
202+
*
203+
* @param {HastElementContent | Array<HastElementContent>} node
204+
* hast node.
205+
* @returns {void}
206+
* Nothing.
186207
*/
187208
function add(node) {
188209
/** @type {Array<NlcstSentenceContent> | undefined} */
@@ -220,12 +241,19 @@ export function toNlcst(tree, file, Parser) {
220241
}
221242

222243
/**
244+
* Handle implicit paragraphs.
245+
*
246+
* See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>.
247+
*
223248
* @param {Array<HastElementContent>} children
249+
* hast nodes.
250+
* @returns {void}
251+
* Nothing.
224252
*/
225253
function implicit(children) {
226254
let index = -1
227255
let start = -1
228-
/** @type {boolean|undefined} */
256+
/** @type {boolean | undefined} */
229257
let viable
230258

231259
while (++index <= children.length) {
@@ -254,15 +282,17 @@ export function toNlcst(tree, file, Parser) {
254282
}
255283

256284
/**
257-
* Convert `node` (hast) to nlcst.
285+
* Turn a hast node into nlcst nodes.
258286
*
259287
* @param {HastContent} node
260-
* @returns {Array<NlcstSentenceContent>|undefined}
288+
* hast node.
289+
* @returns {Array<NlcstSentenceContent> | undefined}
290+
* nlcst sentence content.
261291
*/
262292
function one(node) {
263-
/** @type {Array<NlcstSentenceContent>|undefined} */
293+
/** @type {Array<NlcstSentenceContent> | undefined} */
264294
let replacement
265-
/** @type {boolean|undefined} */
295+
/** @type {boolean | undefined} */
266296
let change
267297

268298
if (node.type === 'text') {
@@ -283,41 +313,52 @@ export function toNlcst(tree, file, Parser) {
283313
}
284314
}
285315

286-
return change && replacement
287-
? patch(replacement, loc, loc.toOffset(pointStart(node)))
288-
: replacement
316+
if (change && replacement) {
317+
patch(replacement, loc, loc.toOffset(pointStart(node)))
318+
}
319+
320+
return replacement
289321
}
290322

291323
/**
292-
* Convert all `children` (hast) to nlcst.
324+
* Turn hast nodes into nlcst nodes.
293325
*
294-
* @param {Array<HastContent>} children
295-
* @returns {Array<NlcstSentenceContent>}
326+
* @param {Array<HastContent>} nodes
327+
* hast nodes.
328+
* @returns {Array<NlcstSentenceContent> | undefined}
329+
* nlcst sentence content.
296330
*/
297-
function all(children) {
331+
function all(nodes) {
298332
/** @type {Array<NlcstSentenceContent>} */
299333
const results = []
300334
let index = -1
301335

302-
while (++index < children.length) {
303-
results.push(...(one(children[index]) || []))
336+
while (++index < nodes.length) {
337+
const result = one(nodes[index])
338+
if (result) {
339+
results.push(...result)
340+
}
304341
}
305342

306343
return results
307344
}
308345

309346
/**
310347
* Patch a position on each node in `nodes`.
348+
*
311349
* `offset` is the offset in `file` this run of content starts at.
312350
*
313351
* Note that nlcst nodes are concrete, meaning that their starting and ending
314352
* positions can be inferred from their content.
315353
*
316-
* @template {Array<NlcstContent>} T
317-
* @param {T} nodes
354+
* @param {Array<NlcstContent>} nodes
355+
* Nodes to patch.
318356
* @param {ReturnType<location>} location
357+
* Location info.
319358
* @param {number} offset
320-
* @returns {T}
359+
* Current offset.
360+
* @returns {void}
361+
* Nothing.
321362
*/
322363
function patch(nodes, location, offset) {
323364
let index = -1
@@ -339,22 +380,28 @@ export function toNlcst(tree, file, Parser) {
339380

340381
start = end
341382
}
342-
343-
return nodes
344383
}
345384
}
346385

347386
/**
387+
* Check if an element has a `data-nlcst` attribute set to `source`.
388+
*
348389
* @param {HastElement} node
390+
* Element.
349391
* @returns {boolean}
392+
* Whether `node` has a `data-nlcst` attribute set to `source`.
350393
*/
351394
function dataNlcstSourced(node) {
352395
return Boolean(node.properties && node.properties.dataNlcst === 'source')
353396
}
354397

355398
/**
399+
* Check if an element has a `data-nlcst` attribute set to `ignore`.
400+
*
356401
* @param {HastElement} node
402+
* Element.
357403
* @returns {boolean}
404+
* Whether `node` has a `data-nlcst` attribute set to `ignore`.
358405
*/
359406
function dataNlcstIgnore(node) {
360407
return Boolean(node.properties && node.properties.dataNlcst === 'ignore')

0 commit comments

Comments
 (0)