File tree 2 files changed +25
-6
lines changed
2 files changed +25
-6
lines changed Original file line number Diff line number Diff line change @@ -137,6 +137,7 @@ export const findAndReplace =
137
137
let start = 0
138
138
// @ts -expect-error: TS is wrong, some of these children can be text.
139
139
const index = parent . children . indexOf ( node )
140
+ let change = false
140
141
/** @type {Array<PhrasingContent> } */
141
142
let nodes = [ ]
142
143
/** @type {number|undefined } */
@@ -160,9 +161,7 @@ export const findAndReplace =
160
161
value = value . length > 0 ? { type : 'text' , value} : undefined
161
162
}
162
163
163
- if ( value === false ) {
164
- position = undefined
165
- } else {
164
+ if ( value !== false ) {
166
165
if ( start !== position ) {
167
166
nodes . push ( {
168
167
type : 'text' ,
@@ -177,6 +176,7 @@ export const findAndReplace =
177
176
}
178
177
179
178
start = position + match [ 0 ] . length
179
+ change = true
180
180
}
181
181
182
182
if ( ! find . global ) {
@@ -186,14 +186,14 @@ export const findAndReplace =
186
186
match = find . exec ( node . value )
187
187
}
188
188
189
- if ( position === undefined ) {
190
- nodes = [ node ]
191
- } else {
189
+ if ( change ) {
192
190
if ( start < node . value . length ) {
193
191
nodes . push ( { type : 'text' , value : node . value . slice ( start ) } )
194
192
}
195
193
196
194
parent . children . splice ( index , 1 , ...nodes )
195
+ } else {
196
+ nodes = [ node ]
197
197
}
198
198
199
199
return index + nodes . length
Original file line number Diff line number Diff line change @@ -236,6 +236,25 @@ test('findAndReplace', (t) => {
236
236
'should not be order-sensitive with regexes'
237
237
)
238
238
239
+ t . deepEqual (
240
+ findAndReplace ( u ( 'paragraph' , [ u ( 'text' , 'aaa bbb' ) ] ) , [
241
+ [
242
+ / \b \w + \b / g,
243
+ function ( /** @type {string } */ value ) {
244
+ return value === 'aaa' ? u ( 'strong' , [ u ( 'text' , value ) ] ) : false
245
+ }
246
+ ]
247
+ ] ) ,
248
+ {
249
+ type : 'paragraph' ,
250
+ children : [
251
+ { type : 'strong' , children : [ { type : 'text' , value : 'aaa' } ] } ,
252
+ { type : 'text' , value : ' bbb' }
253
+ ]
254
+ } ,
255
+ 'should support a match, and then a `false`'
256
+ )
257
+
239
258
t . deepEqual (
240
259
findAndReplace ( create ( ) , 'emphasis' , ( ) => false ) ,
241
260
u ( 'paragraph' , [
You can’t perform that action at this time.
0 commit comments