Skip to content

Commit f3de1d3

Browse files
committed
Add toFlow, toSpecificContent helpers on state
1 parent cbee331 commit f3de1d3

21 files changed

+238
-254
lines changed

lib/handlers/a.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export function a(state, node) {
2020
type: 'link',
2121
url: state.resolve(String(properties.href || '') || null),
2222
title: properties.title ? String(properties.title) : null,
23-
// @ts-expect-error: assume valid children.
23+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
24+
// We also support straddling later.
2425
children: state.all(node)
2526
}
2627
state.patch(node, result)

lib/handlers/blockquote.js

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

7-
import {wrapChildren} from '../util/wrap-children.js'
8-
97
/**
108
* @param {State} state
119
* State.
@@ -16,7 +14,7 @@ import {wrapChildren} from '../util/wrap-children.js'
1614
*/
1715
export function blockquote(state, node) {
1816
/** @type {Blockquote} */
19-
const result = {type: 'blockquote', children: wrapChildren(state, node)}
17+
const result = {type: 'blockquote', children: state.toFlow(state.all(node))}
2018
state.patch(node, result)
2119
return result
2220
}

lib/handlers/del.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export function del(state, node) {
1616
/** @type {Delete} */
1717
const result = {
1818
type: 'delete',
19-
// @ts-expect-error: assume valid children.
19+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
20+
// We also support straddling later.
2021
children: state.all(node)
2122
}
2223
state.patch(node, result)

lib/handlers/dl.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import {listItemsSpread} from '../util/list-items-spread.js'
19-
import {wrapListItems} from '../util/wrap-list-items.js'
2019

2120
/**
2221
* @param {State} state
@@ -117,23 +116,31 @@ export function dl(state, node) {
117116
* mdast nodes.
118117
*/
119118
function handle(state, children) {
120-
const nodes = wrapListItems(state, {type: 'element', tagName: 'x', children})
119+
const nodes = state.all({type: 'element', tagName: 'x', children})
120+
const listItems = state.toSpecificContent(nodes, create)
121121

122-
if (nodes.length === 0) {
122+
if (listItems.length === 0) {
123123
return []
124124
}
125125

126-
if (nodes.length === 1) {
127-
return nodes[0].children
126+
if (listItems.length === 1) {
127+
return listItems[0].children
128128
}
129129

130130
return [
131131
{
132132
type: 'list',
133133
ordered: false,
134134
start: null,
135-
spread: listItemsSpread(nodes),
136-
children: nodes
135+
spread: listItemsSpread(listItems),
136+
children: listItems
137137
}
138138
]
139139
}
140+
141+
/**
142+
* @returns {ListContent}
143+
*/
144+
function create() {
145+
return {type: 'listItem', spread: false, checked: null, children: []}
146+
}

lib/handlers/em.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
*/
1515
export function em(state, node) {
1616
/** @type {Emphasis} */
17-
const result = {
18-
type: 'emphasis',
19-
// @ts-expect-error: assume valid children.
20-
children: state.all(node)
21-
}
17+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
18+
const result = {type: 'emphasis', children: state.all(node)}
2219
state.patch(node, result)
2320
return result
2421
}

lib/handlers/heading.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function heading(state, node) {
2121
type: 'heading',
2222
// @ts-expect-error: fine.
2323
depth,
24-
// @ts-expect-error: assume valid children.
24+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
2525
children: state.all(node)
2626
}
2727
state.patch(node, result)

lib/handlers/index.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* @typedef {Extract<Node, import('unist').Parent>} Parent
1010
*/
1111

12-
import {wrapChildren} from '../util/wrap-children.js'
1312
import {a} from './a.js'
1413
import {base} from './base.js'
1514
import {blockquote} from './blockquote.js'
@@ -124,26 +123,26 @@ export const handlers = {
124123
thead: all,
125124
time: all,
126125

127-
address: wrapChildren,
128-
article: wrapChildren,
129-
aside: wrapChildren,
130-
body: wrapChildren,
131-
center: wrapChildren,
132-
div: wrapChildren,
133-
fieldset: wrapChildren,
134-
figcaption: wrapChildren,
135-
figure: wrapChildren,
136-
form: wrapChildren,
137-
footer: wrapChildren,
138-
header: wrapChildren,
139-
hgroup: wrapChildren,
140-
html: wrapChildren,
141-
legend: wrapChildren,
142-
main: wrapChildren,
143-
multicol: wrapChildren,
144-
nav: wrapChildren,
145-
picture: wrapChildren,
146-
section: wrapChildren,
126+
address: flow,
127+
article: flow,
128+
aside: flow,
129+
body: flow,
130+
center: flow,
131+
div: flow,
132+
fieldset: flow,
133+
figcaption: flow,
134+
figure: flow,
135+
form: flow,
136+
footer: flow,
137+
header: flow,
138+
hgroup: flow,
139+
html: flow,
140+
legend: flow,
141+
main: flow,
142+
multicol: flow,
143+
nav: flow,
144+
picture: flow,
145+
section: flow,
147146

148147
a,
149148
audio: media,
@@ -201,6 +200,16 @@ export const handlers = {
201200

202201
function ignore() {}
203202

203+
/**
204+
* @param {State} state
205+
* State.
206+
* @param {Parent} node
207+
* Parent to transform.
208+
*/
209+
function flow(state, node) {
210+
return state.toFlow(state.all(node))
211+
}
212+
204213
/**
205214
* @param {State} state
206215
* State.

lib/handlers/li.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @typedef {import('../state.js').State} State
55
*/
66

7-
import {wrapChildren} from '../util/wrap-children.js'
8-
97
/**
108
* @param {State} state
119
* State.
@@ -44,14 +42,14 @@ export function li(state, node) {
4442
}
4543
}
4644

47-
const content = wrapChildren(state, clone || node)
45+
const children = state.toFlow(state.all(clone || node))
4846

4947
/** @type {ListItem} */
5048
const result = {
5149
type: 'listItem',
52-
spread: content.length > 1,
50+
spread: children.length > 1,
5351
checked,
54-
children: content
52+
children
5553
}
5654
state.patch(node, result)
5755
return result

lib/handlers/list.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').List} List
4+
* @typedef {import('mdast').ListContent} ListContent
45
* @typedef {import('../state.js').State} State
56
*/
67

78
import {listItemsSpread} from '../util/list-items-spread.js'
8-
import {wrapListItems} from '../util/wrap-list-items.js'
99

1010
/**
1111
* @param {State} state
@@ -17,7 +17,7 @@ import {wrapListItems} from '../util/wrap-list-items.js'
1717
*/
1818
export function list(state, node) {
1919
const ordered = node.tagName === 'ol'
20-
const children = wrapListItems(state, node)
20+
const children = state.toSpecificContent(state.all(node), create)
2121
/** @type {number | null} */
2222
let start = null
2323

@@ -39,3 +39,10 @@ export function list(state, node) {
3939
state.patch(node, result)
4040
return result
4141
}
42+
43+
/**
44+
* @returns {ListContent}
45+
*/
46+
function create() {
47+
return {type: 'listItem', spread: false, checked: null, children: []}
48+
}

lib/handlers/media.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export function media(state, node) {
7474
type: 'link',
7575
title: properties.title ? String(properties.title) : null,
7676
url: state.resolve(src),
77-
// @ts-expect-error Assume phrasing content.
77+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
78+
// We also support straddling later.
7879
children: nodes
7980
}
8081
state.patch(node, result)

lib/handlers/p.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
* mdast node.
1414
*/
1515
export function p(state, node) {
16-
const nodes = state.all(node)
16+
const children = state.all(node)
1717

18-
if (nodes.length > 0) {
18+
if (children.length > 0) {
1919
/** @type {Paragraph} */
20-
const result = {
21-
type: 'paragraph',
22-
// @ts-expect-error Assume phrasing content.
23-
children: nodes
24-
}
20+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
21+
const result = {type: 'paragraph', children}
2522
state.patch(node, result)
2623
return result
2724
}

lib/handlers/root.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export function root(state, node) {
1818
let children = state.all(node)
1919

2020
if (state.options.document || wrapNeeded(children)) {
21-
// @ts-expect-error: improve `all`?
2221
children = wrap(children)
2322
}
2423

lib/handlers/strong.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
*/
1515
export function strong(state, node) {
1616
/** @type {Strong} */
17-
const result = {
18-
type: 'strong',
19-
// @ts-expect-error: assume valid children.
20-
children: state.all(node)
21-
}
17+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
18+
const result = {type: 'strong', children: state.all(node)}
2219
state.patch(node, result)
2320
return result
2421
}

lib/handlers/table-cell.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
* mdast node.
1414
*/
1515
export function tableCell(state, node) {
16-
const children = state.all(node)
17-
1816
/** @type {TableCell} */
19-
const result = {
20-
type: 'tableCell',
21-
// @ts-expect-error: assume valid children.
22-
children
23-
}
17+
// @ts-expect-error: allow potentially “invalid” nodes, they might be unknown.
18+
const result = {type: 'tableCell', children: state.all(node)}
2419
state.patch(node, result)
2520

2621
if (node.properties) {

lib/handlers/table-row.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('hast').Element} Element
33
* @typedef {import('mdast').TableRow} TableRow
4+
* @typedef {import('mdast').RowContent} RowContent
45
* @typedef {import('../state.js').State} State
56
*/
67

@@ -13,12 +14,17 @@
1314
* mdast node.
1415
*/
1516
export function tableRow(state, node) {
17+
const children = state.toSpecificContent(state.all(node), create)
18+
1619
/** @type {TableRow} */
17-
const result = {
18-
type: 'tableRow',
19-
// @ts-expect-error: assume valid children.
20-
children: state.all(node)
21-
}
20+
const result = {type: 'tableRow', children}
2221
state.patch(node, result)
2322
return result
2423
}
24+
25+
/**
26+
* @returns {RowContent}
27+
*/
28+
function create() {
29+
return {type: 'tableCell', children: []}
30+
}

0 commit comments

Comments
 (0)