Utility functions to handle Yozora markdown ast.
-
npm
npm install --save @yozora/ast-util
-
yarn
yarn add @yozora/ast-util
Name | Description |
---|---|
calcDefinitionMap |
Traverse yozora ast and generate a link reference definition map. |
calcExcerptAst |
Calc 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 |
collectDefinitions |
Collect link reference definitions in a pre-order traversal. |
collectFootnoteDefinitions |
Collect footnote reference definitions in a pre-order traversal. |
defaultUrlResolver |
Default url resolver |
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. |
shallowMutateAstInPreorder |
Traverse AST and replace nodes in pre-order. |
traverseAst |
Traverse Yozora AST and perform a mutating operation for each matched node |
import { ImageType, BlockquoteType } from '@yozora/ast'
import {
collectDefinitions,
collectFootnoteDefinitions,
calcHeadingToc,
replaceAST,
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
)
// 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
)
)