Skip to content

Commit ed45ec4

Browse files
committed
Add wrap helper on state
1 parent e701470 commit ed45ec4

File tree

9 files changed

+48
-53
lines changed

9 files changed

+48
-53
lines changed

lib/footer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
import {normalizeUri} from 'micromark-util-sanitize-uri'
9-
import {wrap} from './wrap.js'
109

1110
/**
1211
* Generate a hast footer for called footnote definitions.
@@ -89,7 +88,7 @@ export function footer(state) {
8988
type: 'element',
9089
tagName: 'li',
9190
properties: {id: state.clobberPrefix + 'fn-' + safeId},
92-
children: wrap(content, true)
91+
children: state.wrap(content, true)
9392
}
9493

9594
state.patch(def, listItem)
@@ -121,7 +120,7 @@ export function footer(state) {
121120
type: 'element',
122121
tagName: 'ol',
123122
properties: {},
124-
children: wrap(listItems, true)
123+
children: state.wrap(listItems, true)
125124
},
126125
{type: 'text', value: '\n'}
127126
]

lib/handlers/blockquote.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @typedef {import('../state.js').State} State
55
*/
66

7-
import {wrap} from '../wrap.js'
8-
97
/**
108
* Turn an mdast `blockquote` node into hast.
119
*
@@ -22,7 +20,7 @@ export function blockquote(state, node) {
2220
type: 'element',
2321
tagName: 'blockquote',
2422
properties: {},
25-
children: wrap(state.all(node), true)
23+
children: state.wrap(state.all(node), true)
2624
}
2725
state.patch(node, result)
2826
return state.applyData(node, result)

lib/handlers/list.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @typedef {import('../state.js').State} State
66
*/
77

8-
import {wrap} from '../wrap.js'
9-
108
/**
119
* Turn an mdast `list` node into hast.
1210
*
@@ -48,7 +46,7 @@ export function list(state, node) {
4846
type: 'element',
4947
tagName: node.ordered ? 'ol' : 'ul',
5048
properties,
51-
children: wrap(results, true)
49+
children: state.wrap(results, true)
5250
}
5351
state.patch(node, result)
5452
return state.applyData(node, result)

lib/handlers/root.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @typedef {import('../state.js').State} State
66
*/
77

8-
import {wrap} from '../wrap.js'
9-
108
/**
119
* Turn an mdast `root` node into hast.
1210
*
@@ -19,7 +17,7 @@ import {wrap} from '../wrap.js'
1917
*/
2018
export function root(state, node) {
2119
/** @type {HastRoot} */
22-
const result = {type: 'root', children: wrap(state.all(node))}
20+
const result = {type: 'root', children: state.wrap(state.all(node))}
2321
state.patch(node, result)
2422
return state.applyData(node, result)
2523
}

lib/handlers/table-row.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* @typedef {Extract<Nodes, Parent>} Parents
1515
*/
1616

17-
import {wrap} from '../wrap.js'
18-
1917
/**
2018
* Turn an mdast `tableRow` node into hast.
2119
*
@@ -67,7 +65,7 @@ export function tableRow(state, node, parent) {
6765
type: 'element',
6866
tagName: 'tr',
6967
properties: {},
70-
children: wrap(cells, true)
68+
children: state.wrap(cells, true)
7169
}
7270
state.patch(node, result)
7371
return state.applyData(node, result)

lib/handlers/table.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import {pointStart, pointEnd} from 'unist-util-position'
8-
import {wrap} from '../wrap.js'
98

109
/**
1110
* Turn an mdast `table` node into hast.
@@ -29,7 +28,7 @@ export function table(state, node) {
2928
type: 'element',
3029
tagName: 'thead',
3130
properties: {},
32-
children: wrap([firstRow], true)
31+
children: state.wrap([firstRow], true)
3332
}
3433
state.patch(node.children[0], head)
3534
tableContent.push(head)
@@ -41,7 +40,7 @@ export function table(state, node) {
4140
type: 'element',
4241
tagName: 'tbody',
4342
properties: {},
44-
children: wrap(rows, true)
43+
children: state.wrap(rows, true)
4544
}
4645

4746
const start = pointStart(node.children[1])
@@ -55,7 +54,7 @@ export function table(state, node) {
5554
type: 'element',
5655
tagName: 'table',
5756
properties: {},
58-
children: wrap(tableContent, true)
57+
children: state.wrap(tableContent, true)
5958
}
6059
state.patch(node, result)
6160
return state.applyData(node, result)

lib/state.js

+36
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
* Transform an mdast node to hast.
119119
* @property {(node: MdastNodes) => Array<HastElementContent>} all
120120
* Transform the children of an mdast parent to hast.
121+
* @property {<Type extends HastContent>(nodes: Array<Type>, loose?: boolean | null | undefined) => Array<Type | HastText>} wrap
122+
* Wrap `nodes` with line endings between each node, adds initial/final line endings when `loose`.
121123
* @property {(left: MdastNodeWithData | PositionLike | null | undefined, right: HastElementContent) => HastElementContent} augment
122124
* Like `state` but lower-level and usable on non-elements.
123125
* Deprecated: use `patch` and `applyData`.
@@ -223,6 +225,7 @@ export function createState(tree, options) {
223225
state.applyData = applyData
224226
state.one = oneBound
225227
state.all = allBound
228+
state.wrap = wrap
226229
// To do: next major: remove `augment`.
227230
state.augment = augment
228231

@@ -539,3 +542,36 @@ function defaultUnknownHandler(state, node) {
539542
state.patch(node, result)
540543
return state.applyData(node, result)
541544
}
545+
546+
/**
547+
* Wrap `nodes` with line endings between each node.
548+
*
549+
* @template {HastContent} Type
550+
* Node type.
551+
* @param {Array<Type>} nodes
552+
* List of nodes to wrap.
553+
* @param {boolean | null | undefined} [loose=false]
554+
* Whether to add line endings at start and end.
555+
* @returns {Array<Type | HastText>}
556+
* Wrapped nodes.
557+
*/
558+
export function wrap(nodes, loose) {
559+
/** @type {Array<Type | HastText>} */
560+
const result = []
561+
let index = -1
562+
563+
if (loose) {
564+
result.push({type: 'text', value: '\n'})
565+
}
566+
567+
while (++index < nodes.length) {
568+
if (index) result.push({type: 'text', value: '\n'})
569+
result.push(nodes[index])
570+
}
571+
572+
if (loose && nodes.length > 0) {
573+
result.push({type: 'text', value: '\n'})
574+
}
575+
576+
return result
577+
}

lib/wrap.js

-34
This file was deleted.

readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ Info passed around about the current state (TypeScript type).
294294
— transform an mdast node to hast
295295
* `all` (`(node: MdastNode) => Array<HastNode>`)
296296
— transform the children of an mdast parent to hast
297+
* `wrap` (`<Type extends HastNode>(nodes: Array<Type>, loose?: boolean) => Array<Type | HastText>`)
298+
— wrap `nodes` with line endings between each node, adds initial/final line
299+
endings when `loose`
297300
* `handlers` ([`Handlers`][api-handlers])
298301
— applied node handlers
299302
* `footnoteById` (`Record<string, MdastFootnoteDefinition>`)

0 commit comments

Comments
 (0)