@yozora/tokenizer-fenced-block 2.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-fenced-block",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "author": {
5
5
  "name": "guanghechen",
6
6
  "url": "https://github.com/guanghechen/"
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "files": [
29
29
  "lib/",
30
- "src/",
30
+ "lib/**/*.map",
31
31
  "package.json",
32
32
  "CHANGELOG.md",
33
33
  "LICENSE",
@@ -38,9 +38,9 @@
38
38
  "prepublishOnly": "cross-env ROLLUP_SHOULD_SOURCEMAP=false yarn build"
39
39
  },
40
40
  "dependencies": {
41
- "@yozora/ast": "^2.1.2",
42
- "@yozora/character": "^2.1.2",
43
- "@yozora/core-tokenizer": "^2.1.2"
41
+ "@yozora/ast": "^2.1.4",
42
+ "@yozora/character": "^2.1.4",
43
+ "@yozora/core-tokenizer": "^2.1.4"
44
44
  },
45
- "gitHead": "992bacafd173e7788e99fed34ce8b45f6ed24cfe"
45
+ "gitHead": "aa464ed1e3cd84892773a833910cfc53a556bf5f"
46
46
  }
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- export { match as fencedBlockMatch } from './match'
2
- export { FencedBlockTokenizer, FencedBlockTokenizer as default } from './tokenizer'
3
- export { FencedBlockType } from './types'
4
- export type {
5
- IFencedBlockHookContext,
6
- IToken as IFencedBlockToken,
7
- ITokenizerProps as IFencedBlockTokenizerProps,
8
- } from './types'
package/src/match.ts DELETED
@@ -1,170 +0,0 @@
1
- import type { NodeType } from '@yozora/ast'
2
- import type { INodePoint } from '@yozora/character'
3
- import { calcTrimBoundaryOfCodePoints, isSpaceCharacter } from '@yozora/character'
4
- import type {
5
- IBlockToken,
6
- IMatchBlockHook,
7
- IMatchBlockPhaseApi,
8
- IPhrasingContentLine,
9
- IResultOfEatAndInterruptPreviousSibling,
10
- IResultOfEatContinuationText,
11
- IResultOfEatOpener,
12
- } from '@yozora/core-tokenizer'
13
- import { calcEndPoint, calcStartPoint, eatOptionalCharacters } from '@yozora/core-tokenizer'
14
- import type { IFencedBlockHookContext, IToken } from './types'
15
-
16
- export function match<
17
- T extends NodeType = NodeType,
18
- IThis extends IFencedBlockHookContext<T> = IFencedBlockHookContext<T>,
19
- >(this: IThis, _api: IMatchBlockPhaseApi): IMatchBlockHook<T, IToken<T>> {
20
- const { nodeType, markers, markersRequired, checkInfoString } = this
21
- return {
22
- isContainingBlock: false,
23
- eatOpener,
24
- eatAndInterruptPreviousSibling,
25
- eatContinuationText,
26
- }
27
-
28
- function eatOpener(line: Readonly<IPhrasingContentLine>): IResultOfEatOpener<T, IToken<T>> {
29
- /**
30
- * Four spaces indentation produces an indented code block
31
- * @see https://github.github.com/gfm/#example-104
32
- */
33
- if (line.countOfPrecedeSpaces >= 4) return null
34
-
35
- const { endIndex, firstNonWhitespaceIndex } = line
36
- if (firstNonWhitespaceIndex + markersRequired - 1 >= endIndex) return null
37
-
38
- const { nodePoints, startIndex } = line
39
- const marker: number = nodePoints[firstNonWhitespaceIndex].codePoint
40
- if (markers.indexOf(marker) < 0) return null
41
-
42
- const i = eatOptionalCharacters(nodePoints, firstNonWhitespaceIndex + 1, endIndex, marker)
43
- const countOfMark = i - firstNonWhitespaceIndex
44
-
45
- /**
46
- * The number of marker is not enough.
47
- * @see https://github.github.com/gfm/#example-91
48
- */
49
- if (countOfMark < markersRequired) return null
50
-
51
- /**
52
- * Eating Information string
53
- * The line with the opening block fence may optionally contain some text
54
- * following the block fence; this is trimmed of leading and trailing
55
- * whitespace and called the info string. If the info string comes after
56
- * a backtick fence, it may not contain any backtick characters. (The
57
- * reason for this restriction is that otherwise some inline code would
58
- * be incorrectly interpreted as the beginning of a fenced code block.)
59
- * @see https://github.github.com/gfm/#info-string
60
- */
61
- const [iLft, iRht] = calcTrimBoundaryOfCodePoints(nodePoints, i, endIndex)
62
- const infoString: INodePoint[] = nodePoints.slice(iLft, iRht)
63
-
64
- /**
65
- * Check if info string is valid, such as info strings for backtick code
66
- * blocks cannot contain backticks.
67
- * @see https://github.github.com/gfm/#example-115
68
- * @see https://github.github.com/gfm/#example-116
69
- */
70
- if (checkInfoString != null && !checkInfoString(infoString, marker, countOfMark)) {
71
- return null
72
- }
73
-
74
- const nextIndex = endIndex
75
- const token: IToken<T> = {
76
- nodeType: nodeType as T,
77
- position: {
78
- start: calcStartPoint(nodePoints, startIndex),
79
- end: calcEndPoint(nodePoints, nextIndex - 1),
80
- },
81
- indent: firstNonWhitespaceIndex - startIndex,
82
- marker: marker!,
83
- markerCount: countOfMark,
84
- lines: [],
85
- infoString,
86
- }
87
- return { token, nextIndex }
88
- }
89
-
90
- function eatAndInterruptPreviousSibling(
91
- line: Readonly<IPhrasingContentLine>,
92
- prevSiblingToken: Readonly<IBlockToken>,
93
- ): IResultOfEatAndInterruptPreviousSibling<T, IToken<T>> {
94
- const result = eatOpener(line)
95
- if (result == null) return null
96
- return {
97
- token: result.token,
98
- nextIndex: result.nextIndex,
99
- remainingSibling: prevSiblingToken,
100
- }
101
- }
102
-
103
- function eatContinuationText(
104
- line: Readonly<IPhrasingContentLine>,
105
- token: IToken<T>,
106
- ): IResultOfEatContinuationText {
107
- const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex, countOfPrecedeSpaces } = line
108
-
109
- /**
110
- * Check closing block fence
111
- *
112
- * The closing block fence may be indented up to three spaces, and may be
113
- * followed only by spaces, which are ignored. If the end of the containing
114
- * block (or document) is reached and no closing block fence has been found,
115
- * the code block contains all of the lines after the opening block fence
116
- * until the end of the containing block (or document). (An alternative spec
117
- * would require backtracking in the event that a closing block fence is not
118
- * found. But this makes parsing much less efficient, and there seems to be
119
- * no real down side to the behavior described here.)
120
- * @see https://github.github.com/gfm/#code-fence
121
- *
122
- * Closing fence indented with at most 3 spaces
123
- * @see https://github.github.com/gfm/#example-107
124
- */
125
- if (countOfPrecedeSpaces < 4 && firstNonWhitespaceIndex < endIndex) {
126
- let i = eatOptionalCharacters(nodePoints, firstNonWhitespaceIndex, endIndex, token.marker)
127
- const markerCount = i - firstNonWhitespaceIndex
128
-
129
- /**
130
- * The closing block fence must be at least as long as the opening fence
131
- * @see https://github.github.com/gfm/#example-94
132
- */
133
- if (markerCount >= token.markerCount) {
134
- // The closing block fence may be followed only by spaces.
135
- for (; i < endIndex; ++i) {
136
- const c = nodePoints[i].codePoint
137
- if (!isSpaceCharacter(c)) break
138
- }
139
-
140
- if (i + 1 >= endIndex) {
141
- return { status: 'closing', nextIndex: endIndex }
142
- }
143
- }
144
- }
145
-
146
- /**
147
- * Eating code content
148
- *
149
- * The content of the code block consists of all subsequent lines, until a
150
- * closing block fence of the same type as the code block began with
151
- * (backticks or tildes), and with at least as many backticks or tildes as
152
- * the opening block fence. If the leading block fence is indented N spaces,
153
- * then up to N spaces of indentation are removed from each line of the
154
- * content (if present).
155
- *
156
- * If a content line is not indented, it is preserved unchanged. If it is
157
- * indented less than N spaces, all of the indentation is removed, but the
158
- * line feed should be preserve.
159
- */
160
- const firstIndex = Math.min(startIndex + token.indent, firstNonWhitespaceIndex, endIndex - 1)
161
- token.lines.push({
162
- nodePoints,
163
- startIndex: firstIndex,
164
- endIndex,
165
- firstNonWhitespaceIndex,
166
- countOfPrecedeSpaces,
167
- })
168
- return { status: 'opening', nextIndex: endIndex }
169
- }
170
- }
package/src/tokenizer.ts DELETED
@@ -1,44 +0,0 @@
1
- import type { Node, NodeType } from '@yozora/ast'
2
- import type { ICodePoint } from '@yozora/character'
3
- import type { IBlockTokenizer, IMatchBlockHookCreator } from '@yozora/core-tokenizer'
4
- import { BaseBlockTokenizer, TokenizerPriority } from '@yozora/core-tokenizer'
5
- import { match } from './match'
6
- import type { IFencedBlockHookContext, IToken, ITokenizerProps } from './types'
7
-
8
- /**
9
- * Lexical Matcher for FencedBlock.
10
- *
11
- * A block fence is a sequence of fence-marker characters (different marker
12
- * cannot be mixed.) A fenced block begins with a block fence, indented no more
13
- * than three spaces.
14
- *
15
- * @see https://github.com/syntax-tree/mdast#code
16
- * @see https://github.github.com/gfm/#code-fence
17
- */
18
- export abstract class FencedBlockTokenizer<
19
- T extends NodeType = NodeType,
20
- INode extends Node<T> = Node<T>,
21
- IThis extends IFencedBlockHookContext<T> = IFencedBlockHookContext<T>,
22
- >
23
- extends BaseBlockTokenizer<T, IToken<T>, INode, any>
24
- implements IBlockTokenizer<T, IToken<T>, INode, IThis>
25
- {
26
- protected readonly nodeType: T
27
- protected readonly markers: ICodePoint[] = []
28
- protected readonly markersRequired: number
29
- protected readonly checkInfoString: ITokenizerProps<T>['checkInfoString']
30
-
31
- /* istanbul ignore next */
32
- constructor(props: ITokenizerProps<T>) {
33
- super({
34
- name: props.name,
35
- priority: props.priority ?? TokenizerPriority.FENCED_BLOCK,
36
- })
37
- this.nodeType = props.nodeType
38
- this.markers = props.markers
39
- this.markersRequired = props.markersRequired
40
- this.checkInfoString = props.checkInfoString
41
- }
42
-
43
- public override readonly match: IMatchBlockHookCreator<T, IToken<T>, IThis> = match
44
- }
package/src/types.ts DELETED
@@ -1,91 +0,0 @@
1
- import type { NodeType } from '@yozora/ast'
2
- import type { ICodePoint, INodePoint } from '@yozora/character'
3
- import type {
4
- IBaseBlockTokenizerProps,
5
- IPartialBlockToken,
6
- IPhrasingContentLine,
7
- ITokenizer,
8
- } from '@yozora/core-tokenizer'
9
-
10
- export const FencedBlockType = 'fencedBlock'
11
- // eslint-disable-next-line @typescript-eslint/no-redeclare
12
- export type FencedBlockType = typeof FencedBlockType
13
-
14
- export interface IToken<T extends NodeType> extends IPartialBlockToken<T> {
15
- /**
16
- * Line indent of a fenced block.
17
- */
18
- indent: number
19
- /**
20
- * Fence marker.
21
- */
22
- marker: number
23
- /**
24
- * The number of fence marker.
25
- */
26
- markerCount: number
27
- /**
28
- * Lines to construct the contents of a paragraph.
29
- */
30
- lines: IPhrasingContentLine[]
31
- /**
32
- * Meta info string
33
- */
34
- infoString: INodePoint[]
35
- }
36
-
37
- export interface IFencedBlockHookContext<T extends NodeType> extends ITokenizer {
38
- /**
39
- * Type of special FencedBlock token and FencedBlock node.
40
- */
41
- nodeType: T
42
- /**
43
- * Available fence markers.
44
- */
45
- markers: ICodePoint[]
46
- /**
47
- * The minimum amount required
48
- */
49
- markersRequired: number
50
- /**
51
- * Check if the info string is valid.
52
- * @param infoString
53
- * @param marker
54
- * @param countOfMarker
55
- */
56
- checkInfoString?(
57
- infoString: Readonly<INodePoint[]>,
58
- marker: ICodePoint,
59
- countOfMarker: number,
60
- ): boolean
61
- }
62
-
63
- export interface ITokenizerProps<T extends NodeType> extends Partial<IBaseBlockTokenizerProps> {
64
- /**
65
- * ITokenizer name.
66
- */
67
- name: string
68
- /**
69
- * Type of special FencedBlock token and FencedBlock node.
70
- */
71
- nodeType: T
72
- /**
73
- * Available fence markers.
74
- */
75
- markers: ICodePoint[]
76
- /**
77
- * The minimum amount required
78
- */
79
- markersRequired: number
80
- /**
81
- * Check if the info string is valid.
82
- * @param infoString
83
- * @param marker
84
- * @param countOfMarker
85
- */
86
- checkInfoString?(
87
- infoString: Readonly<INodePoint[]>,
88
- marker: ICodePoint,
89
- countOfMarker: number,
90
- ): boolean
91
- }