2.3.12 • Published 4 months ago

@yozora/ast-util v2.3.12

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Utility functions to handle Yozora markdown ast.

Install

  • npm

    npm install --save @yozora/ast-util
  • yarn

    yarn add @yozora/ast-util

Usage

NameDescription
calcDefinitionMapTraverse yozora ast and generate a link reference definition map.
calcExcerptAstCalc 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
collectDefinitionsCollect link reference definitions in a pre-order traversal.
collectFootnoteDefinitionsCollect footnote reference definitions in a pre-order traversal.
defaultUrlResolverDefault url resolver
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.
shallowMutateAstInPreorderTraverse AST and replace nodes in pre-order.
traverseAstTraverse Yozora AST and perform a mutating operation for each matched node

Example

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
  )
)

Related

2.3.12

4 months ago

2.3.11

5 months ago

2.3.10

5 months ago

2.3.9

6 months ago

2.3.8

7 months ago

2.3.7

7 months ago

2.3.6

8 months ago

2.3.4

9 months ago

2.3.5

8 months ago

2.3.3

9 months ago

2.3.2

1 year ago

2.3.1

1 year ago

2.2.0

2 years ago

2.3.0

2 years ago

2.1.5

2 years ago

2.0.6

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.5-alpha.0

2 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.5

2 years ago

2.0.4

3 years ago

2.0.2-alpha.0

3 years ago

2.0.1

3 years ago

2.0.0-alpha.3

3 years ago

2.0.0-alpha.0

3 years ago

2.0.0-alpha.1

3 years ago

2.0.0-alpha.2

3 years ago

2.0.0

3 years ago

1.3.0

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.2.0-alpha.1

4 years ago

1.2.0-alpha.0

4 years ago

1.1.0

4 years ago

1.1.0-alpha.2

4 years ago

1.1.0-alpha.1

4 years ago

1.1.0-alpha.0

4 years ago

1.0.0

4 years ago

1.0.0-beta.0

4 years ago

1.0.0-alpha.29

4 years ago

1.0.0-alpha.27

4 years ago

1.0.0-alpha.28

4 years ago

1.0.0-alpha.26

4 years ago

1.0.0-alpha.25

4 years ago

1.0.0-alpha.23

4 years ago

1.0.0-alpha.24

4 years ago

1.0.0-alpha.21

4 years ago

1.0.0-alpha.20

4 years ago

1.0.0-alpha.22

4 years ago

1.0.0-alpha.19

4 years ago

1.0.0-alpha.18

4 years ago

1.0.0-alpha.17

4 years ago

1.0.0-alpha.16

4 years ago

1.0.0-alpha.15

4 years ago

1.0.0-alpha.14

4 years ago

1.0.0-alpha.13

4 years ago