Skip to main content
Version: 2.x.x

@yozora/core-parser

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

@yozora/core-parser provides a plug-in system that can load tokenizer for collaborative processing in the lifecycles.

Install

npm install --save @yozora/core-parser

Usage

@yozora/core-parser provides a default Parser implementation DefaultParser.

import { DefaultParser } from '@yozora/core-parser'
import EmphasisTokenizer from '@yozora/tokenizer-emphasis'
import ParagraphTokenizer from '@yozora/tokenizer-paragraph'
import TextTokenizer from '@yozora/tokenizer-text'

const parser = new DefaultParser({ shouldReservePosition: true })
.useFallbackTokenizer(new ParagraphTokenizer())
.useFallbackTokenizer(new TextTokenizer())

// add custom tokenizers through `useTokenizer()`
.useTokenizer(new EmphasisTokenizer())

// Parse literal string
parser.parse('source content such as **emphasis**')

// Parse literal string lists
parser.parse(['source content', 'additional *content*'])

// Parse iterable string
// The Parser will processing at every time a line is read.
function* genContents () {
yield 'source content1\n'
yield 'source content2\n'
return 'source content3\n'
}
parser.parse(genContents())

// Reserve the positions
parser.parse('source content', { shouldReservePosition: true })

DefaultParser Options

  • Constructor Options

    NameRequiredDefaultType
    blockFallbackTokenizerfalsenullBlockFallbackTokenizer | null
    inlineFallbackTokenizerfalsenullInlineFallbackTokenizer | null
    defaultParseOptionsfalseSee belowParse Options
    • blockFallbackTokenizer: Fallback tokenizer on processing block structure phase.

    • inlineFallbackTokenizer: Fallback tokenizer on processing inline structure phase.

    • defaultParseOptions: Default options for parse(). Default:

      {
      presetDefinitions: [],
      presetFootnoteDefinitions: [],
      shouldReservePosition: false
      }
  • Parse Options

    NameRequiredDefaultType
    presetDefinitionsfalsedefaultParserOptions.presetDefinitions[Association][src-Association] list.
    presetFootnoteDefinitionsfalsedefaultParserOptions.presetFootnoteDefinitionsboolean
    shouldReservePositionfalsedefaultParserOptions.shouldReservePositionboolean
    • presetDefinitions: Preset definition meta data list.
    • presetFootnoteDefinitions: Preset footnote definition meta data list.
    • shouldReservePosition: Whether it is necessary to reserve the position of the Node parsed.

Lifecycle

math-block

parse-block

match-inline

parse-inline