Skip to main content
Version: 3.x.x 🚧

@yozora/ast-util

Npm VersionNpm DownloadNpm LicenseModule formats: cjs, esmNode.js VersionCode Style: prettier

This package contains a collect of utility functions to handle Yozora markdown ast.

Install

npm install --save @yozora/ast-util

Usage

NameDescription
calcDefinitionMapTraverse yozora ast and generate a link reference definition map.
calcExcerptAstGenerate a excerpt ast from the original ast.
calcFootnoteDefinitionMapTraverse yozora ast and generate a footnote reference definition map.
calcHeadingTocGenerate heading toc, and update the referenced Heading.identifier simultaneously
calcIdentifierSetGenerate a definition identifier set from Yozora AST.
calcIdentifierFromYastNodesGenerate a link identifier for Node list.
collectDefinitionsCollect link reference definitions in a pre-order traversal.
collectFootnoteDefinitionsCollect footnote reference definitions in a pre-order traversal.
collectNodesCollect nodes with the specified nodeTypes (or matcher) through pre-order traversal.
defaultUrlResolverDefault url resolver
removePositionsRemove Position from AST (a shallow cloned AST will be created).
replaceFootnotesInReferencesReplace inline footnotes into footnote references and footnote reference definitions (irreversible)
resolveUrlsForAstTraverse Yozora AST and resolve urls for aim nodes (irreversible)
searchNodeSearch a node from Yozora AST in pre-order traversing
shallowCloneAstShallow clone the Yozora AST until the match reaches the termination condition.
shallowMutateAstInPostorderTraverse AST and replace nodes in post-order (immutable).
shallowMutateAstInPreorderTraverse AST and replace nodes in pre-order (immutable).
traverseASTTraverse 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
)
)