@yozora/tokenizer-indented-code 1.3.0 → 2.0.0-alpha.3

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/README.md CHANGED
@@ -84,14 +84,14 @@ so you can use `YozoraParser` / `GfmExParser` / `GfmParser` directly.
84
84
  registered in *YastParser* as a plugin-in before it can be used.
85
85
 
86
86
  ```typescript {4,9}
87
- import { DefaultYastParser } from '@yozora/core-parser'
87
+ import { DefaultParser } from '@yozora/core-parser'
88
88
  import ParagraphTokenizer from '@yozora/tokenizer-paragraph'
89
89
  import TextTokenizer from '@yozora/tokenizer-text'
90
90
  import IndentedCodeTokenizer from '@yozora/tokenizer-indented-code'
91
91
 
92
- const parser = new DefaultYastParser()
93
- .useBlockFallbackTokenizer(new ParagraphTokenizer())
94
- .useInlineFallbackTokenizer(new TextTokenizer())
92
+ const parser = new DefaultParser()
93
+ .useFallbackTokenizer(new ParagraphTokenizer())
94
+ .useFallbackTokenizer(new TextTokenizer())
95
95
  .useTokenizer(new IndentedCodeTokenizer())
96
96
 
97
97
  // parse source markdown content
@@ -231,7 +231,6 @@ Name | Type | Required | Default
231
231
  [@yozora/tokenizer-link]: https://github.com/yozorajs/yozora/tree/main/tokenizers/link#readme
232
232
  [@yozora/tokenizer-link-reference]: https://github.com/yozorajs/yozora/tree/main/tokenizers/link-reference#readme
233
233
  [@yozora/tokenizer-list]: https://github.com/yozorajs/yozora/tree/main/tokenizers/list#readme
234
- [@yozora/tokenizer-list-item]: https://github.com/yozorajs/yozora/tree/main/tokenizers/list-item#readme
235
234
  [@yozora/tokenizer-math]: https://github.com/yozorajs/yozora/tree/main/tokenizers/math#readme
236
235
  [@yozora/tokenizer-paragraph]: https://github.com/yozorajs/yozora/tree/main/tokenizers/paragraph#readme
237
236
  [@yozora/tokenizer-setext-heading]: https://github.com/yozorajs/yozora/tree/main/tokenizers/setext-heading#readme
@@ -291,7 +290,6 @@ Name | Type | Required | Default
291
290
  [doc-@yozora/tokenizer-definition]: https://yozora.guanghechen.com/docs/package/tokenizer-definition
292
291
  [doc-@yozora/tokenizer-link-reference]: https://yozora.guanghechen.com/docs/package/tokenizer-link-reference
293
292
  [doc-@yozora/tokenizer-list]: https://yozora.guanghechen.com/docs/package/tokenizer-list
294
- [doc-@yozora/tokenizer-list-item]: https://yozora.guanghechen.com/docs/package/tokenizer-list-item
295
293
  [doc-@yozora/tokenizer-math]: https://yozora.guanghechen.com/docs/package/tokenizer-math
296
294
  [doc-@yozora/tokenizer-paragraph]: https://yozora.guanghechen.com/docs/package/tokenizer-paragraph
297
295
  [doc-@yozora/tokenizer-setext-heading]: https://yozora.guanghechen.com/docs/package/tokenizer-setext-heading
package/lib/cjs/index.js CHANGED
@@ -6,18 +6,13 @@ 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-indented-code';
10
-
11
- class IndentedCodeTokenizer 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
+ eatContinuationText,
14
+ };
15
+ function eatOpener(line) {
21
16
  if (line.countOfPrecedeSpaces < 4)
22
17
  return null;
23
18
  const { nodePoints, startIndex, firstNonWhitespaceIndex, endIndex } = line;
@@ -50,8 +45,8 @@ class IndentedCodeTokenizer extends coreTokenizer.BaseBlockTokenizer {
50
45
  };
51
46
  return { token, nextIndex };
52
47
  }
53
- eatContinuationText(line, token) {
54
- const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex, countOfPrecedeSpaces, } = line;
48
+ function eatContinuationText(line, token) {
49
+ const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex, countOfPrecedeSpaces } = line;
55
50
  if (countOfPrecedeSpaces < 4 && firstNonWhitespaceIndex < endIndex)
56
51
  return { status: 'notMatched' };
57
52
  const firstIndex = Math.min(endIndex - 1, startIndex + 4);
@@ -64,28 +59,49 @@ class IndentedCodeTokenizer extends coreTokenizer.BaseBlockTokenizer {
64
59
  });
65
60
  return { status: 'opening', nextIndex: endIndex };
66
61
  }
67
- parseBlock(token) {
68
- const { lines } = token;
69
- let startLineIndex = 0, endLineIndex = lines.length;
70
- for (; startLineIndex < endLineIndex; ++startLineIndex) {
71
- const line = lines[startLineIndex];
72
- if (line.firstNonWhitespaceIndex < line.endIndex)
73
- break;
74
- }
75
- for (; startLineIndex < endLineIndex; --endLineIndex) {
76
- const line = lines[endLineIndex - 1];
77
- if (line.firstNonWhitespaceIndex < line.endIndex)
78
- break;
79
- }
80
- const contents = coreTokenizer.mergeContentLinesFaithfully(lines, startLineIndex, endLineIndex);
81
- const node = {
82
- type: ast.CodeType,
83
- value: character.calcStringFromNodePoints(contents),
84
- };
85
- return node;
62
+ };
63
+
64
+ const parse = function (api) {
65
+ return {
66
+ parse: tokens => tokens.map(token => {
67
+ const { lines } = token;
68
+ let startLineIndex = 0, endLineIndex = lines.length;
69
+ for (; startLineIndex < endLineIndex; ++startLineIndex) {
70
+ const line = lines[startLineIndex];
71
+ if (line.firstNonWhitespaceIndex < line.endIndex)
72
+ break;
73
+ }
74
+ for (; startLineIndex < endLineIndex; --endLineIndex) {
75
+ const line = lines[endLineIndex - 1];
76
+ if (line.firstNonWhitespaceIndex < line.endIndex)
77
+ break;
78
+ }
79
+ const contents = coreTokenizer.mergeContentLinesFaithfully(lines, startLineIndex, endLineIndex);
80
+ const value = character.calcStringFromNodePoints(contents);
81
+ const node = api.shouldReservePosition
82
+ ? { type: ast.CodeType, position: token.position, value }
83
+ : { type: ast.CodeType, value };
84
+ return node;
85
+ }),
86
+ };
87
+ };
88
+
89
+ const uniqueName = '@yozora/tokenizer-indented-code';
90
+
91
+ class IndentedCodeTokenizer extends coreTokenizer.BaseBlockTokenizer {
92
+ constructor(props = {}) {
93
+ var _a, _b;
94
+ super({
95
+ name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
96
+ priority: (_b = props.priority) !== null && _b !== void 0 ? _b : coreTokenizer.TokenizerPriority.ATOMIC,
97
+ });
98
+ this.match = match;
99
+ this.parse = parse;
86
100
  }
87
101
  }
88
102
 
89
103
  exports.IndentedCodeTokenizer = IndentedCodeTokenizer;
90
104
  exports.IndentedCodeTokenizerName = uniqueName;
91
- exports['default'] = IndentedCodeTokenizer;
105
+ exports["default"] = IndentedCodeTokenizer;
106
+ exports.indentedCodeMatch = match;
107
+ exports.indentedCodeParse = parse;
package/lib/esm/index.js CHANGED
@@ -1,19 +1,14 @@
1
1
  import { CodeType } from '@yozora/ast';
2
2
  import { AsciiCodePoint, VirtualCodePoint, calcStringFromNodePoints } from '@yozora/character';
3
- import { BaseBlockTokenizer, TokenizerPriority, calcStartYastNodePoint, calcEndYastNodePoint, mergeContentLinesFaithfully } from '@yozora/core-tokenizer';
3
+ import { calcStartYastNodePoint, calcEndYastNodePoint, mergeContentLinesFaithfully, BaseBlockTokenizer, TokenizerPriority } from '@yozora/core-tokenizer';
4
4
 
5
- const uniqueName = '@yozora/tokenizer-indented-code';
6
-
7
- class IndentedCodeTokenizer 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
+ eatContinuationText,
10
+ };
11
+ function eatOpener(line) {
17
12
  if (line.countOfPrecedeSpaces < 4)
18
13
  return null;
19
14
  const { nodePoints, startIndex, firstNonWhitespaceIndex, endIndex } = line;
@@ -46,8 +41,8 @@ class IndentedCodeTokenizer extends BaseBlockTokenizer {
46
41
  };
47
42
  return { token, nextIndex };
48
43
  }
49
- eatContinuationText(line, token) {
50
- const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex, countOfPrecedeSpaces, } = line;
44
+ function eatContinuationText(line, token) {
45
+ const { nodePoints, startIndex, endIndex, firstNonWhitespaceIndex, countOfPrecedeSpaces } = line;
51
46
  if (countOfPrecedeSpaces < 4 && firstNonWhitespaceIndex < endIndex)
52
47
  return { status: 'notMatched' };
53
48
  const firstIndex = Math.min(endIndex - 1, startIndex + 4);
@@ -60,26 +55,45 @@ class IndentedCodeTokenizer extends BaseBlockTokenizer {
60
55
  });
61
56
  return { status: 'opening', nextIndex: endIndex };
62
57
  }
63
- parseBlock(token) {
64
- const { lines } = token;
65
- let startLineIndex = 0, endLineIndex = lines.length;
66
- for (; startLineIndex < endLineIndex; ++startLineIndex) {
67
- const line = lines[startLineIndex];
68
- if (line.firstNonWhitespaceIndex < line.endIndex)
69
- break;
70
- }
71
- for (; startLineIndex < endLineIndex; --endLineIndex) {
72
- const line = lines[endLineIndex - 1];
73
- if (line.firstNonWhitespaceIndex < line.endIndex)
74
- break;
75
- }
76
- const contents = mergeContentLinesFaithfully(lines, startLineIndex, endLineIndex);
77
- const node = {
78
- type: CodeType,
79
- value: calcStringFromNodePoints(contents),
80
- };
81
- return node;
58
+ };
59
+
60
+ const parse = function (api) {
61
+ return {
62
+ parse: tokens => tokens.map(token => {
63
+ const { lines } = token;
64
+ let startLineIndex = 0, endLineIndex = lines.length;
65
+ for (; startLineIndex < endLineIndex; ++startLineIndex) {
66
+ const line = lines[startLineIndex];
67
+ if (line.firstNonWhitespaceIndex < line.endIndex)
68
+ break;
69
+ }
70
+ for (; startLineIndex < endLineIndex; --endLineIndex) {
71
+ const line = lines[endLineIndex - 1];
72
+ if (line.firstNonWhitespaceIndex < line.endIndex)
73
+ break;
74
+ }
75
+ const contents = mergeContentLinesFaithfully(lines, startLineIndex, endLineIndex);
76
+ const value = calcStringFromNodePoints(contents);
77
+ const node = api.shouldReservePosition
78
+ ? { type: CodeType, position: token.position, value }
79
+ : { type: CodeType, value };
80
+ return node;
81
+ }),
82
+ };
83
+ };
84
+
85
+ const uniqueName = '@yozora/tokenizer-indented-code';
86
+
87
+ class IndentedCodeTokenizer extends BaseBlockTokenizer {
88
+ constructor(props = {}) {
89
+ var _a, _b;
90
+ super({
91
+ name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
92
+ priority: (_b = props.priority) !== null && _b !== void 0 ? _b : TokenizerPriority.ATOMIC,
93
+ });
94
+ this.match = match;
95
+ this.parse = parse;
82
96
  }
83
97
  }
84
98
 
85
- export { IndentedCodeTokenizer, uniqueName as IndentedCodeTokenizerName, IndentedCodeTokenizer as default };
99
+ export { IndentedCodeTokenizer, uniqueName as IndentedCodeTokenizerName, IndentedCodeTokenizer as default, match as indentedCodeMatch, parse as indentedCodeParse };
@@ -1,5 +1,5 @@
1
- import { IndentedCodeTokenizer } from './tokenizer';
2
- export { IndentedCodeTokenizer } from './tokenizer';
1
+ export { match as indentedCodeMatch } from './match';
2
+ export { parse as indentedCodeParse } from './parse';
3
+ export { IndentedCodeTokenizer, IndentedCodeTokenizer as default } from './tokenizer';
3
4
  export { uniqueName as IndentedCodeTokenizerName } from './types';
4
- export type { Token as IndentedCodeToken, TokenizerProps as IndentedCodeTokenizerProps, } from './types';
5
- export default IndentedCodeTokenizer;
5
+ export type { IThis as IIndentedCodeHookContext, IToken as IIndentedCodeToken, ITokenizerProps as IIndentedCodeTokenizerProps, } from './types';
@@ -0,0 +1,12 @@
1
+ import type { IMatchBlockHookCreator } from '@yozora/core-tokenizer';
2
+ import type { IThis, IToken, T } from './types';
3
+ /**
4
+ * An indented code block is composed of one or more indented chunks
5
+ * separated by blank lines. An indented chunk is a sequence of non-blank
6
+ * lines, each indented four or more spaces. The contents of the code block
7
+ * are the literal contents of the lines, including trailing line endings,
8
+ * minus four spaces of indentation.
9
+ *
10
+ * @see https://github.github.com/gfm/#indented-code-block
11
+ */
12
+ export declare const match: IMatchBlockHookCreator<T, IToken, IThis>;
@@ -0,0 +1,3 @@
1
+ import type { IParseBlockHookCreator } from '@yozora/core-tokenizer';
2
+ import type { INode, IThis, IToken, T } from './types';
3
+ export declare const parse: IParseBlockHookCreator<T, IToken, INode, IThis>;
@@ -1,33 +1,12 @@
1
- import type { PhrasingContentLine, ResultOfEatContinuationText, ResultOfEatOpener, ResultOfParse, Tokenizer, TokenizerMatchBlockHook, TokenizerParseBlockHook } 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 { INode, IThis, IToken, ITokenizerProps, T } from './types';
4
4
  /**
5
5
  * Lexical Analyzer for IndentedCode.
6
- *
7
- * An indented code block is composed of one or more indented chunks
8
- * separated by blank lines. An indented chunk is a sequence of non-blank
9
- * lines, each indented four or more spaces. The contents of the code block
10
- * are the literal contents of the lines, including trailing line endings,
11
- * minus four spaces of indentation.
12
- *
13
6
  * @see https://github.github.com/gfm/#indented-code-block
14
7
  */
15
- export declare class IndentedCodeTokenizer extends BaseBlockTokenizer implements Tokenizer, TokenizerMatchBlockHook<T, Token>, TokenizerParseBlockHook<T, Token, Node> {
16
- readonly isContainingBlock = false;
17
- constructor(props?: TokenizerProps);
18
- /**
19
- * @override
20
- * @see TokenizerMatchBlockHook
21
- */
22
- eatOpener(line: Readonly<PhrasingContentLine>): ResultOfEatOpener<T, Token>;
23
- /**
24
- * @override
25
- * @see TokenizerMatchBlockHook
26
- */
27
- eatContinuationText(line: Readonly<PhrasingContentLine>, token: Token): ResultOfEatContinuationText;
28
- /**
29
- * @override
30
- * @see TokenizerParseBlockHook
31
- */
32
- parseBlock(token: Readonly<Token>): ResultOfParse<T, Node>;
8
+ export declare class IndentedCodeTokenizer extends BaseBlockTokenizer<T, IToken, INode, IThis> implements IBlockTokenizer<T, IToken, INode, IThis> {
9
+ constructor(props?: ITokenizerProps);
10
+ readonly match: IMatchBlockHookCreator<T, IToken, IThis>;
11
+ readonly parse: IParseBlockHookCreator<T, IToken, INode, IThis>;
33
12
  }
@@ -1,12 +1,13 @@
1
1
  import type { Code, CodeType } from '@yozora/ast';
2
- import type { BaseBlockTokenizerProps, PartialYastBlockToken, PhrasingContentLine } from '@yozora/core-tokenizer';
2
+ import type { IBaseBlockTokenizerProps, IPartialYastBlockToken, IPhrasingContentLine, ITokenizer } from '@yozora/core-tokenizer';
3
3
  export declare type T = CodeType;
4
- export declare type Node = Code;
4
+ export declare type INode = Code;
5
5
  export declare const uniqueName = "@yozora/tokenizer-indented-code";
6
- export interface Token extends PartialYastBlockToken<T> {
6
+ export interface IToken extends IPartialYastBlockToken<T> {
7
7
  /**
8
8
  * Lines to construct the contents of a paragraph.
9
9
  */
10
- lines: PhrasingContentLine[];
10
+ lines: IPhrasingContentLine[];
11
11
  }
12
- export declare type TokenizerProps = Partial<BaseBlockTokenizerProps>;
12
+ export declare type IThis = ITokenizer;
13
+ export declare type ITokenizerProps = Partial<IBaseBlockTokenizerProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-indented-code",
3
- "version": "1.3.0",
3
+ "version": "2.0.0-alpha.3",
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.3.0",
39
- "@yozora/character": "^1.3.0",
40
- "@yozora/core-tokenizer": "^1.3.0"
38
+ "@yozora/ast": "^2.0.0-alpha.3",
39
+ "@yozora/character": "^2.0.0-alpha.3",
40
+ "@yozora/core-tokenizer": "^2.0.0-alpha.3"
41
41
  },
42
- "gitHead": "18c9b167004ad97718b2f94f25139f80598cbf7a"
42
+ "gitHead": "9f274fc7487a8c1dd213405d92508f9a7621f730"
43
43
  }