Skip to content

Commit 64004ba

Browse files
committed
Refactor some more code
1 parent ffbb8a8 commit 64004ba

File tree

1 file changed

+56
-93
lines changed

1 file changed

+56
-93
lines changed

lib/state.js

Lines changed: 56 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ export function createState(tree, options) {
134134

135135
/** @type {State} */
136136
const state = {
137-
all: allBound,
137+
all,
138138
applyData,
139139
definitionById,
140140
footnoteById,
141141
footnoteCounts,
142142
footnoteOrder: [],
143143
handlers,
144-
one: oneBound,
144+
one,
145145
options: settings,
146146
patch,
147147
wrap
@@ -173,8 +173,22 @@ export function createState(tree, options) {
173173
* @returns {Array<HastElementContent> | HastElementContent | undefined}
174174
* Resulting hast node.
175175
*/
176-
function oneBound(node, parent) {
177-
return one(state, node, parent)
176+
function one(node, parent) {
177+
const type = node.type
178+
179+
if (own.call(state.handlers, type)) {
180+
return state.handlers[type](state, node, parent)
181+
}
182+
183+
if (state.options.passThrough && state.options.passThrough.includes(type)) {
184+
// To do: next major: deep clone.
185+
// @ts-expect-error: types of passed through nodes are expected to be added manually.
186+
return 'children' in node ? {...node, children: state.all(node)} : node
187+
}
188+
189+
const unknown = state.options.unknownHandler || defaultUnknownHandler
190+
191+
return unknown(state, node, parent)
178192
}
179193

180194
/**
@@ -185,8 +199,42 @@ export function createState(tree, options) {
185199
* @returns {Array<HastElementContent>}
186200
* Resulting hast nodes.
187201
*/
188-
function allBound(parent) {
189-
return all(state, parent)
202+
function all(parent) {
203+
/** @type {Array<HastElementContent>} */
204+
const values = []
205+
206+
if ('children' in parent) {
207+
const nodes = parent.children
208+
let index = -1
209+
while (++index < nodes.length) {
210+
const result = state.one(nodes[index], parent)
211+
212+
// To do: see if we van clean this? Can we merge texts?
213+
if (result) {
214+
if (index && nodes[index - 1].type === 'break') {
215+
if (!Array.isArray(result) && result.type === 'text') {
216+
result.value = result.value.replace(/^\s+/, '')
217+
}
218+
219+
if (!Array.isArray(result) && result.type === 'element') {
220+
const head = result.children[0]
221+
222+
if (head && head.type === 'text') {
223+
head.value = head.value.replace(/^\s+/, '')
224+
}
225+
}
226+
}
227+
228+
if (Array.isArray(result)) {
229+
values.push(...result)
230+
} else {
231+
values.push(result)
232+
}
233+
}
234+
}
235+
}
236+
237+
return values
190238
}
191239
}
192240

@@ -237,12 +285,7 @@ function applyData(from, to) {
237285
// The intent is likely to keep the content around (otherwise: pass
238286
// `hChildren`).
239287
else {
240-
result = {
241-
type: 'element',
242-
tagName: hName,
243-
properties: {},
244-
children: []
245-
}
288+
result = {type: 'element', tagName: hName, properties: {}, children: []}
246289

247290
// To do: next major: take the children from the `root`, or inject the
248291
// raw/text/comment or so into the element?
@@ -274,86 +317,6 @@ function applyData(from, to) {
274317
return result
275318
}
276319

277-
/**
278-
* Transform an mdast node into a hast node.
279-
*
280-
* @param {State} state
281-
* Info passed around.
282-
* @param {MdastNodes} node
283-
* mdast node.
284-
* @param {MdastParents | undefined} [parent]
285-
* Parent of `node`.
286-
* @returns {Array<HastElementContent> | HastElementContent | undefined}
287-
* Resulting hast node.
288-
*/
289-
// To do: next major: do not expose, keep bound.
290-
export function one(state, node, parent) {
291-
const type = node.type
292-
293-
if (own.call(state.handlers, type)) {
294-
return state.handlers[type](state, node, parent)
295-
}
296-
297-
if (state.options.passThrough && state.options.passThrough.includes(type)) {
298-
// To do: next major: deep clone.
299-
// @ts-expect-error: types of passed through nodes are expected to be added manually.
300-
return 'children' in node ? {...node, children: all(state, node)} : node
301-
}
302-
303-
const unknown = state.options.unknownHandler || defaultUnknownHandler
304-
305-
return unknown(state, node, parent)
306-
}
307-
308-
/**
309-
* Transform the children of an mdast node into hast nodes.
310-
*
311-
* @param {State} state
312-
* Info passed around.
313-
* @param {MdastNodes} parent
314-
* mdast node to compile
315-
* @returns {Array<HastElementContent>}
316-
* Resulting hast nodes.
317-
*/
318-
// To do: next major: do not expose, keep bound.
319-
export function all(state, parent) {
320-
/** @type {Array<HastElementContent>} */
321-
const values = []
322-
323-
if ('children' in parent) {
324-
const nodes = parent.children
325-
let index = -1
326-
while (++index < nodes.length) {
327-
const result = one(state, nodes[index], parent)
328-
329-
// To do: see if we van clean this? Can we merge texts?
330-
if (result) {
331-
if (index && nodes[index - 1].type === 'break') {
332-
if (!Array.isArray(result) && result.type === 'text') {
333-
result.value = result.value.replace(/^\s+/, '')
334-
}
335-
336-
if (!Array.isArray(result) && result.type === 'element') {
337-
const head = result.children[0]
338-
339-
if (head && head.type === 'text') {
340-
head.value = head.value.replace(/^\s+/, '')
341-
}
342-
}
343-
}
344-
345-
if (Array.isArray(result)) {
346-
values.push(...result)
347-
} else {
348-
values.push(result)
349-
}
350-
}
351-
}
352-
}
353-
354-
return values
355-
}
356-
357320
/**
358321
* Transform an unknown node.
359322
*
@@ -375,7 +338,7 @@ function defaultUnknownHandler(state, node) {
375338
type: 'element',
376339
tagName: 'div',
377340
properties: {},
378-
children: all(state, node)
341+
children: state.all(node)
379342
}
380343

381344
state.patch(node, result)

0 commit comments

Comments
 (0)