Skip to content

Commit f5462e9

Browse files
committed
Change types to use mdast types
1 parent f575ece commit f5462e9

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

Diff for: index.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* @typedef Options Configuration.
33
* @property {Test} [ignore] `unist-util-is` test used to assert parents
44
*
5-
* @typedef {import('mdast').Text} Text
6-
* @typedef {import('mdast').Parent} Parent
75
* @typedef {import('mdast').Root} Root
6+
* @typedef {import('mdast').Content} Content
87
* @typedef {import('mdast').PhrasingContent} PhrasingContent
9-
* @typedef {Parent['children'][number]|Root} Node
8+
* @typedef {import('mdast').Text} Text
9+
* @typedef {Content|Root} Node
10+
* @typedef {Extract<Node, import('mdast').Parent>} Parent
1011
*
1112
* @typedef {import('unist-util-visit-parents').Test} Test
1213
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
@@ -95,14 +96,11 @@ export const findAndReplace =
9596
/** @type {import('unist-util-visit-parents').Visitor<Text>} */
9697
function visitor(node, parents) {
9798
let index = -1
98-
/** @type {Parent} */
99-
let parent
10099
/** @type {Parent|undefined} */
101100
let grandparent
102101

103102
while (++index < parents.length) {
104-
// @ts-expect-error mdast vs. unist parent.
105-
parent = parents[index]
103+
const parent = /** @type {Parent} */ (parents[index])
106104

107105
if (
108106
ignored(
@@ -132,6 +130,7 @@ export const findAndReplace =
132130
const find = pairs[pairIndex][0]
133131
const replace = pairs[pairIndex][1]
134132
let start = 0
133+
// @ts-expect-error: TS is wrong, some of these children can be text.
135134
let index = parent.children.indexOf(node)
136135
/** @type {Array.<PhrasingContent>} */
137136
let nodes = []

Diff for: test.js

+28
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ test('findAndReplace', (t) => {
6060
'should work when given `find` as a `RegExp` and `replace` as a `Function`'
6161
)
6262

63+
t.deepEqual(
64+
findAndReplace(create(), 'emphasis', () => ''),
65+
u('paragraph', [
66+
u('text', 'Some '),
67+
u('emphasis', []),
68+
u('text', ', '),
69+
u('strong', [u('text', 'importance')]),
70+
u('text', ', and '),
71+
u('inlineCode', 'code'),
72+
u('text', '.')
73+
]),
74+
'should work when given `replace` returns an empty string'
75+
)
76+
6377
t.deepEqual(
6478
findAndReplace(create(), 'emphasis', () => {
6579
return u('delete', [u('break')])
@@ -76,6 +90,20 @@ test('findAndReplace', (t) => {
7690
'should work when given `replace` returns a node'
7791
)
7892

93+
t.deepEqual(
94+
findAndReplace(create(), 'emphasis', () => [u('delete', []), u('break')]),
95+
u('paragraph', [
96+
u('text', 'Some '),
97+
u('emphasis', [u('delete', []), u('break')]),
98+
u('text', ', '),
99+
u('strong', [u('text', 'importance')]),
100+
u('text', ', and '),
101+
u('inlineCode', 'code'),
102+
u('text', '.')
103+
]),
104+
'should work when given `replace` returns a list of nodes'
105+
)
106+
79107
t.deepEqual(
80108
findAndReplace(create(), [
81109
['emphasis', '!!!'],

0 commit comments

Comments
 (0)