Skip to content

Commit 8a4cc21

Browse files
committed
Add stack of nodes in match objects
1 parent 8c7e552 commit 8a4cc21

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Diff for: index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @typedef {import('./lib/index.js').Find} Find
44
* @typedef {import('./lib/index.js').Replace} Replace
55
* @typedef {import('./lib/index.js').ReplaceFunction} ReplaceFunction
6+
* @typedef {import('./lib/index.js').RegExpMatchObject} RegExpMatchObject
67
* @typedef {import('./lib/index.js').FindAndReplaceTuple} FindAndReplaceTuple
78
* @typedef {import('./lib/index.js').FindAndReplaceSchema} FindAndReplaceSchema
89
* @typedef {import('./lib/index.js').FindAndReplaceList} FindAndReplaceList

Diff for: lib/index.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
* @typedef {import('hast-util-is-element').Test} Test
1313
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
1414
*
15+
* @typedef RegExpMatchObject
16+
* @property {number} index
17+
* @property {string} input
18+
* @property {[Root, ...Array<Element>, Text]} stack
19+
*
1520
* @typedef {string|RegExp} Find
1621
* @typedef {string|ReplaceFunction} Replace
1722
*
@@ -97,16 +102,17 @@ export function findAndReplace(tree, find, replace, options) {
97102
}
98103

99104
if (grandparent) {
100-
return handler(node, grandparent)
105+
return handler(node, parents)
101106
}
102107
}
103108

104109
/**
105110
* @param {Text} node
106-
* @param {Parent} parent
111+
* @param {Array<Root|Element>} parents
107112
* @returns {VisitorResult}
108113
*/
109-
function handler(node, parent) {
114+
function handler(node, parents) {
115+
const parent = parents[parents.length - 1]
110116
const find = pairs[pairIndex][0]
111117
const replace = pairs[pairIndex][1]
112118
let start = 0
@@ -122,7 +128,14 @@ export function findAndReplace(tree, find, replace, options) {
122128

123129
while (match) {
124130
position = match.index
125-
let value = replace(...match, {index: match.index, input: match.input})
131+
/** @type {RegExpMatchObject} */
132+
const matchObject = {
133+
index: match.index,
134+
input: match.input,
135+
// @ts-expect-error: stack is fine.
136+
stack: [...parents, node]
137+
}
138+
let value = replace(...match, matchObject)
126139

127140
if (typeof value === 'string') {
128141
value = value.length > 0 ? {type: 'text', value} : undefined

Diff for: readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ The given `tree` ([`Node`][node]).
163163

164164
This package is fully typed with [TypeScript][].
165165
It exports the additional types `Find`, `Replace`, `ReplaceFunction`,
166-
`FindAndReplaceTuple`, `FindAndReplaceSchema`, `FindAndReplaceList`, and
167-
`Options`.
166+
`FindAndReplaceTuple`, `FindAndReplaceSchema`, `FindAndReplaceList`,
167+
`RegExpMatchObject`, and `Options`.
168168

169169
## Compatibility
170170

0 commit comments

Comments
 (0)