Refactor routing in App component to enhance navigation and improve error handling by integrating dynamic routes and updating the NotFound route.
This commit is contained in:
9
node_modules/mdast-util-find-and-replace/index.d.ts
generated
vendored
Normal file
9
node_modules/mdast-util-find-and-replace/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
export { findAndReplace } from "./lib/index.js";
|
||||
export type FindAndReplaceList = import("./lib/index.js").FindAndReplaceList;
|
||||
export type FindAndReplaceTuple = import("./lib/index.js").FindAndReplaceTuple;
|
||||
export type Find = import("./lib/index.js").Find;
|
||||
export type Options = import("./lib/index.js").Options;
|
||||
export type RegExpMatchObject = import("./lib/index.js").RegExpMatchObject;
|
||||
export type ReplaceFunction = import("./lib/index.js").ReplaceFunction;
|
||||
export type Replace = import("./lib/index.js").Replace;
|
||||
//# sourceMappingURL=index.d.ts.map
|
1
node_modules/mdast-util-find-and-replace/index.d.ts.map
generated
vendored
Normal file
1
node_modules/mdast-util-find-and-replace/index.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":";iCACa,OAAO,gBAAgB,EAAE,kBAAkB;kCAC3C,OAAO,gBAAgB,EAAE,mBAAmB;mBAC5C,OAAO,gBAAgB,EAAE,IAAI;sBAC7B,OAAO,gBAAgB,EAAE,OAAO;gCAChC,OAAO,gBAAgB,EAAE,iBAAiB;8BAC1C,OAAO,gBAAgB,EAAE,eAAe;sBACxC,OAAO,gBAAgB,EAAE,OAAO"}
|
11
node_modules/mdast-util-find-and-replace/index.js
generated
vendored
Normal file
11
node_modules/mdast-util-find-and-replace/index.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @typedef {import('./lib/index.js').FindAndReplaceList} FindAndReplaceList
|
||||
* @typedef {import('./lib/index.js').FindAndReplaceTuple} FindAndReplaceTuple
|
||||
* @typedef {import('./lib/index.js').Find} Find
|
||||
* @typedef {import('./lib/index.js').Options} Options
|
||||
* @typedef {import('./lib/index.js').RegExpMatchObject} RegExpMatchObject
|
||||
* @typedef {import('./lib/index.js').ReplaceFunction} ReplaceFunction
|
||||
* @typedef {import('./lib/index.js').Replace} Replace
|
||||
*/
|
||||
|
||||
export {findAndReplace} from './lib/index.js'
|
79
node_modules/mdast-util-find-and-replace/lib/index.d.ts
generated
vendored
Normal file
79
node_modules/mdast-util-find-and-replace/lib/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Find patterns in a tree and replace them.
|
||||
*
|
||||
* The algorithm searches the tree in *preorder* for complete values in `Text`
|
||||
* nodes.
|
||||
* Partial matches are not supported.
|
||||
*
|
||||
* @param {Nodes} tree
|
||||
* Tree to change.
|
||||
* @param {FindAndReplaceList | FindAndReplaceTuple} list
|
||||
* Patterns to find.
|
||||
* @param {Options | null | undefined} [options]
|
||||
* Configuration (when `find` is not `Find`).
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
export function findAndReplace(tree: Nodes, list: FindAndReplaceList | FindAndReplaceTuple, options?: Options | null | undefined): undefined;
|
||||
/**
|
||||
* Info on the match.
|
||||
*/
|
||||
export type RegExpMatchObject = {
|
||||
/**
|
||||
* The index of the search at which the result was found.
|
||||
*/
|
||||
index: number;
|
||||
/**
|
||||
* A copy of the search string in the text node.
|
||||
*/
|
||||
input: string;
|
||||
/**
|
||||
* All ancestors of the text node, where the last node is the text itself.
|
||||
*/
|
||||
stack: [...Array<Parents>, Text];
|
||||
};
|
||||
/**
|
||||
* Pattern to find.
|
||||
*
|
||||
* Strings are escaped and then turned into global expressions.
|
||||
*/
|
||||
export type Find = RegExp | string;
|
||||
/**
|
||||
* Several find and replaces, in array form.
|
||||
*/
|
||||
export type FindAndReplaceList = Array<FindAndReplaceTuple>;
|
||||
/**
|
||||
* Find and replace in tuple form.
|
||||
*/
|
||||
export type FindAndReplaceTuple = [Find, Replace?];
|
||||
/**
|
||||
* Thing to replace with.
|
||||
*/
|
||||
export type Replace = ReplaceFunction | string | null | undefined;
|
||||
/**
|
||||
* Callback called when a search matches.
|
||||
*/
|
||||
export type ReplaceFunction = (...parameters: any[]) => Array<PhrasingContent> | PhrasingContent | string | false | null | undefined;
|
||||
/**
|
||||
* Normalized find and replace.
|
||||
*/
|
||||
export type Pair = [RegExp, ReplaceFunction];
|
||||
/**
|
||||
* All find and replaced.
|
||||
*/
|
||||
export type Pairs = Array<Pair>;
|
||||
/**
|
||||
* Configuration.
|
||||
*/
|
||||
export type Options = {
|
||||
/**
|
||||
* Test for which nodes to ignore (optional).
|
||||
*/
|
||||
ignore?: Test | null | undefined;
|
||||
};
|
||||
import type { Nodes } from 'mdast';
|
||||
import type { Parents } from 'mdast';
|
||||
import type { Text } from 'mdast';
|
||||
import type { PhrasingContent } from 'mdast';
|
||||
import type { Test } from 'unist-util-visit-parents';
|
||||
//# sourceMappingURL=index.d.ts.map
|
1
node_modules/mdast-util-find-and-replace/lib/index.d.ts.map
generated
vendored
Normal file
1
node_modules/mdast-util-find-and-replace/lib/index.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AA6DA;;;;;;;;;;;;;;;GAeG;AACH,qCATW,KAAK,QAEL,kBAAkB,GAAG,mBAAmB,YAExC,OAAO,GAAG,IAAI,GAAG,SAAS,GAExB,SAAS,CA6HrB;;;;;;;;WA/La,MAAM;;;;WAEN,MAAM;;;;WAEN,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC;;;;;;;mBAG1B,MAAM,GAAG,MAAM;;;;iCAKf,KAAK,CAAC,mBAAmB,CAAC;;;;kCAG1B,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;;;;sBAGhB,eAAe,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;8CAK1C,GAAG,EAAA,KAMJ,KAAK,CAAC,eAAe,CAAC,GAAG,eAAe,GAAG,MAAM,GAAG,KAAK,GAAG,IAAI,GAAG,SAAS;;;;mBAQ5E,CAAC,MAAM,EAAE,eAAe,CAAC;;;;oBAGzB,KAAK,CAAC,IAAI,CAAC;;;;;;;;aAKV,IAAI,GAAG,IAAI,GAAG,SAAS;;2BApDyB,OAAO;6BAAP,OAAO;0BAAP,OAAO;qCAAP,OAAO;0BACjB,0BAA0B"}
|
261
node_modules/mdast-util-find-and-replace/lib/index.js
generated
vendored
Normal file
261
node_modules/mdast-util-find-and-replace/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,261 @@
|
||||
/**
|
||||
* @import {Nodes, Parents, PhrasingContent, Root, Text} from 'mdast'
|
||||
* @import {BuildVisitor, Test, VisitorResult} from 'unist-util-visit-parents'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef RegExpMatchObject
|
||||
* Info on the match.
|
||||
* @property {number} index
|
||||
* The index of the search at which the result was found.
|
||||
* @property {string} input
|
||||
* A copy of the search string in the text node.
|
||||
* @property {[...Array<Parents>, Text]} stack
|
||||
* All ancestors of the text node, where the last node is the text itself.
|
||||
*
|
||||
* @typedef {RegExp | string} Find
|
||||
* Pattern to find.
|
||||
*
|
||||
* Strings are escaped and then turned into global expressions.
|
||||
*
|
||||
* @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList
|
||||
* Several find and replaces, in array form.
|
||||
*
|
||||
* @typedef {[Find, Replace?]} FindAndReplaceTuple
|
||||
* Find and replace in tuple form.
|
||||
*
|
||||
* @typedef {ReplaceFunction | string | null | undefined} Replace
|
||||
* Thing to replace with.
|
||||
*
|
||||
* @callback ReplaceFunction
|
||||
* Callback called when a search matches.
|
||||
* @param {...any} parameters
|
||||
* The parameters are the result of corresponding search expression:
|
||||
*
|
||||
* * `value` (`string`) — whole match
|
||||
* * `...capture` (`Array<string>`) — matches from regex capture groups
|
||||
* * `match` (`RegExpMatchObject`) — info on the match
|
||||
* @returns {Array<PhrasingContent> | PhrasingContent | string | false | null | undefined}
|
||||
* Thing to replace with.
|
||||
*
|
||||
* * when `null`, `undefined`, `''`, remove the match
|
||||
* * …or when `false`, do not replace at all
|
||||
* * …or when `string`, replace with a text node of that value
|
||||
* * …or when `Node` or `Array<Node>`, replace with those nodes
|
||||
*
|
||||
* @typedef {[RegExp, ReplaceFunction]} Pair
|
||||
* Normalized find and replace.
|
||||
*
|
||||
* @typedef {Array<Pair>} Pairs
|
||||
* All find and replaced.
|
||||
*
|
||||
* @typedef Options
|
||||
* Configuration.
|
||||
* @property {Test | null | undefined} [ignore]
|
||||
* Test for which nodes to ignore (optional).
|
||||
*/
|
||||
|
||||
import escape from 'escape-string-regexp'
|
||||
import {visitParents} from 'unist-util-visit-parents'
|
||||
import {convert} from 'unist-util-is'
|
||||
|
||||
/**
|
||||
* Find patterns in a tree and replace them.
|
||||
*
|
||||
* The algorithm searches the tree in *preorder* for complete values in `Text`
|
||||
* nodes.
|
||||
* Partial matches are not supported.
|
||||
*
|
||||
* @param {Nodes} tree
|
||||
* Tree to change.
|
||||
* @param {FindAndReplaceList | FindAndReplaceTuple} list
|
||||
* Patterns to find.
|
||||
* @param {Options | null | undefined} [options]
|
||||
* Configuration (when `find` is not `Find`).
|
||||
* @returns {undefined}
|
||||
* Nothing.
|
||||
*/
|
||||
export function findAndReplace(tree, list, options) {
|
||||
const settings = options || {}
|
||||
const ignored = convert(settings.ignore || [])
|
||||
const pairs = toPairs(list)
|
||||
let pairIndex = -1
|
||||
|
||||
while (++pairIndex < pairs.length) {
|
||||
visitParents(tree, 'text', visitor)
|
||||
}
|
||||
|
||||
/** @type {BuildVisitor<Root, 'text'>} */
|
||||
function visitor(node, parents) {
|
||||
let index = -1
|
||||
/** @type {Parents | undefined} */
|
||||
let grandparent
|
||||
|
||||
while (++index < parents.length) {
|
||||
const parent = parents[index]
|
||||
/** @type {Array<Nodes> | undefined} */
|
||||
const siblings = grandparent ? grandparent.children : undefined
|
||||
|
||||
if (
|
||||
ignored(
|
||||
parent,
|
||||
siblings ? siblings.indexOf(parent) : undefined,
|
||||
grandparent
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
grandparent = parent
|
||||
}
|
||||
|
||||
if (grandparent) {
|
||||
return handler(node, parents)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a text node which is not in an ignored parent.
|
||||
*
|
||||
* @param {Text} node
|
||||
* Text node.
|
||||
* @param {Array<Parents>} parents
|
||||
* Parents.
|
||||
* @returns {VisitorResult}
|
||||
* Result.
|
||||
*/
|
||||
function handler(node, parents) {
|
||||
const parent = parents[parents.length - 1]
|
||||
const find = pairs[pairIndex][0]
|
||||
const replace = pairs[pairIndex][1]
|
||||
let start = 0
|
||||
/** @type {Array<Nodes>} */
|
||||
const siblings = parent.children
|
||||
const index = siblings.indexOf(node)
|
||||
let change = false
|
||||
/** @type {Array<PhrasingContent>} */
|
||||
let nodes = []
|
||||
|
||||
find.lastIndex = 0
|
||||
|
||||
let match = find.exec(node.value)
|
||||
|
||||
while (match) {
|
||||
const position = match.index
|
||||
/** @type {RegExpMatchObject} */
|
||||
const matchObject = {
|
||||
index: match.index,
|
||||
input: match.input,
|
||||
stack: [...parents, node]
|
||||
}
|
||||
let value = replace(...match, matchObject)
|
||||
|
||||
if (typeof value === 'string') {
|
||||
value = value.length > 0 ? {type: 'text', value} : undefined
|
||||
}
|
||||
|
||||
// It wasn’t a match after all.
|
||||
if (value === false) {
|
||||
// False acts as if there was no match.
|
||||
// So we need to reset `lastIndex`, which currently being at the end of
|
||||
// the current match, to the beginning.
|
||||
find.lastIndex = position + 1
|
||||
} else {
|
||||
if (start !== position) {
|
||||
nodes.push({
|
||||
type: 'text',
|
||||
value: node.value.slice(start, position)
|
||||
})
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
nodes.push(...value)
|
||||
} else if (value) {
|
||||
nodes.push(value)
|
||||
}
|
||||
|
||||
start = position + match[0].length
|
||||
change = true
|
||||
}
|
||||
|
||||
if (!find.global) {
|
||||
break
|
||||
}
|
||||
|
||||
match = find.exec(node.value)
|
||||
}
|
||||
|
||||
if (change) {
|
||||
if (start < node.value.length) {
|
||||
nodes.push({type: 'text', value: node.value.slice(start)})
|
||||
}
|
||||
|
||||
parent.children.splice(index, 1, ...nodes)
|
||||
} else {
|
||||
nodes = [node]
|
||||
}
|
||||
|
||||
return index + nodes.length
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a tuple or a list of tuples into pairs.
|
||||
*
|
||||
* @param {FindAndReplaceList | FindAndReplaceTuple} tupleOrList
|
||||
* Schema.
|
||||
* @returns {Pairs}
|
||||
* Clean pairs.
|
||||
*/
|
||||
function toPairs(tupleOrList) {
|
||||
/** @type {Pairs} */
|
||||
const result = []
|
||||
|
||||
if (!Array.isArray(tupleOrList)) {
|
||||
throw new TypeError('Expected find and replace tuple or list of tuples')
|
||||
}
|
||||
|
||||
/** @type {FindAndReplaceList} */
|
||||
// @ts-expect-error: correct.
|
||||
const list =
|
||||
!tupleOrList[0] || Array.isArray(tupleOrList[0])
|
||||
? tupleOrList
|
||||
: [tupleOrList]
|
||||
|
||||
let index = -1
|
||||
|
||||
while (++index < list.length) {
|
||||
const tuple = list[index]
|
||||
result.push([toExpression(tuple[0]), toFunction(tuple[1])])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a find into an expression.
|
||||
*
|
||||
* @param {Find} find
|
||||
* Find.
|
||||
* @returns {RegExp}
|
||||
* Expression.
|
||||
*/
|
||||
function toExpression(find) {
|
||||
return typeof find === 'string' ? new RegExp(escape(find), 'g') : find
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a replace into a function.
|
||||
*
|
||||
* @param {Replace} replace
|
||||
* Replace.
|
||||
* @returns {ReplaceFunction}
|
||||
* Function.
|
||||
*/
|
||||
function toFunction(replace) {
|
||||
return typeof replace === 'function'
|
||||
? replace
|
||||
: function () {
|
||||
return replace
|
||||
}
|
||||
}
|
22
node_modules/mdast-util-find-and-replace/license
generated
vendored
Normal file
22
node_modules/mdast-util-find-and-replace/license
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) Titus Wormer <tituswormer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
85
node_modules/mdast-util-find-and-replace/package.json
generated
vendored
Normal file
85
node_modules/mdast-util-find-and-replace/package.json
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
{
|
||||
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
|
||||
"bugs": "https://github.com/syntax-tree/mdast-util-find-and-replace/issues",
|
||||
"contributors": [
|
||||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
|
||||
],
|
||||
"dependencies": {
|
||||
"@types/mdast": "^4.0.0",
|
||||
"escape-string-regexp": "^5.0.0",
|
||||
"unist-util-is": "^6.0.0",
|
||||
"unist-util-visit-parents": "^6.0.0"
|
||||
},
|
||||
"description": "mdast utility to find and replace text in a tree",
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.0.0",
|
||||
"c8": "^10.0.0",
|
||||
"prettier": "^3.0.0",
|
||||
"remark-cli": "^12.0.0",
|
||||
"remark-preset-wooorm": "^10.0.0",
|
||||
"type-coverage": "^2.0.0",
|
||||
"typescript": "^5.0.0",
|
||||
"unist-builder": "^4.0.0",
|
||||
"xo": "^0.60.0"
|
||||
},
|
||||
"exports": "./index.js",
|
||||
"files": [
|
||||
"index.d.ts.map",
|
||||
"index.d.ts",
|
||||
"index.js",
|
||||
"lib/"
|
||||
],
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/unified"
|
||||
},
|
||||
"keywords": [
|
||||
"find",
|
||||
"markdown",
|
||||
"mdast-util",
|
||||
"mdast",
|
||||
"unist",
|
||||
"utility",
|
||||
"util",
|
||||
"replace"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "mdast-util-find-and-replace",
|
||||
"prettier": {
|
||||
"bracketSpacing": false,
|
||||
"semi": false,
|
||||
"singleQuote": true,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "none",
|
||||
"useTabs": false
|
||||
},
|
||||
"remarkConfig": {
|
||||
"plugins": [
|
||||
"remark-preset-wooorm"
|
||||
]
|
||||
},
|
||||
"repository": "syntax-tree/mdast-util-find-and-replace",
|
||||
"scripts": {
|
||||
"build": "tsc --build --clean && tsc --build && type-coverage",
|
||||
"format": "remark --frail --output --quiet -- . && prettier --log-level warn --write -- . && xo --fix",
|
||||
"test-api": "node --conditions development test.js",
|
||||
"test-coverage": "c8 --100 --reporter lcov -- npm run test-api",
|
||||
"test": "npm run build && npm run format && npm run test-coverage"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"typeCoverage": {
|
||||
"atLeast": 100,
|
||||
"ignoreFiles": [
|
||||
"lib/index.d.ts"
|
||||
],
|
||||
"strict": true
|
||||
},
|
||||
"type": "module",
|
||||
"version": "3.0.2",
|
||||
"xo": {
|
||||
"prettier": true,
|
||||
"rules": {
|
||||
"unicorn/prefer-at": "off"
|
||||
}
|
||||
}
|
||||
}
|
368
node_modules/mdast-util-find-and-replace/readme.md
generated
vendored
Normal file
368
node_modules/mdast-util-find-and-replace/readme.md
generated
vendored
Normal file
@@ -0,0 +1,368 @@
|
||||
# mdast-util-find-and-replace
|
||||
|
||||
[![Build][build-badge]][build]
|
||||
[![Coverage][coverage-badge]][coverage]
|
||||
[![Downloads][downloads-badge]][downloads]
|
||||
[![Size][size-badge]][size]
|
||||
[![Sponsors][sponsors-badge]][collective]
|
||||
[![Backers][backers-badge]][collective]
|
||||
[![Chat][chat-badge]][chat]
|
||||
|
||||
[mdast][] utility to find and replace things.
|
||||
|
||||
## Contents
|
||||
|
||||
* [What is this?](#what-is-this)
|
||||
* [When should I use this?](#when-should-i-use-this)
|
||||
* [Install](#install)
|
||||
* [Use](#use)
|
||||
* [API](#api)
|
||||
* [`findAndReplace(tree, list[, options])`](#findandreplacetree-list-options)
|
||||
* [`Find`](#find)
|
||||
* [`FindAndReplaceList`](#findandreplacelist)
|
||||
* [`FindAndReplaceTuple`](#findandreplacetuple)
|
||||
* [`Options`](#options)
|
||||
* [`RegExpMatchObject`](#regexpmatchobject)
|
||||
* [`Replace`](#replace)
|
||||
* [`ReplaceFunction`](#replacefunction)
|
||||
* [Types](#types)
|
||||
* [Compatibility](#compatibility)
|
||||
* [Security](#security)
|
||||
* [Related](#related)
|
||||
* [Contribute](#contribute)
|
||||
* [License](#license)
|
||||
|
||||
## What is this?
|
||||
|
||||
This package is a utility that lets you find patterns (`string`, `RegExp`) in
|
||||
text and replace them with nodes.
|
||||
|
||||
## When should I use this?
|
||||
|
||||
This utility is typically useful when you have regexes and want to modify mdast.
|
||||
One example is when you have some form of “mentions” (such as
|
||||
`/@([a-z][_a-z0-9])\b/gi`) and want to create links to persons from them.
|
||||
|
||||
A similar package, [`hast-util-find-and-replace`][hast-util-find-and-replace]
|
||||
does the same but on [hast][].
|
||||
|
||||
## Install
|
||||
|
||||
This package is [ESM only][esm].
|
||||
In Node.js (version 16+), install with [npm][]:
|
||||
|
||||
```sh
|
||||
npm install mdast-util-find-and-replace
|
||||
```
|
||||
|
||||
In Deno with [`esm.sh`][esmsh]:
|
||||
|
||||
```js
|
||||
import {findAndReplace} from 'https://esm.sh/mdast-util-find-and-replace@3'
|
||||
```
|
||||
|
||||
In browsers with [`esm.sh`][esmsh]:
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import {findAndReplace} from 'https://esm.sh/mdast-util-find-and-replace@3?bundle'
|
||||
</script>
|
||||
```
|
||||
|
||||
## Use
|
||||
|
||||
```js
|
||||
import {findAndReplace} from 'mdast-util-find-and-replace'
|
||||
import {u} from 'unist-builder'
|
||||
import {inspect} from 'unist-util-inspect'
|
||||
|
||||
const tree = u('paragraph', [
|
||||
u('text', 'Some '),
|
||||
u('emphasis', [u('text', 'emphasis')]),
|
||||
u('text', ' and '),
|
||||
u('strong', [u('text', 'importance')]),
|
||||
u('text', '.')
|
||||
])
|
||||
|
||||
findAndReplace(tree, [
|
||||
[/and/gi, 'or'],
|
||||
[/emphasis/gi, 'em'],
|
||||
[/importance/gi, 'strong'],
|
||||
[
|
||||
/Some/g,
|
||||
function ($0) {
|
||||
return u('link', {url: '//example.com#' + $0}, [u('text', $0)])
|
||||
}
|
||||
]
|
||||
])
|
||||
|
||||
console.log(inspect(tree))
|
||||
```
|
||||
|
||||
Yields:
|
||||
|
||||
```txt
|
||||
paragraph[8]
|
||||
├─0 link[1]
|
||||
│ │ url: "//example.com#Some"
|
||||
│ └─0 text "Some"
|
||||
├─1 text " "
|
||||
├─2 emphasis[1]
|
||||
│ └─0 text "em"
|
||||
├─3 text " "
|
||||
├─4 text "or"
|
||||
├─5 text " "
|
||||
├─6 strong[1]
|
||||
│ └─0 text "strong"
|
||||
└─7 text "."
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
This package exports the identifier [`findAndReplace`][api-find-and-replace].
|
||||
There is no default export.
|
||||
|
||||
### `findAndReplace(tree, list[, options])`
|
||||
|
||||
Find patterns in a tree and replace them.
|
||||
|
||||
The algorithm searches the tree in *[preorder][]* for complete values in
|
||||
[`Text`][text] nodes.
|
||||
Partial matches are not supported.
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `tree` ([`Node`][node])
|
||||
— tree to change
|
||||
* `list` ([`FindAndReplaceList`][api-find-and-replace-list] or
|
||||
[`FindAndReplaceTuple`][api-find-and-replace-tuple])
|
||||
— one or more find-and-replace pairs
|
||||
* `options` ([`Options`][api-options])
|
||||
— configuration
|
||||
|
||||
###### Returns
|
||||
|
||||
Nothing (`undefined`).
|
||||
|
||||
### `Find`
|
||||
|
||||
Pattern to find (TypeScript type).
|
||||
|
||||
Strings are escaped and then turned into global expressions.
|
||||
|
||||
###### Type
|
||||
|
||||
```ts
|
||||
type Find = RegExp | string
|
||||
```
|
||||
|
||||
### `FindAndReplaceList`
|
||||
|
||||
Several find and replaces, in array form (TypeScript type).
|
||||
|
||||
###### Type
|
||||
|
||||
```ts
|
||||
type FindAndReplaceList = Array<FindAndReplaceTuple>
|
||||
```
|
||||
|
||||
See [`FindAndReplaceTuple`][api-find-and-replace-tuple].
|
||||
|
||||
### `FindAndReplaceTuple`
|
||||
|
||||
Find and replace in tuple form (TypeScript type).
|
||||
|
||||
###### Type
|
||||
|
||||
```ts
|
||||
type FindAndReplaceTuple = [Find, Replace?]
|
||||
```
|
||||
|
||||
See [`Find`][api-find] and [`Replace`][api-replace].
|
||||
|
||||
### `Options`
|
||||
|
||||
Configuration (TypeScript type).
|
||||
|
||||
###### Fields
|
||||
|
||||
* `ignore` ([`Test`][test], optional)
|
||||
— test for which elements to ignore
|
||||
|
||||
### `RegExpMatchObject`
|
||||
|
||||
Info on the match (TypeScript type).
|
||||
|
||||
###### Fields
|
||||
|
||||
* `index` (`number`)
|
||||
— the index of the search at which the result was found
|
||||
* `input` (`string`)
|
||||
— a copy of the search string in the text node
|
||||
* `stack` ([`Array<Node>`][node])
|
||||
— all ancestors of the text node, where the last node is the text itself
|
||||
|
||||
### `Replace`
|
||||
|
||||
Thing to replace with (TypeScript type).
|
||||
|
||||
###### Type
|
||||
|
||||
```ts
|
||||
type Replace = ReplaceFunction | string
|
||||
```
|
||||
|
||||
See [`ReplaceFunction`][api-replace-function].
|
||||
|
||||
### `ReplaceFunction`
|
||||
|
||||
Callback called when a search matches (TypeScript type).
|
||||
|
||||
###### Parameters
|
||||
|
||||
The parameters are the result of corresponding search expression:
|
||||
|
||||
* `value` (`string`)
|
||||
— whole match
|
||||
* `...capture` (`Array<string>`)
|
||||
— matches from regex capture groups
|
||||
* `match` ([`RegExpMatchObject`][api-regexp-match-object])
|
||||
— info on the match
|
||||
|
||||
###### Returns
|
||||
|
||||
Thing to replace with:
|
||||
|
||||
* when `null`, `undefined`, `''`, remove the match
|
||||
* …or when `false`, do not replace at all
|
||||
* …or when `string`, replace with a text node of that value
|
||||
* …or when `Node` or `Array<Node>`, replace with those nodes
|
||||
|
||||
## Types
|
||||
|
||||
This package is fully typed with [TypeScript][].
|
||||
It exports the additional types [`Find`][api-find],
|
||||
[`FindAndReplaceList`][api-find-and-replace-list],
|
||||
[`FindAndReplaceTuple`][api-find-and-replace-tuple],
|
||||
[`Options`][api-options],
|
||||
[`RegExpMatchObject`][api-regexp-match-object],
|
||||
[`Replace`][api-replace], and
|
||||
[`ReplaceFunction`][api-replace-function].
|
||||
|
||||
## Compatibility
|
||||
|
||||
Projects maintained by the unified collective are compatible with maintained
|
||||
versions of Node.js.
|
||||
|
||||
When we cut a new major release, we drop support for unmaintained versions of
|
||||
Node.
|
||||
This means we try to keep the current release line,
|
||||
`mdast-util-find-and-replace@^3`, compatible with Node.js 16.
|
||||
|
||||
## Security
|
||||
|
||||
Use of `mdast-util-find-and-replace` does not involve [hast][] or user content
|
||||
so there are no openings for [cross-site scripting (XSS)][xss] attacks.
|
||||
|
||||
## Related
|
||||
|
||||
* [`hast-util-find-and-replace`](https://github.com/syntax-tree/hast-util-find-and-replace)
|
||||
— find and replace in hast
|
||||
* [`hast-util-select`](https://github.com/syntax-tree/hast-util-select)
|
||||
— `querySelector`, `querySelectorAll`, and `matches`
|
||||
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
|
||||
— select unist nodes with CSS-like selectors
|
||||
|
||||
## Contribute
|
||||
|
||||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
|
||||
ways to get started.
|
||||
See [`support.md`][support] for ways to get help.
|
||||
|
||||
This project has a [code of conduct][coc].
|
||||
By interacting with this repository, organisation, or community you agree to
|
||||
abide by its terms.
|
||||
|
||||
## License
|
||||
|
||||
[MIT][license] © [Titus Wormer][author]
|
||||
|
||||
<!-- Definition -->
|
||||
|
||||
[build-badge]: https://github.com/syntax-tree/mdast-util-find-and-replace/workflows/main/badge.svg
|
||||
|
||||
[build]: https://github.com/syntax-tree/mdast-util-find-and-replace/actions
|
||||
|
||||
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/mdast-util-find-and-replace.svg
|
||||
|
||||
[coverage]: https://codecov.io/github/syntax-tree/mdast-util-find-and-replace
|
||||
|
||||
[downloads-badge]: https://img.shields.io/npm/dm/mdast-util-find-and-replace.svg
|
||||
|
||||
[downloads]: https://www.npmjs.com/package/mdast-util-find-and-replace
|
||||
|
||||
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=mdast-util-find-and-replace
|
||||
|
||||
[size]: https://bundlejs.com/?q=mdast-util-find-and-replace
|
||||
|
||||
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
|
||||
|
||||
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
|
||||
|
||||
[collective]: https://opencollective.com/unified
|
||||
|
||||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
|
||||
|
||||
[chat]: https://github.com/syntax-tree/unist/discussions
|
||||
|
||||
[npm]: https://docs.npmjs.com/cli/install
|
||||
|
||||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
||||
|
||||
[esmsh]: https://esm.sh
|
||||
|
||||
[typescript]: https://www.typescriptlang.org
|
||||
|
||||
[license]: license
|
||||
|
||||
[author]: https://wooorm.com
|
||||
|
||||
[health]: https://github.com/syntax-tree/.github
|
||||
|
||||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
|
||||
|
||||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
|
||||
|
||||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
|
||||
|
||||
[hast]: https://github.com/syntax-tree/hast
|
||||
|
||||
[mdast]: https://github.com/syntax-tree/mdast
|
||||
|
||||
[node]: https://github.com/syntax-tree/mdast#nodes
|
||||
|
||||
[preorder]: https://github.com/syntax-tree/unist#preorder
|
||||
|
||||
[text]: https://github.com/syntax-tree/mdast#text
|
||||
|
||||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
|
||||
|
||||
[test]: https://github.com/syntax-tree/unist-util-is#api
|
||||
|
||||
[hast-util-find-and-replace]: https://github.com/syntax-tree/hast-util-find-and-replace
|
||||
|
||||
[api-find-and-replace]: #findandreplacetree-list-options
|
||||
|
||||
[api-options]: #options
|
||||
|
||||
[api-find]: #find
|
||||
|
||||
[api-replace]: #replace
|
||||
|
||||
[api-replace-function]: #replacefunction
|
||||
|
||||
[api-find-and-replace-list]: #findandreplacelist
|
||||
|
||||
[api-find-and-replace-tuple]: #findandreplacetuple
|
||||
|
||||
[api-regexp-match-object]: #regexpmatchobject
|
Reference in New Issue
Block a user