@yozora/markup-weaver
TypeScript icon, indicating that this package has built-in type declarations

2.3.1 • Public • Published

@yozora/markup-weaver


This package is designed to weave the markup-like AST into markup contents.

Install

  • npm

    npm install --save @yozora/markup-weaver
  • yarn

    yarn add @yozora/markup-weaver

Usage

@yozora/markup-weaver provide a DefaultMarkupWeaver to weave markup-like AST into markup contents.

BTW, You can convert a piece of text into markup AST via @yozora/parser or at https://yozora.guanghechen.com.

  • Here is a simple example to weave AST into markup contents, note that position is optional.

    import { DefaultMarkupWeaver } from '@yozora/markup-weaver'
    
    const weaver = new DefaultMarkupWeaver()
    weaver.weave({
      "type": "root",
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "emphasis: "
            },
            {
              "type": "strong",
              "children": [
                {
                  "type": "text",
                  "value": "foo \""
                },
                {
                  "type": "emphasis",
                  "children": [
                    {
                      "type": "text",
                      "value": "bar"
                    }
                  ]
                },
                {
                  "type": "text",
                  "value": "\" foo"
                }
              ]
            }
          ]
        },
        {
          "type": "heading",
          "depth": 1,
          "children": [
            {
              "type": "text",
              "value": "Setext headings"
            }
          ]
        }
      ]
    })
    
    // =>
    // emphasis: **foo "*bar*" foo**
    // # Setext headings
  • Use custom weaver

    import type { Literal } from '@yozora/ast'
    import type { INodeMarkup, INodeWeaver } from '@yozora/markup-weaver'
    import { DefaultMarkupWeaver } from '@yozora/markup-weaver'
    
    type Mention = Literal<'mention'>
    class MentionWeaver implements INodeWeaver<Mention> {
      public readonly type = 'mention'
      public readonly isBlockLevel = (): boolean => false
      public weave(node: Mention): INodeMarkup {
        return { opener: '@' + node.value }
      }
    }
    
    const weaver = new DefaultMarkupWeaver()
    weaver.useWeaver(new MentionWeaver)
    
    weaver.weave({
      "type": "root",
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "mention",
              "value": "guanghechen"
            }
          ]
        }
      ]
    })
    // => @guanghechen

Related

Readme

Keywords

none

Package Sidebar

Install

npm i @yozora/markup-weaver

Weekly Downloads

3

Version

2.3.1

License

MIT

Unpacked Size

118 kB

Total Files

8

Last publish

Collaborators

  • lemonclown