@yozora/tokenizer-autolink 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
@@ -2,12 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var ast = require('@yozora/ast');
6
5
  var character = require('@yozora/character');
6
+ var ast = require('@yozora/ast');
7
7
  var coreTokenizer = require('@yozora/core-tokenizer');
8
8
 
9
- const uniqueName = '@yozora/tokenizer-autolink';
10
-
11
9
  function eatEmailAddress(nodePoints, startIndex, endIndex) {
12
10
  let i = startIndex;
13
11
  for (; i < endIndex; i += 1) {
@@ -105,6 +103,8 @@ function eatAutolinkSchema(nodePoints, startIndex, endIndex) {
105
103
  return { valid: true, nextIndex: i };
106
104
  }
107
105
 
106
+ const uniqueName = '@yozora/tokenizer-autolink';
107
+
108
108
  const helpers = [
109
109
  { contentType: 'uri', eat: eatAbsoluteUri },
110
110
  { contentType: 'email', eat: eatEmailAddress },
@@ -116,67 +116,77 @@ class AutolinkTokenizer extends coreTokenizer.BaseInlineTokenizer {
116
116
  name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
117
117
  priority: (_b = props.priority) !== null && _b !== void 0 ? _b : coreTokenizer.TokenizerPriority.ATOMIC,
118
118
  });
119
- }
120
- _findDelimiter(startIndex, endIndex, nodePoints) {
121
- for (let i = startIndex; i < endIndex; ++i) {
122
- if (nodePoints[i].codePoint !== character.AsciiCodePoint.OPEN_ANGLE)
123
- continue;
124
- let nextIndex = endIndex;
125
- let contentType = null;
126
- for (const helper of helpers) {
127
- const eatResult = helper.eat(nodePoints, i + 1, endIndex);
128
- nextIndex = Math.min(nextIndex, eatResult.nextIndex);
129
- if (eatResult.valid) {
130
- contentType = helper.contentType;
131
- nextIndex = eatResult.nextIndex;
132
- break;
119
+ this.match = api => {
120
+ return {
121
+ findDelimiter: () => coreTokenizer.genFindDelimiter(_findDelimiter),
122
+ processSingleDelimiter,
123
+ };
124
+ function _findDelimiter(startIndex, endIndex) {
125
+ const nodePoints = api.getNodePoints();
126
+ for (let i = startIndex; i < endIndex; ++i) {
127
+ if (nodePoints[i].codePoint !== character.AsciiCodePoint.OPEN_ANGLE)
128
+ continue;
129
+ let nextIndex = endIndex;
130
+ let contentType = null;
131
+ for (const helper of helpers) {
132
+ const eatResult = helper.eat(nodePoints, i + 1, endIndex);
133
+ nextIndex = Math.min(nextIndex, eatResult.nextIndex);
134
+ if (eatResult.valid) {
135
+ contentType = helper.contentType;
136
+ nextIndex = eatResult.nextIndex;
137
+ break;
138
+ }
139
+ }
140
+ if (contentType == null) {
141
+ i = Math.max(i, nextIndex - 1);
142
+ continue;
143
+ }
144
+ if (nextIndex < endIndex &&
145
+ nodePoints[nextIndex].codePoint === character.AsciiCodePoint.CLOSE_ANGLE) {
146
+ return {
147
+ type: 'full',
148
+ startIndex: i,
149
+ endIndex: nextIndex + 1,
150
+ contentType,
151
+ };
152
+ }
153
+ i = nextIndex - 1;
133
154
  }
155
+ return null;
134
156
  }
135
- if (contentType == null) {
136
- i = Math.max(i, nextIndex - 1);
137
- continue;
138
- }
139
- if (nextIndex < endIndex &&
140
- nodePoints[nextIndex].codePoint === character.AsciiCodePoint.CLOSE_ANGLE) {
141
- return {
142
- type: 'full',
143
- startIndex: i,
144
- endIndex: nextIndex + 1,
145
- contentType,
157
+ function processSingleDelimiter(delimiter) {
158
+ const token = {
159
+ nodeType: ast.LinkType,
160
+ startIndex: delimiter.startIndex,
161
+ endIndex: delimiter.endIndex,
162
+ contentType: delimiter.contentType,
163
+ children: api.resolveFallbackTokens([], delimiter.startIndex + 1, delimiter.endIndex - 1),
146
164
  };
165
+ return [token];
147
166
  }
148
- i = nextIndex - 1;
149
- }
150
- return null;
151
- }
152
- processSingleDelimiter(delimiter, nodePoints, api) {
153
- const token = {
154
- nodeType: ast.LinkType,
155
- startIndex: delimiter.startIndex,
156
- endIndex: delimiter.endIndex,
157
- contentType: delimiter.contentType,
158
- children: api.resolveFallbackTokens([], delimiter.startIndex + 1, delimiter.endIndex - 1, nodePoints),
159
- };
160
- return [token];
161
- }
162
- processToken(token, children, nodePoints) {
163
- let url = character.calcStringFromNodePoints(nodePoints, token.startIndex + 1, token.endIndex - 1);
164
- if (token.contentType === 'email') {
165
- url = 'mailto:' + url;
166
- }
167
- const encodedUrl = coreTokenizer.encodeLinkDestination(url);
168
- const result = {
169
- type: ast.LinkType,
170
- url: encodedUrl,
171
- children: children !== null && children !== void 0 ? children : [],
172
167
  };
173
- return result;
168
+ this.parse = api => ({
169
+ parse: (token, children) => {
170
+ const nodePoints = api.getNodePoints();
171
+ let url = character.calcStringFromNodePoints(nodePoints, token.startIndex + 1, token.endIndex - 1);
172
+ if (token.contentType === 'email') {
173
+ url = 'mailto:' + url;
174
+ }
175
+ const encodedUrl = coreTokenizer.encodeLinkDestination(url);
176
+ const result = {
177
+ type: ast.LinkType,
178
+ url: encodedUrl,
179
+ children,
180
+ };
181
+ return result;
182
+ },
183
+ });
174
184
  }
175
185
  }
176
186
 
177
187
  exports.AutolinkTokenizer = AutolinkTokenizer;
178
188
  exports.AutolinkTokenizerName = uniqueName;
179
- exports['default'] = AutolinkTokenizer;
189
+ exports["default"] = AutolinkTokenizer;
180
190
  exports.eatAbsoluteUri = eatAbsoluteUri;
181
191
  exports.eatAutolinkSchema = eatAutolinkSchema;
182
192
  exports.eatEmailAddress = eatEmailAddress;
package/lib/esm/index.js CHANGED
@@ -1,8 +1,6 @@
1
- import { LinkType } from '@yozora/ast';
2
1
  import { isAsciiLetter, isAsciiDigitCharacter, AsciiCodePoint, isAlphanumeric, isAsciiCharacter, isWhitespaceCharacter, isAsciiControlCharacter, calcStringFromNodePoints } from '@yozora/character';
3
- import { BaseInlineTokenizer, TokenizerPriority, encodeLinkDestination } from '@yozora/core-tokenizer';
4
-
5
- const uniqueName = '@yozora/tokenizer-autolink';
2
+ import { LinkType } from '@yozora/ast';
3
+ import { BaseInlineTokenizer, TokenizerPriority, genFindDelimiter, encodeLinkDestination } from '@yozora/core-tokenizer';
6
4
 
7
5
  function eatEmailAddress(nodePoints, startIndex, endIndex) {
8
6
  let i = startIndex;
@@ -101,6 +99,8 @@ function eatAutolinkSchema(nodePoints, startIndex, endIndex) {
101
99
  return { valid: true, nextIndex: i };
102
100
  }
103
101
 
102
+ const uniqueName = '@yozora/tokenizer-autolink';
103
+
104
104
  const helpers = [
105
105
  { contentType: 'uri', eat: eatAbsoluteUri },
106
106
  { contentType: 'email', eat: eatEmailAddress },
@@ -112,61 +112,71 @@ class AutolinkTokenizer extends BaseInlineTokenizer {
112
112
  name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
113
113
  priority: (_b = props.priority) !== null && _b !== void 0 ? _b : TokenizerPriority.ATOMIC,
114
114
  });
115
- }
116
- _findDelimiter(startIndex, endIndex, nodePoints) {
117
- for (let i = startIndex; i < endIndex; ++i) {
118
- if (nodePoints[i].codePoint !== AsciiCodePoint.OPEN_ANGLE)
119
- continue;
120
- let nextIndex = endIndex;
121
- let contentType = null;
122
- for (const helper of helpers) {
123
- const eatResult = helper.eat(nodePoints, i + 1, endIndex);
124
- nextIndex = Math.min(nextIndex, eatResult.nextIndex);
125
- if (eatResult.valid) {
126
- contentType = helper.contentType;
127
- nextIndex = eatResult.nextIndex;
128
- break;
115
+ this.match = api => {
116
+ return {
117
+ findDelimiter: () => genFindDelimiter(_findDelimiter),
118
+ processSingleDelimiter,
119
+ };
120
+ function _findDelimiter(startIndex, endIndex) {
121
+ const nodePoints = api.getNodePoints();
122
+ for (let i = startIndex; i < endIndex; ++i) {
123
+ if (nodePoints[i].codePoint !== AsciiCodePoint.OPEN_ANGLE)
124
+ continue;
125
+ let nextIndex = endIndex;
126
+ let contentType = null;
127
+ for (const helper of helpers) {
128
+ const eatResult = helper.eat(nodePoints, i + 1, endIndex);
129
+ nextIndex = Math.min(nextIndex, eatResult.nextIndex);
130
+ if (eatResult.valid) {
131
+ contentType = helper.contentType;
132
+ nextIndex = eatResult.nextIndex;
133
+ break;
134
+ }
135
+ }
136
+ if (contentType == null) {
137
+ i = Math.max(i, nextIndex - 1);
138
+ continue;
139
+ }
140
+ if (nextIndex < endIndex &&
141
+ nodePoints[nextIndex].codePoint === AsciiCodePoint.CLOSE_ANGLE) {
142
+ return {
143
+ type: 'full',
144
+ startIndex: i,
145
+ endIndex: nextIndex + 1,
146
+ contentType,
147
+ };
148
+ }
149
+ i = nextIndex - 1;
129
150
  }
151
+ return null;
130
152
  }
131
- if (contentType == null) {
132
- i = Math.max(i, nextIndex - 1);
133
- continue;
134
- }
135
- if (nextIndex < endIndex &&
136
- nodePoints[nextIndex].codePoint === AsciiCodePoint.CLOSE_ANGLE) {
137
- return {
138
- type: 'full',
139
- startIndex: i,
140
- endIndex: nextIndex + 1,
141
- contentType,
153
+ function processSingleDelimiter(delimiter) {
154
+ const token = {
155
+ nodeType: LinkType,
156
+ startIndex: delimiter.startIndex,
157
+ endIndex: delimiter.endIndex,
158
+ contentType: delimiter.contentType,
159
+ children: api.resolveFallbackTokens([], delimiter.startIndex + 1, delimiter.endIndex - 1),
142
160
  };
161
+ return [token];
143
162
  }
144
- i = nextIndex - 1;
145
- }
146
- return null;
147
- }
148
- processSingleDelimiter(delimiter, nodePoints, api) {
149
- const token = {
150
- nodeType: LinkType,
151
- startIndex: delimiter.startIndex,
152
- endIndex: delimiter.endIndex,
153
- contentType: delimiter.contentType,
154
- children: api.resolveFallbackTokens([], delimiter.startIndex + 1, delimiter.endIndex - 1, nodePoints),
155
- };
156
- return [token];
157
- }
158
- processToken(token, children, nodePoints) {
159
- let url = calcStringFromNodePoints(nodePoints, token.startIndex + 1, token.endIndex - 1);
160
- if (token.contentType === 'email') {
161
- url = 'mailto:' + url;
162
- }
163
- const encodedUrl = encodeLinkDestination(url);
164
- const result = {
165
- type: LinkType,
166
- url: encodedUrl,
167
- children: children !== null && children !== void 0 ? children : [],
168
163
  };
169
- return result;
164
+ this.parse = api => ({
165
+ parse: (token, children) => {
166
+ const nodePoints = api.getNodePoints();
167
+ let url = calcStringFromNodePoints(nodePoints, token.startIndex + 1, token.endIndex - 1);
168
+ if (token.contentType === 'email') {
169
+ url = 'mailto:' + url;
170
+ }
171
+ const encodedUrl = encodeLinkDestination(url);
172
+ const result = {
173
+ type: LinkType,
174
+ url: encodedUrl,
175
+ children,
176
+ };
177
+ return result;
178
+ },
179
+ });
170
180
  }
171
181
  }
172
182
 
@@ -1,7 +1,5 @@
1
- import { AutolinkTokenizer } from './tokenizer';
2
1
  export * from './util/email';
3
2
  export * from './util/uri';
4
- export { AutolinkTokenizer } from './tokenizer';
3
+ export { AutolinkTokenizer, AutolinkTokenizer as default } from './tokenizer';
5
4
  export { uniqueName as AutolinkTokenizerName } from './types';
6
- export type { Token as AutolinkToken, TokenizerProps as AutolinkTokenizerProps, AutolinkContentType, } from './types';
7
- export default AutolinkTokenizer;
5
+ export type { IToken as IAutolinkToken, ITokenizerProps as IAutolinkTokenizerProps, AutolinkContentType, } from './types';
@@ -1,8 +1,6 @@
1
- import type { YastNode } from '@yozora/ast';
2
- import type { NodePoint } from '@yozora/character';
3
- import type { MatchInlinePhaseApi, 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, IToken, ITokenizerProps, T } from './types';
6
4
  /**
7
5
  * Lexical Analyzer for Autolink.
8
6
  *
@@ -11,21 +9,8 @@ import type { Delimiter, Node, T, Token, TokenizerProps } from './types';
11
9
  *
12
10
  * @see https://github.github.com/gfm/#autolink
13
11
  */
14
- export declare class AutolinkTokenizer extends BaseInlineTokenizer<Delimiter> implements Tokenizer, TokenizerMatchInlineHook<T, Delimiter, Token>, TokenizerParseInlineHook<T, Token, Node> {
15
- constructor(props?: TokenizerProps);
16
- /**
17
- * @override
18
- * @see BaseInlineTokenizer
19
- */
20
- protected _findDelimiter(startIndex: number, endIndex: number, nodePoints: ReadonlyArray<NodePoint>): Delimiter | null;
21
- /**
22
- * @override
23
- * @see TokenizerMatchInlineHook
24
- */
25
- processSingleDelimiter(delimiter: Delimiter, nodePoints: ReadonlyArray<NodePoint>, api: Readonly<MatchInlinePhaseApi>): ResultOfProcessSingleDelimiter<T, Token>;
26
- /**
27
- * @override
28
- * @see TokenizerParseInlineHook
29
- */
30
- processToken(token: Token, children: YastNode[] | undefined, nodePoints: ReadonlyArray<NodePoint>): Node;
12
+ export declare class AutolinkTokenizer extends BaseInlineTokenizer<T, IDelimiter, IToken, INode> implements IInlineTokenizer<T, IDelimiter, IToken, INode> {
13
+ constructor(props?: ITokenizerProps);
14
+ readonly match: IMatchInlineHookCreator<T, IDelimiter, IToken>;
15
+ readonly parse: IParseInlineHookCreator<T, IToken, INode>;
31
16
  }
@@ -1,26 +1,26 @@
1
- import type { Link, LinkType } from '@yozora/ast';
2
- import type { NodePoint } from '@yozora/character';
3
- import type { BaseInlineTokenizerProps, PartialYastInlineToken, ResultOfRequiredEater, YastTokenDelimiter } from '@yozora/core-tokenizer';
1
+ import type { ILink, LinkType } from '@yozora/ast';
2
+ import type { INodePoint } from '@yozora/character';
3
+ import type { IBaseInlineTokenizerProps, IPartialYastInlineToken, IResultOfRequiredEater, IYastTokenDelimiter } from '@yozora/core-tokenizer';
4
4
  export declare type AutolinkContentType = 'uri' | 'email';
5
5
  export declare type T = LinkType;
6
- export declare type Node = Link;
6
+ export declare type INode = ILink;
7
7
  export declare const uniqueName = "@yozora/tokenizer-autolink";
8
- export interface Token extends PartialYastInlineToken<T> {
8
+ export interface IToken extends IPartialYastInlineToken<T> {
9
9
  /**
10
10
  * Autolink content type: absolute uri or email.
11
11
  */
12
12
  contentType: AutolinkContentType;
13
13
  }
14
- export interface Delimiter extends YastTokenDelimiter {
14
+ export interface IDelimiter extends IYastTokenDelimiter {
15
15
  type: 'full';
16
16
  /**
17
17
  * Autolink content type: absolute uri or email.
18
18
  */
19
19
  contentType: AutolinkContentType;
20
20
  }
21
- export declare type TokenizerProps = Partial<BaseInlineTokenizerProps>;
22
- export declare type ContentEater = (nodePoints: ReadonlyArray<NodePoint>, startIndex: number, endIndex: number) => ResultOfRequiredEater;
23
- export interface ContentHelper {
21
+ export declare type ITokenizerProps = Partial<IBaseInlineTokenizerProps>;
22
+ export declare type ContentEater = (nodePoints: ReadonlyArray<INodePoint>, startIndex: number, endIndex: number) => IResultOfRequiredEater;
23
+ export interface IContentHelper {
24
24
  contentType: AutolinkContentType;
25
25
  eat: ContentEater;
26
26
  }
@@ -1,5 +1,5 @@
1
- import type { NodePoint } from '@yozora/character';
2
- import type { ResultOfRequiredEater } from '@yozora/core-tokenizer';
1
+ import type { INodePoint } from '@yozora/character';
2
+ import type { IResultOfRequiredEater } from '@yozora/core-tokenizer';
3
3
  /**
4
4
  * An email address, for these purposes, is anything that matches the
5
5
  * non-normative regex from the HTML5 spec:
@@ -9,4 +9,4 @@ import type { ResultOfRequiredEater } from '@yozora/core-tokenizer';
9
9
  *
10
10
  * @see https://github.github.com/gfm/#email-address
11
11
  */
12
- export declare function eatEmailAddress(nodePoints: ReadonlyArray<NodePoint>, startIndex: number, endIndex: number): ResultOfRequiredEater;
12
+ export declare function eatEmailAddress(nodePoints: ReadonlyArray<INodePoint>, startIndex: number, endIndex: number): IResultOfRequiredEater;
@@ -1,5 +1,5 @@
1
- import type { NodePoint } from '@yozora/character';
2
- import type { ResultOfRequiredEater } from '@yozora/core-tokenizer';
1
+ import type { INodePoint } from '@yozora/character';
2
+ import type { IResultOfRequiredEater } from '@yozora/core-tokenizer';
3
3
  /**
4
4
  * Try to find to autolink absolute uri strictly start from the give `startIndex`.
5
5
  *
@@ -10,7 +10,7 @@ import type { ResultOfRequiredEater } from '@yozora/core-tokenizer';
10
10
  *
11
11
  * @see https://github.github.com/gfm/#absolute-uri
12
12
  */
13
- export declare function eatAbsoluteUri(nodePoints: ReadonlyArray<NodePoint>, startIndex: number, endIndex: number): ResultOfRequiredEater;
13
+ export declare function eatAbsoluteUri(nodePoints: ReadonlyArray<INodePoint>, startIndex: number, endIndex: number): IResultOfRequiredEater;
14
14
  /**
15
15
  * Try to find to autolink schema strictly start from the give `startIndex`.
16
16
  *
@@ -20,4 +20,4 @@ export declare function eatAbsoluteUri(nodePoints: ReadonlyArray<NodePoint>, sta
20
20
  *
21
21
  * @see https://github.github.com/gfm/#scheme
22
22
  */
23
- export declare function eatAutolinkSchema(nodePoints: ReadonlyArray<NodePoint>, startIndex: number, endIndex: number): ResultOfRequiredEater;
23
+ export declare function eatAutolinkSchema(nodePoints: ReadonlyArray<INodePoint>, startIndex: number, endIndex: number): IResultOfRequiredEater;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-autolink",
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
  }