Skip to content

Commit 380b71b

Browse files
committed
Add patch on state
This commit adds a helper utility to copy positional info from one node into another.
1 parent 35db2db commit 380b71b

31 files changed

+60
-195
lines changed

lib/handlers/a.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ export function a(state, node) {
2626
// @ts-expect-error: assume valid children.
2727
children: all(state, node)
2828
}
29-
30-
// To do: clean.
31-
if (node.position) {
32-
result.position = node.position
33-
}
34-
29+
state.patch(node, result)
3530
return result
3631
}

lib/handlers/blockquote.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ import {wrapChildren} from '../util/wrap-children.js'
1616
*/
1717
export function blockquote(state, node) {
1818
/** @type {Blockquote} */
19-
const result = {
20-
type: 'blockquote',
21-
children: wrapChildren(state, node)
22-
}
23-
24-
// To do: clean.
25-
if (node.position) {
26-
result.position = node.position
27-
}
28-
19+
const result = {type: 'blockquote', children: wrapChildren(state, node)}
20+
state.patch(node, result)
2921
return result
3022
}

lib/handlers/br.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
export function br(state, node) {
1717
/** @type {Break | Text} */
1818
const result = state.wrapText ? {type: 'break'} : {type: 'text', value: ' '}
19-
20-
// To do: clean.
21-
if (node.position) {
22-
result.position = node.position
23-
}
24-
19+
state.patch(node, result)
2520
return result
2621
}

lib/handlers/code.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ export function code(state, node) {
6161
meta: null,
6262
value: trimTrailingLines(wrapText(state, toText(node)))
6363
}
64-
65-
// To do: clean.
66-
if (node.position) {
67-
result.position = node.position
68-
}
69-
64+
state.patch(node, result)
7065
return result
7166
}

lib/handlers/comment.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ export function comment(state, node) {
2020
type: 'html',
2121
value: '<!--' + wrapText(state, node.value) + '-->'
2222
}
23-
24-
// To do: clean.
25-
if (node.position) {
26-
result.position = node.position
27-
}
28-
23+
state.patch(node, result)
2924
return result
3025
}

lib/handlers/del.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export function del(state, node) {
2121
// @ts-expect-error: assume valid children.
2222
children: all(state, node)
2323
}
24-
25-
// To do: clean.
26-
if (node.position) {
27-
result.position = node.position
28-
}
29-
24+
state.patch(node, result)
3025
return result
3126
}

lib/handlers/dl.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ export function dl(state, node) {
103103
spread: listItemsSpread(content),
104104
children: content
105105
}
106-
107-
// To do: clean.
108-
if (node.position) {
109-
result.position = node.position
110-
}
111-
106+
state.patch(node, result)
112107
return result
113108
}
114109
}

lib/handlers/em.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export function em(state, node) {
2121
// @ts-expect-error: assume valid children.
2222
children: all(state, node)
2323
}
24-
25-
// To do: clean.
26-
if (node.position) {
27-
result.position = node.position
28-
}
29-
24+
state.patch(node, result)
3025
return result
3126
}

lib/handlers/heading.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ export function heading(state, node) {
3333
// @ts-expect-error: assume valid children.
3434
children
3535
}
36-
37-
// To do: clean.
38-
if (node.position) {
39-
result.position = node.position
40-
}
41-
36+
state.patch(node, result)
4237
return result
4338
}

lib/handlers/hr.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
export function hr(state, node) {
1616
/** @type {ThematicBreak} */
1717
const result = {type: 'thematicBreak'}
18-
19-
// To do: clean.
20-
if (node.position) {
21-
result.position = node.position
22-
}
23-
18+
state.patch(node, result)
2419
return result
2520
}

lib/handlers/iframe.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ export function iframe(state, node) {
3232
url: resolve(state, src),
3333
children: [{type: 'text', value: wrapText(state, title)}]
3434
}
35-
36-
// To do: clean.
37-
if (node.position) {
38-
result.position = node.position
39-
}
40-
35+
state.patch(node, result)
4136
return result
4237
}
4338
}

lib/handlers/img.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ export function img(state, node) {
2424
title: properties.title ? String(properties.title) : null,
2525
alt: properties.alt ? String(properties.alt) : ''
2626
}
27-
28-
// To do: clean.
29-
if (node.position) {
30-
result.position = node.position
31-
}
32-
27+
state.patch(node, result)
3328
return result
3429
}

lib/handlers/inline-code.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ import {wrapText} from '../util/wrap-text.js'
1818
export function inlineCode(state, node) {
1919
/** @type {InlineCode} */
2020
const result = {type: 'inlineCode', value: wrapText(state, toText(node))}
21-
22-
// To do: clean.
23-
if (node.position) {
24-
result.position = node.position
25-
}
26-
21+
state.patch(node, result)
2722
return result
2823
}

lib/handlers/input.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ export function input(state, node) {
4646
: state.options.unchecked || defaultUnchecked
4747
)
4848
}
49-
50-
// To do: clean.
51-
if (node.position) {
52-
result.position = node.position
53-
}
54-
49+
state.patch(node, result)
5550
return result
5651
}
5752

