@yozora/tokenizer-heading 1.2.0 → 2.0.0-alpha.0

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/lib/cjs/index.js CHANGED
@@ -6,24 +6,18 @@ var ast = require('@yozora/ast');
6
6
  var character = require('@yozora/character');
7
7
  var coreTokenizer = require('@yozora/core-tokenizer');
8
8
 
9
- const uniqueName = '@yozora/tokenizer-heading';
10
-
11
- class HeadingTokenizer extends coreTokenizer.BaseBlockTokenizer {
12
- constructor(props = {}) {
13
- var _a, _b;
14
- super({
15
- name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
16
- priority: (_b = props.priority) !== null && _b !== void 0 ? _b : coreTokenizer.TokenizerPriority.ATOMIC,
17
- });
18
- this.isContainingBlock = false;
19
- }
20
- eatOpener(line) {
9
+ const match = function () {
10
+ return {
11
+ isContainingBlock: false,
12
+ eatOpener,
13
+ eatAndInterruptPreviousSibling,
14
+ };
15
+ function eatOpener(line) {
21
16
  if (line.countOfPrecedeSpaces >= 4)
22
17
  return null;
23
18
  const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex } = line;
24
19
  if (firstNonWhitespaceIndex >= endIndex ||
25
- nodePoints[firstNonWhitespaceIndex].codePoint !==
26
- character.AsciiCodePoint.NUMBER_SIGN) {
20
+ nodePoints[firstNonWhitespaceIndex].codePoint !== character.AsciiCodePoint.NUMBER_SIGN) {
27
21
  return null;
28
22
  }
29
23
  const i = coreTokenizer.eatOptionalCharacters(nodePoints, firstNonWhitespaceIndex + 1, endIndex, character.AsciiCodePoint.NUMBER_SIGN);
@@ -44,8 +38,8 @@ class HeadingTokenizer extends coreTokenizer.BaseBlockTokenizer {
44
38
  };
45
39
  return { token, nextIndex, saturated: true };
46
40
  }
47
- eatAndInterruptPreviousSibling(line, prevSiblingToken) {
48
- const result = this.eatOpener(line);
41
+ function eatAndInterruptPreviousSibling(line, prevSiblingToken) {
42
+ const result = eatOpener(line);
49
43
  if (result == null)
50
44
  return null;
51
45
  return {
@@ -54,47 +48,68 @@ class HeadingTokenizer extends coreTokenizer.BaseBlockTokenizer {
54
48
  remainingSibling: prevSiblingToken,
55
49
  };
56
50
  }
57
- parseBlock(token, children, api) {
58
- const { nodePoints, firstNonWhitespaceIndex, endIndex } = token.line;
59
- let [leftIndex, rightIndex] = character.calcTrimBoundaryOfCodePoints(nodePoints, firstNonWhitespaceIndex + token.depth, endIndex);
60
- let closeCharCount = 0;
61
- for (let j = rightIndex - 1; j >= leftIndex; --j) {
62
- const c = nodePoints[j].codePoint;
63
- if (c !== character.AsciiCodePoint.NUMBER_SIGN)
64
- break;
65
- closeCharCount += 1;
66
- }
67
- if (closeCharCount > 0) {
68
- let spaceCount = 0, j = rightIndex - 1 - closeCharCount;
69
- for (; j >= leftIndex; --j) {
51
+ };
52
+
53
+ const parse = function (api) {
54
+ return {
55
+ parse: token => {
56
+ const { nodePoints, firstNonWhitespaceIndex, endIndex } = token.line;
57
+ let [leftIndex, rightIndex] = character.calcTrimBoundaryOfCodePoints(nodePoints, firstNonWhitespaceIndex + token.depth, endIndex);
58
+ let closeCharCount = 0;
59
+ for (let j = rightIndex - 1; j >= leftIndex; --j) {
70
60
  const c = nodePoints[j].codePoint;
71
- if (!character.isWhitespaceCharacter(c))
61
+ if (c !== character.AsciiCodePoint.NUMBER_SIGN)
72
62
  break;
73
- spaceCount += 1;
63
+ closeCharCount += 1;
74
64
  }
75
- if (spaceCount > 0 || j < leftIndex) {
76
- rightIndex -= closeCharCount + spaceCount;
65
+ if (closeCharCount > 0) {
66
+ let spaceCount = 0, j = rightIndex - 1 - closeCharCount;
67
+ for (; j >= leftIndex; --j) {
68
+ const c = nodePoints[j].codePoint;
69
+ if (!character.isWhitespaceCharacter(c))
70
+ break;
71
+ spaceCount += 1;
72
+ }
73
+ if (spaceCount > 0 || j < leftIndex) {
74
+ rightIndex -= closeCharCount + spaceCount;
75
+ }
77
76
  }
78
- }
79
- const lines = [
80
- {
81
- nodePoints,
82
- startIndex: leftIndex,
83
- endIndex: rightIndex,
84
- firstNonWhitespaceIndex: leftIndex,
85
- countOfPrecedeSpaces: 0,
86
- },
87
- ];
88
- const phrasingContent = api.buildPhrasingContent(lines);
89
- const node = {
90
- type: ast.HeadingType,
91
- depth: token.depth,
92
- children: phrasingContent == null ? [] : [phrasingContent],
93
- };
94
- return node;
77
+ const lines = [
78
+ {
79
+ nodePoints,
80
+ startIndex: leftIndex,
81
+ endIndex: rightIndex,
82
+ firstNonWhitespaceIndex: leftIndex,
83
+ countOfPrecedeSpaces: 0,
84
+ },
85
+ ];
86
+ const phrasingContent = api.buildPhrasingContent(lines);
87
+ const node = {
88
+ type: ast.HeadingType,
89
+ depth: token.depth,
90
+ children: phrasingContent == null ? [] : [phrasingContent],
91
+ };
92
+ return node;
93
+ },
94
+ };
95
+ };
96
+
97
+ const uniqueName = '@yozora/tokenizer-heading';
98
+
99
+ class HeadingTokenizer extends coreTokenizer.BaseBlockTokenizer {
100
+ constructor(props = {}) {
101
+ var _a, _b;
102
+ super({
103
+ name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
104
+ priority: (_b = props.priority) !== null && _b !== void 0 ? _b : coreTokenizer.TokenizerPriority.ATOMIC,
105
+ });
106
+ this.match = match;
107
+ this.parse = parse;
95
108
  }
96
109
  }
97
110
 
98
111
  exports.HeadingTokenizer = HeadingTokenizer;
99
112
  exports.HeadingTokenizerName = uniqueName;
100
- exports['default'] = HeadingTokenizer;
113
+ exports["default"] = HeadingTokenizer;
114
+ exports.headingMatch = match;
115
+ exports.headingParse = parse;
package/lib/esm/index.js CHANGED
@@ -1,25 +1,19 @@
1
1
  import { HeadingType } from '@yozora/ast';
2
2
  import { AsciiCodePoint, isSpaceCharacter, calcTrimBoundaryOfCodePoints, isWhitespaceCharacter } from '@yozora/character';
3
- import { BaseBlockTokenizer, TokenizerPriority, eatOptionalCharacters, calcStartYastNodePoint, calcEndYastNodePoint } from '@yozora/core-tokenizer';
3
+ import { eatOptionalCharacters, calcStartYastNodePoint, calcEndYastNodePoint, BaseBlockTokenizer, TokenizerPriority } from '@yozora/core-tokenizer';
4
4
 
5
- const uniqueName = '@yozora/tokenizer-heading';
6
-
7
- class HeadingTokenizer extends BaseBlockTokenizer {
8
- constructor(props = {}) {
9
- var _a, _b;
10
- super({
11
- name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
12
- priority: (_b = props.priority) !== null && _b !== void 0 ? _b : TokenizerPriority.ATOMIC,
13
- });
14
- this.isContainingBlock = false;
15
- }
16
- eatOpener(line) {
5
+ const match = function () {
6
+ return {
7
+ isContainingBlock: false,
8
+ eatOpener,
9
+ eatAndInterruptPreviousSibling,
10
+ };
11
+ function eatOpener(line) {
17
12
  if (line.countOfPrecedeSpaces >= 4)
18
13
  return null;
19
14
  const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex } = line;
20
15
  if (firstNonWhitespaceIndex >= endIndex ||
21
- nodePoints[firstNonWhitespaceIndex].codePoint !==
22
- AsciiCodePoint.NUMBER_SIGN) {
16
+ nodePoints[firstNonWhitespaceIndex].codePoint !== AsciiCodePoint.NUMBER_SIGN) {
23
17
  return null;
24
18
  }
25
19
  const i = eatOptionalCharacters(nodePoints, firstNonWhitespaceIndex + 1, endIndex, AsciiCodePoint.NUMBER_SIGN);
@@ -40,8 +34,8 @@ class HeadingTokenizer extends BaseBlockTokenizer {
40
34
  };
41
35
  return { token, nextIndex, saturated: true };
42
36
  }
43
- eatAndInterruptPreviousSibling(line, prevSiblingToken) {
44
- const result = this.eatOpener(line);
37
+ function eatAndInterruptPreviousSibling(line, prevSiblingToken) {
38
+ const result = eatOpener(line);
45
39
  if (result == null)
46
40
  return null;
47
41
  return {
@@ -50,45 +44,64 @@ class HeadingTokenizer extends BaseBlockTokenizer {
50
44
  remainingSibling: prevSiblingToken,
51
45
  };
52
46
  }
53
- parseBlock(token, children, api) {
54
- const { nodePoints, firstNonWhitespaceIndex, endIndex } = token.line;
55
- let [leftIndex, rightIndex] = calcTrimBoundaryOfCodePoints(nodePoints, firstNonWhitespaceIndex + token.depth, endIndex);
56
- let closeCharCount = 0;
57
- for (let j = rightIndex - 1; j >= leftIndex; --j) {
58
- const c = nodePoints[j].codePoint;
59
- if (c !== AsciiCodePoint.NUMBER_SIGN)
60
- break;
61
- closeCharCount += 1;
62
- }
63
- if (closeCharCount > 0) {
64
- let spaceCount = 0, j = rightIndex - 1 - closeCharCount;
65
- for (; j >= leftIndex; --j) {
47
+ };
48
+
49
+ const parse = function (api) {
50
+ return {
51
+ parse: token => {
52
+ const { nodePoints, firstNonWhitespaceIndex, endIndex } = token.line;
53
+ let [leftIndex, rightIndex] = calcTrimBoundaryOfCodePoints(nodePoints, firstNonWhitespaceIndex + token.depth, endIndex);
54
+ let closeCharCount = 0;
55
+ for (let j = rightIndex - 1; j >= leftIndex; --j) {
66
56
  const c = nodePoints[j].codePoint;
67
- if (!isWhitespaceCharacter(c))
57
+ if (c !== AsciiCodePoint.NUMBER_SIGN)
68
58
  break;
69
- spaceCount += 1;
59
+ closeCharCount += 1;
70
60
  }
71
- if (spaceCount > 0 || j < leftIndex) {
72
- rightIndex -= closeCharCount + spaceCount;
61
+ if (closeCharCount > 0) {
62
+ let spaceCount = 0, j = rightIndex - 1 - closeCharCount;
63
+ for (; j >= leftIndex; --j) {
64
+ const c = nodePoints[j].codePoint;
65
+ if (!isWhitespaceCharacter(c))
66
+ break;
67
+ spaceCount += 1;
68
+ }
69
+ if (spaceCount > 0 || j < leftIndex) {
70
+ rightIndex -= closeCharCount + spaceCount;
71
+ }
73
72
  }
74
- }
75
- const lines = [
76
- {
77
- nodePoints,
78
- startIndex: leftIndex,
79
- endIndex: rightIndex,
80
- firstNonWhitespaceIndex: leftIndex,
81
- countOfPrecedeSpaces: 0,
82
- },
83
- ];
84
- const phrasingContent = api.buildPhrasingContent(lines);
85
- const node = {
86
- type: HeadingType,
87
- depth: token.depth,
88
- children: phrasingContent == null ? [] : [phrasingContent],
89
- };
90
- return node;
73
+ const lines = [
74
+ {
75
+ nodePoints,
76
+ startIndex: leftIndex,
77
+ endIndex: rightIndex,
78
+ firstNonWhitespaceIndex: leftIndex,
79
+ countOfPrecedeSpaces: 0,
80
+ },
81
+ ];
82
+ const phrasingContent = api.buildPhrasingContent(lines);
83
+ const node = {
84
+ type: HeadingType,
85
+ depth: token.depth,
86
+ children: phrasingContent == null ? [] : [phrasingContent],
87
+ };
88
+ return node;
89
+ },
90
+ };
91
+ };
92
+
93
+ const uniqueName = '@yozora/tokenizer-heading';
94
+
95
+ class HeadingTokenizer extends BaseBlockTokenizer {
96
+ constructor(props = {}) {
97
+ var _a, _b;
98
+ super({
99
+ name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
100
+ priority: (_b = props.priority) !== null && _b !== void 0 ? _b : TokenizerPriority.ATOMIC,
101
+ });
102
+ this.match = match;
103
+ this.parse = parse;
91
104
  }
92
105
  }
93
106
 
94
- export { HeadingTokenizer, uniqueName as HeadingTokenizerName, HeadingTokenizer as default };
107
+ export { HeadingTokenizer, uniqueName as HeadingTokenizerName, HeadingTokenizer as default, match as headingMatch, parse as headingParse };
@@ -1,5 +1,5 @@
1
- import { HeadingTokenizer } from './tokenizer';
2
- export { HeadingTokenizer } from './tokenizer';
1
+ export { match as headingMatch } from './match';
2
+ export { parse as headingParse } from './parse';
3
+ export { HeadingTokenizer, HeadingTokenizer as default } from './tokenizer';
3
4
  export { uniqueName as HeadingTokenizerName } from './types';
4
- export type { Token as HeadingToken, TokenizerProps as HeadingTokenizerProps, } from './types';
5
- export default HeadingTokenizer;
5
+ export type { IHookContext as IHeadingHookContext, IToken as IHeadingToken, ITokenizerProps as IHeadingTokenizerProps, } from './types';
@@ -0,0 +1,17 @@
1
+ import type { IMatchBlockHookCreator } from '@yozora/core-tokenizer';
2
+ import type { IHookContext, IToken, T } from './types';
3
+ /**
4
+ * An ATX heading consists of a string of characters, parsed as inline content,
5
+ * between an opening sequence of 1–6 unescaped '#' characters and an optional
6
+ * closing sequence of any number of unescaped '#' characters. The opening
7
+ * sequence of '#' characters must be followed by a space or by the end of line.
8
+ * The optional closing sequence of #s must be preceded by a space and may be
9
+ * followed by spaces only. The opening # character may be indented 0-3 spaces.
10
+ * The raw contents of the heading are stripped of leading and trailing spaces
11
+ * before being parsed as inline content. The heading level is equal to the
12
+ * number of '#' characters in the opening sequence.
13
+ *
14
+ * @see https://github.com/syntax-tree/mdast#heading
15
+ * @see https://github.github.com/gfm/#atx-heading
16
+ */
17
+ export declare const match: IMatchBlockHookCreator<T, IToken, IHookContext>;
@@ -0,0 +1,3 @@
1
+ import type { IParseBlockHookCreator } from '@yozora/core-tokenizer';
2
+ import type { IHookContext, INode, IToken, T } from './types';
3
+ export declare const parse: IParseBlockHookCreator<T, IToken, INode, IHookContext>;
@@ -1,38 +1,13 @@
1
- import type { ParseBlockPhaseApi, PhrasingContentLine, ResultOfEatAndInterruptPreviousSibling, ResultOfEatOpener, ResultOfParse, Tokenizer, TokenizerMatchBlockHook, TokenizerParseBlockHook, YastBlockToken } from '@yozora/core-tokenizer';
1
+ import type { IBlockTokenizer, IMatchBlockHookCreator, IParseBlockHookCreator } from '@yozora/core-tokenizer';
2
2
  import { BaseBlockTokenizer } from '@yozora/core-tokenizer';
3
- import type { Node, T, Token, TokenizerProps } from './types';
3
+ import type { IHookContext, INode, IToken, ITokenizerProps, T } from './types';
4
4
  /**
5
5
  * Lexical Analyzer for Heading.
6
- *
7
- * An ATX heading consists of a string of characters, parsed as inline content,
8
- * between an opening sequence of 1–6 unescaped '#' characters and an optional
9
- * closing sequence of any number of unescaped '#' characters. The opening
10
- * sequence of '#' characters must be followed by a space or by the end of line.
11
- * The optional closing sequence of #s must be preceded by a space and may be
12
- * followed by spaces only. The opening # character may be indented 0-3 spaces.
13
- * The raw contents of the heading are stripped of leading and trailing spaces
14
- * before being parsed as inline content. The heading level is equal to the
15
- * number of '#' characters in the opening sequence.
16
- *
17
6
  * @see https://github.com/syntax-tree/mdast#heading
18
7
  * @see https://github.github.com/gfm/#atx-heading
19
8
  */
20
- export declare class HeadingTokenizer extends BaseBlockTokenizer implements Tokenizer, TokenizerMatchBlockHook<T, Token>, TokenizerParseBlockHook<T, Token, Node> {
21
- readonly isContainingBlock = false;
22
- constructor(props?: TokenizerProps);
23
- /**
24
- * @override
25
- * @see TokenizerMatchBlockHook
26
- */
27
- eatOpener(line: Readonly<PhrasingContentLine>): ResultOfEatOpener<T, Token>;
28
- /**
29
- * @override
30
- * @see TokenizerMatchBlockHook
31
- */
32
- eatAndInterruptPreviousSibling(line: Readonly<PhrasingContentLine>, prevSiblingToken: Readonly<YastBlockToken>): ResultOfEatAndInterruptPreviousSibling<T, Token>;
33
- /**
34
- * @override
35
- * @see TokenizerParseBlockHook
36
- */
37
- parseBlock(token: Readonly<Token>, children: undefined, api: Readonly<ParseBlockPhaseApi>): ResultOfParse<T, Node>;
9
+ export declare class HeadingTokenizer extends BaseBlockTokenizer<T, IToken, INode, IHookContext> implements IBlockTokenizer<T, IToken, INode, IHookContext> {
10
+ constructor(props?: ITokenizerProps);
11
+ readonly match: IMatchBlockHookCreator<T, IToken, IHookContext>;
12
+ readonly parse: IParseBlockHookCreator<T, IToken, INode, IHookContext>;
38
13
  }
@@ -1,9 +1,9 @@
1
- import type { Heading, HeadingType } from '@yozora/ast';
2
- import type { BaseBlockTokenizerProps, PartialYastBlockToken, PhrasingContentLine } from '@yozora/core-tokenizer';
1
+ import type { HeadingType, IHeading } from '@yozora/ast';
2
+ import type { IBaseBlockTokenizerProps, IPartialYastBlockToken, IPhrasingContentLine, ITokenizer } from '@yozora/core-tokenizer';
3
3
  export declare type T = HeadingType;
4
- export declare type Node = Heading;
4
+ export declare type INode = IHeading;
5
5
  export declare const uniqueName = "@yozora/tokenizer-heading";
6
- export interface Token extends PartialYastBlockToken<T> {
6
+ export interface IToken extends IPartialYastBlockToken<T> {
7
7
  /**
8
8
  * Level of heading
9
9
  */
@@ -11,6 +11,7 @@ export interface Token extends PartialYastBlockToken<T> {
11
11
  /**
12
12
  * Contents
13
13
  */
14
- line: Readonly<PhrasingContentLine>;
14
+ line: Readonly<IPhrasingContentLine>;
15
15
  }
16
- export declare type TokenizerProps = Partial<BaseBlockTokenizerProps>;
16
+ export declare type IHookContext = ITokenizer;
17
+ export declare type ITokenizerProps = Partial<IBaseBlockTokenizerProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-heading",
3
- "version": "1.2.0",
3
+ "version": "2.0.0-alpha.0",
4
4
  "author": {
5
5
  "name": "guanghechen",
6
6
  "url": "https://github.com/guanghechen/"
@@ -35,9 +35,9 @@
35
35
  "test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
36
36
  },
37
37
  "dependencies": {
38
- "@yozora/ast": "^1.2.0",
39
- "@yozora/character": "^1.2.0",
40
- "@yozora/core-tokenizer": "^1.2.0"
38
+ "@yozora/ast": "^2.0.0-alpha.0",
39
+ "@yozora/character": "^2.0.0-alpha.0",
40
+ "@yozora/core-tokenizer": "^2.0.0-alpha.0"
41
41
  },
42
- "gitHead": "86da40e50d2fe9acace68695288e15e012e6cd0d"
42
+ "gitHead": "0171501339c49ffd02ed16a63447fa20a47a29a7"
43
43
  }