Skip to main content
Version: 2.x.x

@yozora/tokenizer-heading

Npm VersionNpm DownloadNpm LicenseModule formats: cjs, esmNode.js VersionTested with JestCode Style: prettier

github flavor markdown spec

An ATX heading consists of a string of characters, parsed as inline content, between an opening sequence of 1-6 unescaped # characters and an optional closing sequence of any number of unescaped # characters. The opening sequence of # characters must be followed by a space or by the end of line. The optional closing sequence of #s must be preceded by a space and may be followed by spaces only. The opening # character may be indented 0-3 spaces. The raw contents of the heading are stripped of leading and trailing spaces before being parsed as inline content. The heading level is equal to the number of # characters in the opening sequence.

Install

npm install --save @yozora/tokenizer-heading

Usage

tip

@yozora/tokenizer-heading has been integrated into @yozora/parser / @yozora/parser-gfm-ex / @yozora/parser-gfm, so you can use YozoraParser / GfmExParser / GfmParser directly.

import YozoraParser from '@yozora/parser'

const parser = new YozoraParser()

// parse source markdown content
parser.parse(`
# h1
## h2
### h3
#### h4
##### h5
###### h6
`)

Options

NameTypeRequiredDefault
namestringfalse"@yozora/tokenizer-heading"
prioritynumberfalseTokenizerPriority.ATOMIC
  • name: The unique name of the tokenizer, used to bind the token it generates, to determine the tokenizer that should be called in each life cycle of the token in the entire matching / parsing phase.

  • priority: Priority of the tokenizer, determine the order of processing, high priority priority execution. interruptable. In addition, in the match-block stage, a high-priority tokenizer can interrupt the matching process of a low-priority tokenizer.

Types

@yozora/tokenizer-heading produce Heading type nodes. See @yozora/ast for full base types.

import type { Parent } from '@yozora/ast'

export const HeadingType = 'heading'
export type HeadingType = typeof HeadingType

/**
* Heading represents a heading of a section.
* @see https://github.com/syntax-tree/mdast#heading
* @see https://github.github.com/gfm/#atx-heading
*/
export interface Heading extends Parent<HeadingType> {
/**
* HTML anchor identifier.
*/
identifier?: string
/**
* level of heading
*/
depth: 1 | 2 | 3 | 4 | 5 | 6
}

Live Examples

  • Basic

    #32
      
      
  • More than six # characters is not a heading.

    #33
      
      
  • At least one space is required between the # characters and the heading’s contents, unless the heading is empty.

    #34
      
      
  • This is not a heading, because the first # is escaped.

    #35
      
      
  • Contents are parsed as inlines.

    #36
      
      
  • Leading and trailing whitespace is ignored in parsing inline content.

    #37
      
      
  • One to three spaces indentation are allowed.

    #38
      
      
  • Four spaces are too much.

      
      
  • A closing sequence of # characters is optional.

    #41
      
      
  • It need not be the same length as the opening sequence.

    #42
      
      
  • Spaces are allowed after the closing sequence.

    #43
      
      
  • A sequence of # characters with anything but spaces following it is not a closing sequence, but counts as part of the contents of the heading.

    #44
      
      
  • The closing sequence must be preceded by a space.

    #45
      
      
  • Backslash-escaped # characters do not count as part of the closing sequence.

    #46
      
      
  • ATX headings need not be separated from surrounding content by blank lines, and they can interrupt paragraphs.

      
      
  • ATX headings can be empty.

    #49