@@ -66,12 +61,7 @@ export function input(state, node) {
6661
title: wrapText(state, String(properties.title || '')) || null,
6762
alt: wrapText(state, String(alt))
6863
}
69-
70-
// To do: clean.
71-
if (node.position) {
72-
result.position = node.position
73-
}
74-
64+
state.patch(node, result)
7565
return result
7666
}
7767

@@ -154,11 +144,6 @@ export function input(state, node) {
154144

155145
/** @type {Text} */
156146
const result = {type: 'text', value: wrapText(state, texts.join(', '))}
157-
158-
// To do: clean.
159-
if (node.position) {
160-
result.position = node.position
161-
}
162-
147+
state.patch(node, result)
163148
return result
164149
}

lib/handlers/li.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ export function li(state, node) {
5353
checked,
5454
children: content
5555
}
56-
57-
// To do: clean.
58-
if (node.position) {
59-
result.position = node.position
60-
}
61-
56+
state.patch(node, result)
6257
return result
6358
}

lib/handlers/list.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ export function list(state, node) {
3636
spread: listItemsSpread(children),
3737
children
3838
}
39-
40-
// To do: clean.
41-
if (node.position) {
42-
result.position = node.position
43-
}
44-
39+
state.patch(node, result)
4540
return result
4641
}

lib/handlers/media.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ export function media(state, node) {
5757
url: resolve(state, poster),
5858
alt: toString(nodes)
5959
}
60-
61-
// To do: clean.
62-
if (node.position) {
63-
image.position = node.position
64-
}
65-
60+
state.patch(node, image)
6661
nodes = [image]
6762
}
6863

@@ -75,12 +70,7 @@ export function media(state, node) {
7570
// @ts-expect-error Assume phrasing content.
7671
children: nodes
7772
}
78-
79-
// To do: clean.
80-
if (node.position) {
81-
result.position = node.position
82-
}
83-
73+
state.patch(node, result)
8474
return result
8575

8676
function findLink() {

lib/handlers/p.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ export function p(state, node) {
2424
// @ts-expect-error Assume phrasing content.
2525
children: nodes
2626
}
27-
28-
// To do: clean.
29-
if (node.position) {
30-
result.position = node.position
31-
}
32-
27+
state.patch(node, result)
3328
return result
3429
}
3530
}

lib/handlers/root.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ export function root(state, node) {
2525

2626
/** @type {MdastRoot} */
2727
const result = {type: 'root', children}
28-
29-
// To do: clean.
30-
if (node.position) {
31-
result.position = node.position
32-
}
33-
28+
state.patch(node, result)
3429
return result
3530
}

lib/handlers/select.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ export function select(state, node) {
2929
if (results.length > 0) {
3030
/** @type {Text} */
3131
const result = {type: 'text', value: wrapText(state, results.join(', '))}
32-
33-
// To do: clean.
34-
if (node.position) {
35-
result.position = node.position
36-
}
37-
32+
state.patch(node, result)
3833
return result
3934
}
4035
}

lib/handlers/strong.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export function strong(state, node) {
2121
// @ts-expect-error: assume valid children.
2222
children: all(state, node)
2323
}
24-
25-
// To do: clean.
26-
if (node.position) {
27-
result.position = node.position
28-
}
29-
24+
state.patch(node, result)
3025
return result
3126
}

lib/handlers/table-cell.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ export function tableCell(state, node) {
2626
// @ts-expect-error: assume valid children.
2727
children
2828
}
29-
30-
// To do: clean.
31-
if (node.position) {
32-
result.position = node.position
33-
}
29+
state.patch(node, result)
3430

3531
if (node.properties) {
3632
const rowSpan = node.properties.rowSpan

lib/handlers/table-row.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ export function tableRow(state, node) {
2121
// @ts-expect-error: assume valid children.
2222
children: all(state, node)
2323
}
24-
25-
// To do: clean.
26-
if (node.position) {
27-
result.position = node.position
28-
}
29-
24+
state.patch(node, result)
3025
return result
3126
}

lib/handlers/table.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ export function table(state, node) {
3636
if (state.inTable) {
3737
/** @type {Text} */
3838
const result = {type: 'text', value: wrapText(state, toText(node))}
39-
40-
// To do: clean.
41-
if (node.position) {
42-
result.position = node.position
43-
}
44-
39+
state.patch(node, result)
4540
return result
4641
}
4742

@@ -132,12 +127,7 @@ export function table(state, node) {
132127

133128
/** @type {Table} */
134129
const result = {type: 'table', align, children: rows}
135-
136-
// To do: clean.
137-
if (node.position) {
138-
result.position = node.position
139-
}
140-
130+
state.patch(node, result)
141131
return result
142132
}
143133

lib/handlers/text.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ import {wrapText} from '../util/wrap-text.js'
1717
export function text(state, node) {
1818
/** @type {MdastText} */
1919
const result = {type: 'text', value: wrapText(state, node.value)}
20-
21-
// To do: clean.
22-
if (node.position) {
23-
result.position = node.position
24-
}
25-
20+
state.patch(node, result)
2621
return result
2722
}

0 commit comments

Comments
 (0)