@yozora/tokenizer-inline-math 1.2.2 → 2.0.0-alpha.2

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,11 @@ 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(nodePoints, api) {
9
+ const match = function (api) {
10
+ const { backtickRequired } = this;
11
+ return { findDelimiter, processSingleDelimiter };
12
+ function* findDelimiter() {
13
+ const nodePoints = api.getNodePoints();
21
14
  const blockStartIndex = api.getBlockStartIndex();
22
15
  const blockEndIndex = api.getBlockEndIndex();
23
16
  const potentialDelimiters = [];
@@ -30,8 +23,7 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
30
23
  case character.AsciiCodePoint.BACKTICK: {
31
24
  const _startIndex = i;
32
25
  i = coreTokenizer.eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, character.AsciiCodePoint.BACKTICK);
33
- if (i >= blockEndIndex ||
34
- nodePoints[i].codePoint !== character.AsciiCodePoint.DOLLAR_SIGN) {
26
+ if (i >= blockEndIndex || nodePoints[i].codePoint !== character.AsciiCodePoint.DOLLAR_SIGN) {
35
27
  break;
36
28
  }
37
29
  const delimiter = {
@@ -45,13 +37,12 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
45
37
  case character.AsciiCodePoint.DOLLAR_SIGN: {
46
38
  const _startIndex = i;
47
39
  i = coreTokenizer.eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, character.AsciiCodePoint.BACKTICK);
48
- if (i < blockEndIndex &&
49
- nodePoints[i].codePoint === character.AsciiCodePoint.DOLLAR_SIGN) {
40
+ if (i < blockEndIndex && nodePoints[i].codePoint === character.AsciiCodePoint.DOLLAR_SIGN) {
50
41
  break;
51
42
  }
52
43
  const thickness = i - _startIndex;
53
44
  if (thickness <= 1) {
54
- if (this.backtickRequired)
45
+ if (backtickRequired)
55
46
  break;
56
47
  const delimiter = {
57
48
  type: 'both',
@@ -115,10 +106,7 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
115
106
  };
116
107
  }
117
108
  }
118
- _findDelimiter() {
119
- return null;
120
- }
121
- processSingleDelimiter(delimiter) {
109
+ function processSingleDelimiter(delimiter) {
122
110
  const token = {
123
111
  nodeType: ast.InlineMathType,
124
112
  startIndex: delimiter.startIndex,
@@ -127,32 +115,55 @@ class InlineMathTokenizer extends coreTokenizer.BaseInlineTokenizer {
127
115
  };
128
116
  return [token];
129
117
  }
130
- processToken(token, children, nodePoints) {
131
- let startIndex = token.startIndex + token.thickness;
132
- let endIndex = token.endIndex - token.thickness;
133
- let isAllSpace = true;
134
- for (let i = startIndex; i < endIndex; ++i) {
135
- if (character.isSpaceLike(nodePoints[i].codePoint))
136
- continue;
137
- isAllSpace = false;
138
- break;
139
- }
140
- if (!isAllSpace && startIndex + 2 < endIndex) {
141
- const firstCharacter = nodePoints[startIndex].codePoint;
142
- const lastCharacter = nodePoints[endIndex - 1].codePoint;
143
- if (character.isSpaceLike(firstCharacter) && character.isSpaceLike(lastCharacter)) {
144
- startIndex += 1;
145
- 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;
146
132
  }
147
- }
148
- const result = {
149
- type: ast.InlineMathType,
150
- value: character.calcStringFromNodePoints(nodePoints, startIndex, endIndex).replace(/\n/, ' '),
151
- };
152
- 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;
153
162
  }
154
163
  }
155
164
 
156
165
  exports.InlineMathTokenizer = InlineMathTokenizer;
157
166
  exports.InlineMathTokenizerName = uniqueName;
158
- exports['default'] = InlineMathTokenizer;
167
+ exports["default"] = InlineMathTokenizer;
168
+ exports.inlineMathMatch = match;
169
+ exports.inlineMathParse = parse;
package/lib/esm/index.js CHANGED
@@ -1,19 +1,12 @@
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(nodePoints, api) {
5
+ const match = function (api) {
6
+ const { backtickRequired } = this;
7
+ return { findDelimiter, processSingleDelimiter };
8
+ function* findDelimiter() {
9
+ const nodePoints = api.getNodePoints();
17
10
  const blockStartIndex = api.getBlockStartIndex();
18
11
  const blockEndIndex = api.getBlockEndIndex();
19
12
  const potentialDelimiters = [];
@@ -26,8 +19,7 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
26
19
  case AsciiCodePoint.BACKTICK: {
27
20
  const _startIndex = i;
28
21
  i = eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, AsciiCodePoint.BACKTICK);
29
- if (i >= blockEndIndex ||
30
- nodePoints[i].codePoint !== AsciiCodePoint.DOLLAR_SIGN) {
22
+ if (i >= blockEndIndex || nodePoints[i].codePoint !== AsciiCodePoint.DOLLAR_SIGN) {
31
23
  break;
32
24
  }
33
25
  const delimiter = {
@@ -41,13 +33,12 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
41
33
  case AsciiCodePoint.DOLLAR_SIGN: {
42
34
  const _startIndex = i;
43
35
  i = eatOptionalCharacters(nodePoints, i + 1, blockEndIndex, AsciiCodePoint.BACKTICK);
44
- if (i < blockEndIndex &&
45
- nodePoints[i].codePoint === AsciiCodePoint.DOLLAR_SIGN) {
36
+ if (i < blockEndIndex && nodePoints[i].codePoint === AsciiCodePoint.DOLLAR_SIGN) {
46
37
  break;
47
38
  }
48
39
  const thickness = i - _startIndex;
49
40
  if (thickness <= 1) {
50
- if (this.backtickRequired)
41
+ if (backtickRequired)
51
42
  break;
52
43
  const delimiter = {
53
44
  type: 'both',
@@ -111,10 +102,7 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
111
102
  };
112
103
  }
113
104
  }
114
- _findDelimiter() {
115
- return null;
116
- }
117
- processSingleDelimiter(delimiter) {
105
+ function processSingleDelimiter(delimiter) {
118
106
  const token = {
119
107
  nodeType: InlineMathType,
120
108
  startIndex: delimiter.startIndex,
@@ -123,30 +111,51 @@ class InlineMathTokenizer extends BaseInlineTokenizer {
123
111
  };
124
112
  return [token];
125
113
  }
126
- processToken(token, children, nodePoints) {
127
- let startIndex = token.startIndex + token.thickness;
128
- let endIndex = token.endIndex - token.thickness;
129
- let isAllSpace = true;
130
- for (let i = startIndex; i < endIndex; ++i) {
131
- if (isSpaceLike(nodePoints[i].codePoint))
132
- continue;
133
- isAllSpace = false;
134
- break;
135
- }
136
- if (!isAllSpace && startIndex + 2 < endIndex) {
137
- const firstCharacter = nodePoints[startIndex].codePoint;
138
- const lastCharacter = nodePoints[endIndex - 1].codePoint;
139
- if (isSpaceLike(firstCharacter) && isSpaceLike(lastCharacter)) {
140
- startIndex += 1;
141
- 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;
142
128
  }
143
- }
144
- const result = {
145
- type: InlineMathType,
146
- value: calcStringFromNodePoints(nodePoints, startIndex, endIndex).replace(/\n/, ' '),
147
- };
148
- 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;
149
158
  }
150
159
  }
151
160
 
152
- 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,32 +1,12 @@
1
- import type { YastNode } from '@yozora/ast';
2
- import type { NodePoint } from '@yozora/character';
3
- import type { MatchInlinePhaseApi, ResultOfFindDelimiters, ResultOfProcessSingleDelimiter, Tokenizer, TokenizerMatchInlineHook, TokenizerParseInlineHook } from '@yozora/core-tokenizer';
1
+ import type { IInlineTokenizer, IMatchInlineHookCreator, IParseInlineHookCreator } from '@yozora/core-tokenizer';
4
2
  import { BaseInlineTokenizer } from '@yozora/core-tokenizer';
5
- import type { Delimiter, Node, T, Token, TokenizerProps } from './types';
3
+ import type { IDelimiter, INode, IThis, IToken, ITokenizerProps, T } from './types';
6
4
  /**
7
5
  * Lexical Analyzer for inlineMath.
8
6
  */
9
- 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> {
10
8
  readonly backtickRequired: boolean;
11
- constructor(props?: TokenizerProps);
12
- /**
13
- * @override
14
- * @see BaseInlineTokenizer
15
- */
16
- findDelimiter(nodePoints: ReadonlyArray<NodePoint>, api: Readonly<MatchInlinePhaseApi>): ResultOfFindDelimiters<Delimiter>;
17
- /**
18
- * @override
19
- * @see BaseInlineTokenizer
20
- */
21
- protected _findDelimiter(): null;
22
- /**
23
- * @override
24
- * @see TokenizerMatchInlineHook
25
- */
26
- processSingleDelimiter(delimiter: Delimiter): ResultOfProcessSingleDelimiter<T, Token>;
27
- /**
28
- * @override
29
- * @see TokenizerParseInlineHook
30
- */
31
- processToken(token: Token, children: YastNode[] | undefined, nodePoints: ReadonlyArray<NodePoint>): Node;
9
+ constructor(props?: ITokenizerProps);
10
+ readonly match: IMatchInlineHookCreator<T, IDelimiter, IToken, IThis>;
11
+ readonly parse: IParseInlineHookCreator<T, IToken, INode, IThis>;
32
12
  }
@@ -1,25 +1,31 @@
1
- import type { InlineMath, InlineMathType } from '@yozora/ast';
2
- import type { BaseInlineTokenizerProps, PartialYastInlineToken, YastTokenDelimiter } from '@yozora/core-tokenizer';
1
+ import type { IInlineMath, InlineMathType } from '@yozora/ast';
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 = IInlineMath;
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.2.2",
3
+ "version": "2.0.0-alpha.2",
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.2.2",
49
- "@yozora/character": "^1.2.2",
50
- "@yozora/core-tokenizer": "^1.2.2"
48
+ "@yozora/ast": "^2.0.0-alpha.2",
49
+ "@yozora/character": "^2.0.0-alpha.2",
50
+ "@yozora/core-tokenizer": "^2.0.0-alpha.2"
51
51
  },
52
52
  "devDependencies": {
53
- "@yozora/tokenizer-inline-code": "^1.2.2"
53
+ "@yozora/tokenizer-inline-code": "^2.0.0-alpha.2"
54
54
  },
55
- "gitHead": "fb4b804548592c09259aabcf9cc4dd96176bd024"
55
+ "gitHead": "da59d85520455c59a117a35032ef1a035c10ea21"
56
56
  }