@yozora/ast-util
This package contains a collect of utility functions to handle Yozora markdown ast.
Install
- npm
- Yarn
- pnpm
npm install --save @yozora/ast-util
yarn add @yozora/ast-util
pnpm add @yozora/ast-util
Usage
Name | Description |
---|---|
calcDefinitionMap | Traverse yozora ast and generate a link reference definition map. |
calcExcerptAst | Generate a excerpt ast from the original ast. |
calcFootnoteDefinitionMap | Traverse yozora ast and generate a footnote reference definition map. |
calcHeadingToc | Generate heading toc, and update the referenced Heading.identifier simultaneously |
calcIdentifierSet | Generate a definition identifier set from Yozora AST. |
calcIdentifierFromYastNodes | Generate a link identifier for Node list. |
collectDefinitions | Collect link reference definitions in a pre-order traversal. |
collectFootnoteDefinitions | Collect footnote reference definitions in a pre-order traversal. |
collectNodes | Collect nodes with the specified nodeTypes (or matcher) through pre-order traversal. |
defaultUrlResolver | Default url resolver |
removePositions | Remove Position from AST (a shallow cloned AST will be created). |
replaceFootnotesInReferences | Replace inline footnotes into footnote references and footnote reference definitions (irreversible) |
resolveUrlsForAst | Traverse Yozora AST and resolve urls for aim nodes (irreversible) |
searchNode | Search a node from Yozora AST in pre-order traversing |
shallowCloneAst | Shallow clone the Yozora AST until the match reaches the termination condition. |
shallowMutateAstInPostorder | Traverse AST and replace nodes in post-order (immutable). |
shallowMutateAstInPreorder | Traverse AST and replace nodes in pre-order (immutable). |
traverseAST | Traverse Yozora AST and perform a mutating operation for each matched node |
Example
import { ImageType, BlockquoteType } from '@yozora/ast'
import {
collectDefinitions,
collectFootnoteDefinitions,
calcHeadingToc,
shallowMutateAstInPostorder,
traverseAST,
} from '@yozora/ast-util'
// Collect definitions.
collectDefinitions(
root, // Yozora ast root
[DefinitionType], // aim Yast types, optional
[], // preset definitions, optional
)
// Collect footnote definitions.
collectFootnoteDefinitions(
root, // Yozora ast root
[FootnoteDefinitionType], // aim Yast types, optional
[], // preset footnote definitions, optional
true, // prefer reference type footnotes, optional.
)
// traverse the Yozora AST and set the image title to the image alt
traverseAST(
root, // Yozora ast root
[ImageType], // aim Yast types, required
(node) => node.title = node.alt // mutating operation, required
)
// traverse the Yozora AST and replace the image to two images.
shallowMutateAstInPostorder(
root,
[ImageType],
(node) => [node, node]
)
// Generate heading toc, each toc node's identifier will with the prefix 'custom-identifier-prefix-'.
// The default prefix is 'heading-'
calcHeadingToc(root, 'custom-identifier-prefix-')
// shallow clone the Yozora AST until a blockquote type node with a blockquote
// type parent and in addition it is not the first child of its parent encountered.
const root2 = shallowCloneAst(
root,
(node, parent, childIndex) => (
parent.type === BlockquoteType &&
childIndex > 0 &&
node.type === BlockquoteType
)
)