@yozora/tokenizer-inline-math 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
@@ -82,14 +82,14 @@ so you can use `YozoraParser` directly.
82
82
  registered in *YastParser* as a plugin-in before it can be used.
83
83
 
84
84
  ```typescript {4,9}
85
- import { DefaultYastParser } from '@yozora/core-parser'
85
+ import { DefaultParser } from '@yozora/core-parser'
86
86
  import ParagraphTokenizer from '@yozora/tokenizer-paragraph'
87
87
  import TextTokenizer from '@yozora/tokenizer-text'
88
88
  import InlineMathTokenizer from '@yozora/tokenizer-inline-math'
89
89
 
90
- const parser = new DefaultYastParser()
91
- .useBlockFallbackTokenizer(new ParagraphTokenizer())
92
- .useInlineFallbackTokenizer(new TextTokenizer())
90
+ const parser = new DefaultParser()
91
+ .useFallbackTokenizer(new ParagraphTokenizer())
92
+ .useFallbackTokenizer(new TextTokenizer())
93
93
  .useTokenizer(new InlineMathTokenizer())
94
94
 
95
95
  // parse source markdown content
@@ -228,7 +228,6 @@ Name | Type | Required | Default
228
228
  [@yozora/tokenizer-link]: https://github.com/yozorajs/yozora/tree/main/tokenizers/link#readme
229
229
  [@yozora/tokenizer-link-reference]: https://github.com/yozorajs/yozora/tree/main/tokenizers/link-reference#readme
230
230
  [@yozora/tokenizer-list]: https://github.com/yozorajs/yozora/tree/main/tokenizers/list#readme
231
- [@yozora/tokenizer-list-item]: https://github.com/yozorajs/yozora/tree/main/tokenizers/list-item#readme
232
231
  [@yozora/tokenizer-math]: https://github.com/yozorajs/yozora/tree/main/tokenizers/math#readme
233
232
  [@yozora/tokenizer-paragraph]: https://github.com/yozorajs/yozora/tree/main/tokenizers/paragraph#readme
234
233
  [@yozora/tokenizer-setext-heading]: https://github.com/yozorajs/yozora/tree/main/tokenizers/setext-heading#readme
@@ -288,7 +287,6 @@ Name | Type | Required | Default
288
287
  [doc-@yozora/tokenizer-definition]: https://yozora.guanghechen.com/docs/package/tokenizer-definition
289
288
  [doc-@yozora/tokenizer-link-reference]: https://yozora.guanghechen.com/docs/package/tokenizer-link-reference
290
289
  [doc-@yozora/tokenizer-list]: https://yozora.guanghechen.com/docs/package/tokenizer-list
291
- [doc-@yozora/tokenizer-list-item]: https://yozora.guanghechen.com/docs/package/tokenizer-list-item
292
290
  [doc-@yozora/tokenizer-math]: https://yozora.guanghechen.com/docs/package/tokenizer-math
293
291
  [doc-@yozora/tokenizer-paragraph]: https://yozora.guanghechen.com/docs/package/tokenizer-paragraph
294
292
  [doc-@yozora/tokenizer-setext-heading]: https://yozora.guanghechen.com/docs/package/tokenizer-setext-heading
package/lib/cjs/index.js CHANGED
@@ -6,18 +6,10 @@ 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-inline-math';
10
-
11
- class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
12
- constructor(props = {}) {
13
- var _a, _b, _c;
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.backtickRequired = (_c = props.backtickRequired) !== null && _c !== void 0 ? _c : true;
19
- }
20
- *findDelimiter(api) {
9
+ const match = function (api) {
10
+ const { backtickRequired } = this;
11
+ return { findDelimiter, processSingleDelimiter };
12
+ function* findDelimiter() {
21
13
  const nodePoints = api.getNodePoints();
22
14
  const blockStartIndex = api.getBlockStartIndex();
23
15
  const blockEndIndex = api.getBlockEndIndex();
@@ -31,8 +23,7 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
31
23
  case character.AsciiCodePoint.BACKTICK: {
32
24
  const _startIndex = i;
33
25
  i = coreTokenizer.eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, character.AsciiCodePoint.BACKTICK);
34
- if (i >= blockEndIndex ||
35
- nodePoints[i].codePoint !== character.AsciiCodePoint.DOLLAR_SIGN) {
26
+ if (i >= blockEndIndex || nodePoints[i].codePoint !== character.AsciiCodePoint.DOLLAR_SIGN) {
36
27
  break;
37
28
  }
38
29
  const delimiter = {
@@ -46,13 +37,12 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
46
37
  case character.AsciiCodePoint.DOLLAR_SIGN: {
47
38
  const _startIndex = i;
48
39
  i = coreTokenizer.eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, character.AsciiCodePoint.BACKTICK);
49
- if (i < blockEndIndex &&
50
- nodePoints[i].codePoint === character.AsciiCodePoint.DOLLAR_SIGN) {
40
+ if (i < blockEndIndex && nodePoints[i].codePoint === character.AsciiCodePoint.DOLLAR_SIGN) {
51
41
  break;
52
42
  }
53
43
  const thickness = i - _startIndex;
54
44
  if (thickness <= 1) {
55
- if (this.backtickRequired)
45
+ if (backtickRequired)
56
46
  break;
57
47
  const delimiter = {
58
48
  type: 'both',
@@ -116,10 +106,7 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
116
106
  };
117
107
  }
118
108
  }
119
- _findDelimiter() {
120
- return null;
121
- }
122
- processSingleDelimiter(delimiter) {
109
+ function processSingleDelimiter(delimiter) {
123
110
  const token = {
124
111
  nodeType: ast.InlineMathType,
125
112
  startIndex: delimiter.startIndex,
@@ -128,33 +115,55 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
128
115
  };
129
116
  return [token];
130
117
  }
131
- parseInline(token, children, api) {
132
- const nodePoints = api.getNodePoints();
133
- let startIndex = token.startIndex + token.thickness;
134
- let endIndex = token.endIndex - token.thickness;
135
- let isAllSpace = true;
136
- for (let i = startIndex; i < endIndex; ++i) {
137
- if (character.isSpaceLike(nodePoints[i].codePoint))
138
- continue;
139
- isAllSpace = false;
140
- break;
141
- }
142
- if (!isAllSpace && startIndex + 2 < endIndex) {
143
- const firstCharacter = nodePoints[startIndex].codePoint;
144
- const lastCharacter = nodePoints[endIndex - 1].codePoint;
145
- if (character.isSpaceLike(firstCharacter) && character.isSpaceLike(lastCharacter)) {
146
- startIndex += 1;
147
- endIndex -= 1;
118
+ };
119
+
120
+ const parse = function (api) {
121
+ return {
122
+ parse: tokens => tokens.map(token => {
123
+ const nodePoints = api.getNodePoints();
124
+ let startIndex = token.startIndex + token.thickness;
125
+ let endIndex = token.endIndex - token.thickness;
126
+ let isAllSpace = true;
127
+ for (let i = startIndex; i < endIndex; ++i) {
128
+ if (character.isSpaceLike(nodePoints[i].codePoint))
129
+ continue;
130
+ isAllSpace = false;
131
+ break;
148
132
  }
149
- }
150
- const result = {
151
- type: ast.InlineMathType,
152
- value: character.calcStringFromNodePoints(nodePoints, startIndex, endIndex).replace(/\n/, ' '),
153
- };
154
- return result;
133
+ if (!isAllSpace && startIndex + 2 < endIndex) {
134
+ const firstCharacter = nodePoints[startIndex].codePoint;
135
+ const lastCharacter = nodePoints[endIndex - 1].codePoint;
136
+ if (character.isSpaceLike(firstCharacter) && character.isSpaceLike(lastCharacter)) {
137
+ startIndex += 1;
138
+ endIndex -= 1;
139
+ }
140
+ }
141
+ const value = character.calcStringFromNodePoints(nodePoints, startIndex, endIndex).replace(/\n/, ' ');
142
+ const node = api.shouldReservePosition
143
+ ? { type: ast.InlineMathType, position: api.calcPosition(token), value }
144
+ : { type: ast.InlineMathType, value };
145
+ return node;
146
+ }),
147
+ };
148
+ };
149
+
150
+ const uniqueName = '@yozora/tokenizer-inline-math';
151
+
152
+ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
153
+ constructor(props = {}) {
154
+ var _a, _b, _c;
155
+ super({
156
+ name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
157
+ priority: (_b = props.priority) !== null && _b !== void 0 ? _b : coreTokenizer.TokenizerPriority.ATOMIC,
158
+ });
159
+ this.match = match;
160
+ this.parse = parse;
161
+ this.backtickRequired = (_c = props.backtickRequired) !== null && _c !== void 0 ? _c : true;
155
162
  }
156
163
  }
157
164
 
158
165
  exports.InlineMathTokenizer = InlineMathTokenizer;
159
166
  exports.InlineMathTokenizerName = uniqueName;
160
- exports['default'] = InlineMathTokenizer;
167
+ exports["default"] = InlineMathTokenizer;
168
+ exports.inlineMathMatch = match;
169
+ exports.inlineMathParse = parse;
package/lib/esm/index.js CHANGED
@@ -1,19 +1,11 @@
1
1
  import { InlineMathType } from '@yozora/ast';
2
2
  import { AsciiCodePoint, isSpaceLike, calcStringFromNodePoints } from '@yozora/character';
3
- import { BaseInlineTokenizer, TokenizerPriority, eatOptionalCharacters } from '@yozora/core-tokenizer';
3
+ import { eatOptionalCharacters, BaseInlineTokenizer, TokenizerPriority } from '@yozora/core-tokenizer';
4
4
 
5
- const uniqueName = '@yozora/tokenizer-inline-math';
6
-
7
- class InlineMathTokenizer extends BaseInlineTokenizer {
8
- constructor(props = {}) {
9
- var _a, _b, _c;
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.backtickRequired = (_c = props.backtickRequired) !== null && _c !== void 0 ? _c : true;
15
- }
16
- *findDelimiter(api) {
5
+ const match = function (api) {
6
+ const { backtickRequired } = this;
7
+ return { findDelimiter, processSingleDelimiter };
8
+ function* findDelimiter() {
17
9
  const nodePoints = api.getNodePoints();
18
10
  const blockStartIndex = api.getBlockStartIndex();
19
11
  const blockEndIndex = api.getBlockEndIndex();
@@ -27,8 +19,7 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
27
19
  case AsciiCodePoint.BACKTICK: {
28
20
  const _startIndex = i;
29
21
  i = eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, AsciiCodePoint.BACKTICK);
30
- if (i >= blockEndIndex ||
31
- nodePoints[i].codePoint !== AsciiCodePoint.DOLLAR_SIGN) {
22
+ if (i >= blockEndIndex || nodePoints[i].codePoint !== AsciiCodePoint.DOLLAR_SIGN) {
32
23
  break;
33
24
  }
34
25
  const delimiter = {
@@ -42,13 +33,12 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
42
33
  case AsciiCodePoint.DOLLAR_SIGN: {
43
34
  const _startIndex = i;
44
35
  i = eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, AsciiCodePoint.BACKTICK);
45
- if (i < blockEndIndex &&
46
- nodePoints[i].codePoint === AsciiCodePoint.DOLLAR_SIGN) {
36
+ if (i < blockEndIndex && nodePoints[i].codePoint === AsciiCodePoint.DOLLAR_SIGN) {
47
37
  break;
48
38
  }
49
39
  const thickness = i - _startIndex;
50
40
  if (thickness <= 1) {
51
- if (this.backtickRequired)
41
+ if (backtickRequired)
52
42
  break;
53
43
  const delimiter = {
54
44
  type: 'both',
@@ -112,10 +102,7 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
112
102
  };
113
103
  }
114
104
  }
115
- _findDelimiter() {
116
- return null;
117
- }
118
- processSingleDelimiter(delimiter) {
105
+ function processSingleDelimiter(delimiter) {
119
106
  const token = {
120
107
  nodeType: InlineMathType,
121
108
  startIndex: delimiter.startIndex,
@@ -124,31 +111,51 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
124
111
  };
125
112
  return [token];
126
113
  }
127
- parseInline(token, children, api) {
128
- const nodePoints = api.getNodePoints();
129
- let startIndex = token.startIndex + token.thickness;
130
- let endIndex = token.endIndex - token.thickness;
131
- let isAllSpace = true;
132
- for (let i = startIndex; i < endIndex; ++i) {
133
- if (isSpaceLike(nodePoints[i].codePoint))
134
- continue;
135
- isAllSpace = false;
136
- break;
137
- }
138
- if (!isAllSpace && startIndex + 2 < endIndex) {
139
- const firstCharacter = nodePoints[startIndex].codePoint;
140
- const lastCharacter = nodePoints[endIndex - 1].codePoint;
141
- if (isSpaceLike(firstCharacter) && isSpaceLike(lastCharacter)) {
142
- startIndex += 1;
143
- endIndex -= 1;
114
+ };
115
+
116
+ const parse = function (api) {
117
+ return {
118
+ parse: tokens => tokens.map(token => {
119
+ const nodePoints = api.getNodePoints();
120
+ let startIndex = token.startIndex + token.thickness;
121
+ let endIndex = token.endIndex - token.thickness;
122
+ let isAllSpace = true;
123
+ for (let i = startIndex; i < endIndex; ++i) {
124
+ if (isSpaceLike(nodePoints[i].codePoint))
125
+ continue;
126
+ isAllSpace = false;
127
+ break;
144
128
  }
145
- }
146
- const result = {
147
- type: InlineMathType,
148
- value: calcStringFromNodePoints(nodePoints, startIndex, endIndex).replace(/\n/, ' '),
149
- };
150
- return result;
129
+ if (!isAllSpace && startIndex + 2 < endIndex) {
130
+ const firstCharacter = nodePoints[startIndex].codePoint;
131
+ const lastCharacter = nodePoints[endIndex - 1].codePoint;
132
+ if (isSpaceLike(firstCharacter) && isSpaceLike(lastCharacter)) {
133
+ startIndex += 1;
134
+ endIndex -= 1;
135
+ }
136
+ }
137
+ const value = calcStringFromNodePoints(nodePoints, startIndex, endIndex).replace(/\n/, ' ');
138
+ const node = api.shouldReservePosition
139
+ ? { type: InlineMathType, position: api.calcPosition(token), value }
140
+ : { type: InlineMathType, value };
141
+ return node;
142
+ }),
143
+ };
144
+ };
145
+
146
+ const uniqueName = '@yozora/tokenizer-inline-math';
147
+
148
+ class InlineMathTokenizer extends BaseInlineTokenizer {
149
+ constructor(props = {}) {
150
+ var _a, _b, _c;
151
+ super({
152
+ name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
153
+ priority: (_b = props.priority) !== null && _b !== void 0 ? _b : TokenizerPriority.ATOMIC,
154
+ });
155
+ this.match = match;
156
+ this.parse = parse;
157
+ this.backtickRequired = (_c = props.backtickRequired) !== null && _c !== void 0 ? _c : true;
151
158
  }
152
159
  }
153
160
 
154
- export { InlineMathTokenizer, uniqueName as InlineMathTokenizerName, InlineMathTokenizer as default };
161
+ export { InlineMathTokenizer, uniqueName as InlineMathTokenizerName, InlineMathTokenizer as default, match as inlineMathMatch, parse as inlineMathParse };
@@ -1,5 +1,5 @@
1
- import { InlineMathTokenizer } from './tokenizer';
2
- export { InlineMathTokenizer } from './tokenizer';
1
+ export { match as inlineMathMatch } from './match';
2
+ export { parse as inlineMathParse } from './parse';
3
+ export { InlineMathTokenizer, InlineMathTokenizer as default } from './tokenizer';
3
4
  export { uniqueName as InlineMathTokenizerName } from './types';
4
- export type { Token as InlineMathToken, TokenizerProps as InlineMathTokenizerProps, } from './types';
5
- export default InlineMathTokenizer;
5
+ export type { IThis as IInlineMathHookContext, IToken as IInlineMathToken, ITokenizerProps as IInlineMathTokenizerProps, } from './types';
@@ -0,0 +1,3 @@
1
+ import type { IMatchInlineHookCreator } from '@yozora/core-tokenizer';
2
+ import type { IDelimiter, IThis, IToken, T } from './types';
3
+ export declare const match: IMatchInlineHookCreator<T, IDelimiter, IToken, IThis>;
@@ -0,0 +1,3 @@
1
+ import type { IParseInlineHookCreator } from '@yozora/core-tokenizer';
2
+ import type { INode, IThis, IToken, T } from './types';
3
+ export declare const parse: IParseInlineHookCreator<T, IToken, INode, IThis>;
@@ -1,31 +1,12 @@
1
- import type { YastNode } from '@yozora/ast';
2
- import type { MatchInlinePhaseApi, ParseInlinePhaseApi, ResultOfFindDelimiters, ResultOfProcessSingleDelimiter, Tokenizer, TokenizerMatchInlineHook, TokenizerParseInlineHook } from '@yozora/core-tokenizer';
1
+ import type { IInlineTokenizer, IMatchInlineHookCreator, IParseInlineHookCreator } from '@yozora/core-tokenizer';
3
2
  import { BaseInlineTokenizer } from '@yozora/core-tokenizer';
4
- import type { Delimiter, Node, T, Token, TokenizerProps } from './types';
3
+ import type { IDelimiter, INode, IThis, IToken, ITokenizerProps, T } from './types';
5
4
  /**
6
5
  * Lexical Analyzer for inlineMath.
7
6
  */
8
- export declare class InlineMathTokenizer extends BaseInlineTokenizer<Delimiter> implements Tokenizer, TokenizerMatchInlineHook<T, Delimiter, Token>, TokenizerParseInlineHook<T, Token, Node> {
7
+ export declare class InlineMathTokenizer extends BaseInlineTokenizer<T, IDelimiter, IToken, INode, IThis> implements IInlineTokenizer<T, IDelimiter, IToken, INode, IThis> {
9
8
  readonly backtickRequired: boolean;
10
- constructor(props?: TokenizerProps);
11
- /**
12
- * @override
13
- * @see BaseInlineTokenizer
14
- */
15
- findDelimiter(api: Readonly<MatchInlinePhaseApi>): ResultOfFindDelimiters<Delimiter>;
16
- /**
17
- * @override
18
- * @see BaseInlineTokenizer
19
- */
20
- protected _findDelimiter(): null;
21
- /**
22
- * @override
23
- * @see TokenizerMatchInlineHook
24
- */
25
- processSingleDelimiter(delimiter: Delimiter): ResultOfProcessSingleDelimiter<T, Token>;
26
- /**
27
- * @override
28
- * @see TokenizerParseInlineHook
29
- */
30
- parseInline(token: Token, children: YastNode[], api: Readonly<ParseInlinePhaseApi>): Node;
9
+ constructor(props?: ITokenizerProps);
10
+ readonly match: IMatchInlineHookCreator<T, IDelimiter, IToken, IThis>;
11
+ readonly parse: IParseInlineHookCreator<T, IToken, INode, IThis>;
31
12
  }
@@ -1,25 +1,31 @@
1
1
  import type { InlineMath, InlineMathType } from '@yozora/ast';
2
- import type { BaseInlineTokenizerProps, PartialYastInlineToken, YastTokenDelimiter } from '@yozora/core-tokenizer';
2
+ import type { IBaseInlineTokenizerProps, IPartialYastInlineToken, ITokenizer, IYastTokenDelimiter } from '@yozora/core-tokenizer';
3
3
  export declare type T = InlineMathType;
4
- export declare type Node = InlineMath;
4
+ export declare type INode = InlineMath;
5
5
  export declare const uniqueName = "@yozora/tokenizer-inline-math";
6
- export interface Token extends PartialYastInlineToken<T> {
6
+ export interface IToken extends IPartialYastInlineToken<T> {
7
7
  /**
8
8
  * Thickness of the InlineMathDelimiter
9
9
  */
10
10
  thickness: number;
11
11
  }
12
12
  /**
13
- * Delimiter of InlineMathToken.
13
+ * IDelimiter of InlineMathToken.
14
14
  */
15
- export interface Delimiter extends YastTokenDelimiter {
15
+ export interface IDelimiter extends IYastTokenDelimiter {
16
16
  type: 'full';
17
17
  /**
18
18
  * Thickness of the InlineMathDelimiter
19
19
  */
20
20
  thickness: number;
21
21
  }
22
- export interface TokenizerProps extends Partial<BaseInlineTokenizerProps> {
22
+ export interface IThis extends ITokenizer {
23
+ /**
24
+ * Whether if the backtick mark wrapping necessary.
25
+ */
26
+ readonly backtickRequired: boolean;
27
+ }
28
+ export interface ITokenizerProps extends Partial<IBaseInlineTokenizerProps> {
23
29
  /**
24
30
  * Whether if the backtick mark wrapping necessary.
25
31
  * @default true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-inline-math",
3
- "version": "1.3.0",
3
+ "version": "2.0.0-alpha.3",
4
4
  "description": "Tokenizer for processing inline math (formulas)",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -45,12 +45,12 @@
45
45
  "test": "cross-env TS_NODE_FILES=true jest --config ../../jest.config.js --rootDir ."
46
46
  },
47
47
  "dependencies": {
48
- "@yozora/ast": "^1.3.0",
49
- "@yozora/character": "^1.3.0",
50
- "@yozora/core-tokenizer": "^1.3.0"
48
+ "@yozora/ast": "^2.0.0-alpha.3",
49
+ "@yozora/character": "^2.0.0-alpha.3",
50
+ "@yozora/core-tokenizer": "^2.0.0-alpha.3"
51
51
  },
52
52
  "devDependencies": {
53
- "@yozora/tokenizer-inline-code": "^1.3.0"
53
+ "@yozora/tokenizer-inline-code": "^2.0.0-alpha.3"
54
54
  },
55
- "gitHead": "18c9b167004ad97718b2f94f25139f80598cbf7a"
55
+ "gitHead": "9f274fc7487a8c1dd213405d92508f9a7621f730"
56
56
  }