@yozora/tokenizer-emphasis 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
@@ -15,136 +15,151 @@ class EmphasisTokenizer extends coreTokenizer.BaseInlineTokenizer {
15
15
  name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
16
16
  priority: (_b = props.priority) !== null && _b !== void 0 ? _b : coreTokenizer.TokenizerPriority.CONTAINING_INLINE,
17
17
  });
18
- }
19
- _findDelimiter(startIndex, endIndex, nodePoints) {
20
- const isOpenerDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
21
- const nextCodePosition = delimiterEndIndex >= endIndex ? null : nodePoints[delimiterEndIndex];
22
- if (nextCodePosition == null ||
23
- character.isUnicodeWhitespaceCharacter(nextCodePosition.codePoint))
24
- return false;
25
- if (!character.isPunctuationCharacter(nextCodePosition.codePoint))
26
- return true;
27
- if (delimiterStartIndex <= startIndex)
28
- return true;
29
- const prevCodePosition = nodePoints[delimiterStartIndex - 1];
30
- return (character.isUnicodeWhitespaceCharacter(prevCodePosition.codePoint) ||
31
- character.isPunctuationCharacter(prevCodePosition.codePoint));
32
- };
33
- const isCloserDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
34
- if (delimiterStartIndex > startIndex) {
35
- const prevCodePosition = nodePoints[delimiterStartIndex - 1];
36
- if (character.isUnicodeWhitespaceCharacter(prevCodePosition.codePoint))
37
- return false;
38
- if (!character.isPunctuationCharacter(prevCodePosition.codePoint))
39
- return true;
40
- }
41
- if (delimiterEndIndex >= endIndex)
42
- return true;
43
- const nextCodePosition = nodePoints[delimiterEndIndex];
44
- return (character.isUnicodeWhitespaceCharacter(nextCodePosition.codePoint) ||
45
- character.isPunctuationCharacter(nextCodePosition.codePoint));
46
- };
47
- for (let i = startIndex; i < endIndex; ++i) {
48
- const c = nodePoints[i].codePoint;
49
- switch (c) {
50
- case character.AsciiCodePoint.BACKSLASH:
51
- i += 1;
52
- break;
53
- case character.AsciiCodePoint.ASTERISK:
54
- case character.AsciiCodePoint.UNDERSCORE: {
55
- const _startIndex = i;
56
- i = coreTokenizer.eatOptionalCharacters(nodePoints, i + 1, endIndex, c) - 1;
57
- const _endIndex = i + 1;
58
- const isLeftFlankingDelimiterRun = isOpenerDelimiter(_startIndex, _endIndex);
59
- const isRightFlankingDelimiterRun = isCloserDelimiter(_startIndex, _endIndex);
60
- let isOpener = isLeftFlankingDelimiterRun;
61
- let isCloser = isRightFlankingDelimiterRun;
62
- if (c === character.AsciiCodePoint.UNDERSCORE) {
63
- if (isLeftFlankingDelimiterRun && isRightFlankingDelimiterRun) {
64
- if (_startIndex > startIndex &&
65
- !character.isPunctuationCharacter(nodePoints[_startIndex - 1].codePoint)) {
66
- isOpener = false;
67
- }
68
- const nextCodePosition = nodePoints[_endIndex];
69
- if (!character.isPunctuationCharacter(nextCodePosition.codePoint)) {
70
- isCloser = false;
18
+ this.match = api => {
19
+ return {
20
+ findDelimiter: () => coreTokenizer.genFindDelimiter(_findDelimiter),
21
+ isDelimiterPair,
22
+ processDelimiterPair,
23
+ };
24
+ function _findDelimiter(startIndex, endIndex) {
25
+ const nodePoints = api.getNodePoints();
26
+ const blockStartIndex = api.getBlockStartIndex();
27
+ const blockEndIndex = api.getBlockEndIndex();
28
+ const isOpenerDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
29
+ if (delimiterEndIndex === blockEndIndex)
30
+ return false;
31
+ if (delimiterEndIndex === endIndex)
32
+ return true;
33
+ const nextCodePosition = nodePoints[delimiterEndIndex];
34
+ if (character.isUnicodeWhitespaceCharacter(nextCodePosition.codePoint))
35
+ return false;
36
+ if (!character.isPunctuationCharacter(nextCodePosition.codePoint))
37
+ return true;
38
+ if (delimiterStartIndex <= startIndex)
39
+ return true;
40
+ const prevCodePosition = nodePoints[delimiterStartIndex - 1];
41
+ return (character.isUnicodeWhitespaceCharacter(prevCodePosition.codePoint) ||
42
+ character.isPunctuationCharacter(prevCodePosition.codePoint));
43
+ };
44
+ const isCloserDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
45
+ if (delimiterStartIndex === blockStartIndex)
46
+ return false;
47
+ if (delimiterStartIndex === startIndex)
48
+ return true;
49
+ const prevCodePosition = nodePoints[delimiterStartIndex - 1];
50
+ if (character.isUnicodeWhitespaceCharacter(prevCodePosition.codePoint))
51
+ return false;
52
+ if (!character.isPunctuationCharacter(prevCodePosition.codePoint))
53
+ return true;
54
+ if (delimiterEndIndex >= endIndex)
55
+ return true;
56
+ const nextCodePosition = nodePoints[delimiterEndIndex];
57
+ return (character.isUnicodeWhitespaceCharacter(nextCodePosition.codePoint) ||
58
+ character.isPunctuationCharacter(nextCodePosition.codePoint));
59
+ };
60
+ for (let i = startIndex; i < endIndex; ++i) {
61
+ const c = nodePoints[i].codePoint;
62
+ switch (c) {
63
+ case character.AsciiCodePoint.BACKSLASH:
64
+ i += 1;
65
+ break;
66
+ case character.AsciiCodePoint.ASTERISK:
67
+ case character.AsciiCodePoint.UNDERSCORE: {
68
+ const _startIndex = i;
69
+ i = coreTokenizer.eatOptionalCharacters(nodePoints, i + 1, endIndex, c) - 1;
70
+ const _endIndex = i + 1;
71
+ const isLeftFlankingDelimiterRun = isOpenerDelimiter(_startIndex, _endIndex);
72
+ const isRightFlankingDelimiterRun = isCloserDelimiter(_startIndex, _endIndex);
73
+ let isOpener = isLeftFlankingDelimiterRun;
74
+ let isCloser = isRightFlankingDelimiterRun;
75
+ if (c === character.AsciiCodePoint.UNDERSCORE) {
76
+ if (isLeftFlankingDelimiterRun && isRightFlankingDelimiterRun) {
77
+ if (_startIndex > startIndex &&
78
+ !character.isPunctuationCharacter(nodePoints[_startIndex - 1].codePoint)) {
79
+ isOpener = false;
80
+ }
81
+ const nextCodePosition = nodePoints[_endIndex];
82
+ if (!character.isPunctuationCharacter(nextCodePosition.codePoint)) {
83
+ isCloser = false;
84
+ }
85
+ }
71
86
  }
87
+ if (!isOpener && !isCloser)
88
+ break;
89
+ const thickness = _endIndex - _startIndex;
90
+ return {
91
+ type: isOpener ? (isCloser ? 'both' : 'opener') : 'closer',
92
+ startIndex: _startIndex,
93
+ endIndex: _endIndex,
94
+ thickness,
95
+ originalThickness: thickness,
96
+ };
72
97
  }
73
98
  }
74
- if (!isOpener && !isCloser)
75
- break;
76
- const thickness = _endIndex - _startIndex;
77
- return {
78
- type: isOpener ? (isCloser ? 'both' : 'opener') : 'closer',
79
- startIndex: _startIndex,
80
- endIndex: _endIndex,
81
- thickness,
82
- originalThickness: thickness,
83
- };
84
99
  }
100
+ return null;
85
101
  }
86
- }
87
- return null;
88
- }
89
- isDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, nodePoints) {
90
- if (nodePoints[openerDelimiter.startIndex].codePoint !==
91
- nodePoints[closerDelimiter.startIndex].codePoint ||
92
- ((openerDelimiter.type === 'both' || closerDelimiter.type === 'both') &&
93
- (openerDelimiter.originalThickness +
94
- closerDelimiter.originalThickness) %
95
- 3 ===
96
- 0 &&
97
- openerDelimiter.originalThickness % 3 !== 0)) {
98
- return { paired: false, opener: true, closer: true };
99
- }
100
- return { paired: true };
101
- }
102
- processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, nodePoints, api) {
103
- let thickness = 1;
104
- if (openerDelimiter.thickness > 1 && closerDelimiter.thickness > 1) {
105
- thickness = 2;
106
- }
107
- internalTokens = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex, nodePoints);
108
- const token = {
109
- nodeType: thickness === 1 ? ast.EmphasisType : ast.StrongType,
110
- startIndex: openerDelimiter.endIndex - thickness,
111
- endIndex: closerDelimiter.startIndex + thickness,
112
- thickness,
113
- children: internalTokens,
114
- };
115
- const remainOpenerDelimiter = openerDelimiter.thickness > thickness
116
- ? {
117
- type: openerDelimiter.type,
118
- startIndex: openerDelimiter.startIndex,
119
- endIndex: openerDelimiter.endIndex - thickness,
120
- thickness: openerDelimiter.thickness - thickness,
121
- originalThickness: openerDelimiter.originalThickness,
102
+ function isDelimiterPair(openerDelimiter, closerDelimiter) {
103
+ const nodePoints = api.getNodePoints();
104
+ if (nodePoints[openerDelimiter.startIndex].codePoint !==
105
+ nodePoints[closerDelimiter.startIndex].codePoint ||
106
+ ((openerDelimiter.type === 'both' || closerDelimiter.type === 'both') &&
107
+ (openerDelimiter.originalThickness + closerDelimiter.originalThickness) % 3 === 0 &&
108
+ openerDelimiter.originalThickness % 3 !== 0)) {
109
+ return { paired: false, opener: true, closer: true };
110
+ }
111
+ return { paired: true };
122
112
  }
123
- : undefined;
124
- const remainCloserDelimiter = closerDelimiter.thickness > thickness
125
- ? {
126
- type: closerDelimiter.type,
127
- startIndex: closerDelimiter.startIndex + thickness,
128
- endIndex: closerDelimiter.endIndex,
129
- thickness: closerDelimiter.thickness - thickness,
130
- originalThickness: closerDelimiter.originalThickness,
113
+ function processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens) {
114
+ let thickness = 1;
115
+ if (openerDelimiter.thickness > 1 && closerDelimiter.thickness > 1) {
116
+ thickness = 2;
117
+ }
118
+ internalTokens = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
119
+ const token = {
120
+ nodeType: thickness === 1 ? ast.EmphasisType : ast.StrongType,
121
+ startIndex: openerDelimiter.endIndex - thickness,
122
+ endIndex: closerDelimiter.startIndex + thickness,
123
+ thickness,
124
+ children: internalTokens,
125
+ };
126
+ const remainOpenerDelimiter = openerDelimiter.thickness > thickness
127
+ ? {
128
+ type: openerDelimiter.type,
129
+ startIndex: openerDelimiter.startIndex,
130
+ endIndex: openerDelimiter.endIndex - thickness,
131
+ thickness: openerDelimiter.thickness - thickness,
132
+ originalThickness: openerDelimiter.originalThickness,
133
+ }
134
+ : undefined;
135
+ const remainCloserDelimiter = closerDelimiter.thickness > thickness
136
+ ? {
137
+ type: closerDelimiter.type,
138
+ startIndex: closerDelimiter.startIndex + thickness,
139
+ endIndex: closerDelimiter.endIndex,
140
+ thickness: closerDelimiter.thickness - thickness,
141
+ originalThickness: closerDelimiter.originalThickness,
142
+ }
143
+ : undefined;
144
+ return {
145
+ tokens: [token],
146
+ remainOpenerDelimiter,
147
+ remainCloserDelimiter,
148
+ };
131
149
  }
132
- : undefined;
133
- return {
134
- tokens: [token],
135
- remainOpenerDelimiter,
136
- remainCloserDelimiter,
137
150
  };
138
- }
139
- processToken(token, children) {
140
- const result = {
141
- type: token.nodeType,
142
- children: children !== null && children !== void 0 ? children : [],
143
- };
144
- return result;
151
+ this.parse = () => ({
152
+ parse: (token, children) => {
153
+ const result = {
154
+ type: token.nodeType,
155
+ children,
156
+ };
157
+ return result;
158
+ },
159
+ });
145
160
  }
146
161
  }
147
162
 
148
163
  exports.EmphasisTokenizer = EmphasisTokenizer;
149
164
  exports.EmphasisTokenizerName = uniqueName;
150
- exports['default'] = EmphasisTokenizer;
165
+ exports["default"] = EmphasisTokenizer;
package/lib/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { EmphasisType, StrongType } from '@yozora/ast';
2
2
  import { AsciiCodePoint, isPunctuationCharacter, isUnicodeWhitespaceCharacter } from '@yozora/character';
3
- import { BaseInlineTokenizer, TokenizerPriority, eatOptionalCharacters } from '@yozora/core-tokenizer';
3
+ import { BaseInlineTokenizer, TokenizerPriority, genFindDelimiter, eatOptionalCharacters } from '@yozora/core-tokenizer';
4
4
 
5
5
  const uniqueName = '@yozora/tokenizer-emphasis';
6
6
 
@@ -11,133 +11,148 @@ class EmphasisTokenizer extends BaseInlineTokenizer {
11
11
  name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
12
12
  priority: (_b = props.priority) !== null && _b !== void 0 ? _b : TokenizerPriority.CONTAINING_INLINE,
13
13
  });
14
- }
15
- _findDelimiter(startIndex, endIndex, nodePoints) {
16
- const isOpenerDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
17
- const nextCodePosition = delimiterEndIndex >= endIndex ? null : nodePoints[delimiterEndIndex];
18
- if (nextCodePosition == null ||
19
- isUnicodeWhitespaceCharacter(nextCodePosition.codePoint))
20
- return false;
21
- if (!isPunctuationCharacter(nextCodePosition.codePoint))
22
- return true;
23
- if (delimiterStartIndex <= startIndex)
24
- return true;
25
- const prevCodePosition = nodePoints[delimiterStartIndex - 1];
26
- return (isUnicodeWhitespaceCharacter(prevCodePosition.codePoint) ||
27
- isPunctuationCharacter(prevCodePosition.codePoint));
28
- };
29
- const isCloserDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
30
- if (delimiterStartIndex > startIndex) {
31
- const prevCodePosition = nodePoints[delimiterStartIndex - 1];
32
- if (isUnicodeWhitespaceCharacter(prevCodePosition.codePoint))
33
- return false;
34
- if (!isPunctuationCharacter(prevCodePosition.codePoint))
35
- return true;
36
- }
37
- if (delimiterEndIndex >= endIndex)
38
- return true;
39
- const nextCodePosition = nodePoints[delimiterEndIndex];
40
- return (isUnicodeWhitespaceCharacter(nextCodePosition.codePoint) ||
41
- isPunctuationCharacter(nextCodePosition.codePoint));
42
- };
43
- for (let i = startIndex; i < endIndex; ++i) {
44
- const c = nodePoints[i].codePoint;
45
- switch (c) {
46
- case AsciiCodePoint.BACKSLASH:
47
- i += 1;
48
- break;
49
- case AsciiCodePoint.ASTERISK:
50
- case AsciiCodePoint.UNDERSCORE: {
51
- const _startIndex = i;
52
- i = eatOptionalCharacters(nodePoints, i + 1, endIndex, c) - 1;
53
- const _endIndex = i + 1;
54
- const isLeftFlankingDelimiterRun = isOpenerDelimiter(_startIndex, _endIndex);
55
- const isRightFlankingDelimiterRun = isCloserDelimiter(_startIndex, _endIndex);
56
- let isOpener = isLeftFlankingDelimiterRun;
57
- let isCloser = isRightFlankingDelimiterRun;
58
- if (c === AsciiCodePoint.UNDERSCORE) {
59
- if (isLeftFlankingDelimiterRun && isRightFlankingDelimiterRun) {
60
- if (_startIndex > startIndex &&
61
- !isPunctuationCharacter(nodePoints[_startIndex - 1].codePoint)) {
62
- isOpener = false;
63
- }
64
- const nextCodePosition = nodePoints[_endIndex];
65
- if (!isPunctuationCharacter(nextCodePosition.codePoint)) {
66
- isCloser = false;
14
+ this.match = api => {
15
+ return {
16
+ findDelimiter: () => genFindDelimiter(_findDelimiter),
17
+ isDelimiterPair,
18
+ processDelimiterPair,
19
+ };
20
+ function _findDelimiter(startIndex, endIndex) {
21
+ const nodePoints = api.getNodePoints();
22
+ const blockStartIndex = api.getBlockStartIndex();
23
+ const blockEndIndex = api.getBlockEndIndex();
24
+ const isOpenerDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
25
+ if (delimiterEndIndex === blockEndIndex)
26
+ return false;
27
+ if (delimiterEndIndex === endIndex)
28
+ return true;
29
+ const nextCodePosition = nodePoints[delimiterEndIndex];
30
+ if (isUnicodeWhitespaceCharacter(nextCodePosition.codePoint))
31
+ return false;
32
+ if (!isPunctuationCharacter(nextCodePosition.codePoint))
33
+ return true;
34
+ if (delimiterStartIndex <= startIndex)
35
+ return true;
36
+ const prevCodePosition = nodePoints[delimiterStartIndex - 1];
37
+ return (isUnicodeWhitespaceCharacter(prevCodePosition.codePoint) ||
38
+ isPunctuationCharacter(prevCodePosition.codePoint));
39
+ };
40
+ const isCloserDelimiter = (delimiterStartIndex, delimiterEndIndex) => {
41
+ if (delimiterStartIndex === blockStartIndex)
42
+ return false;
43
+ if (delimiterStartIndex === startIndex)
44
+ return true;
45
+ const prevCodePosition = nodePoints[delimiterStartIndex - 1];
46
+ if (isUnicodeWhitespaceCharacter(prevCodePosition.codePoint))
47
+ return false;
48
+ if (!isPunctuationCharacter(prevCodePosition.codePoint))
49
+ return true;
50
+ if (delimiterEndIndex >= endIndex)
51
+ return true;
52
+ const nextCodePosition = nodePoints[delimiterEndIndex];
53
+ return (isUnicodeWhitespaceCharacter(nextCodePosition.codePoint) ||
54
+ isPunctuationCharacter(nextCodePosition.codePoint));
55
+ };
56
+ for (let i = startIndex; i < endIndex; ++i) {
57
+ const c = nodePoints[i].codePoint;
58
+ switch (c) {
59
+ case AsciiCodePoint.BACKSLASH:
60
+ i += 1;
61
+ break;
62
+ case AsciiCodePoint.ASTERISK:
63
+ case AsciiCodePoint.UNDERSCORE: {
64
+ const _startIndex = i;
65
+ i = eatOptionalCharacters(nodePoints, i + 1, endIndex, c) - 1;
66
+ const _endIndex = i + 1;
67
+ const isLeftFlankingDelimiterRun = isOpenerDelimiter(_startIndex, _endIndex);
68
+ const isRightFlankingDelimiterRun = isCloserDelimiter(_startIndex, _endIndex);
69
+ let isOpener = isLeftFlankingDelimiterRun;
70
+ let isCloser = isRightFlankingDelimiterRun;
71
+ if (c === AsciiCodePoint.UNDERSCORE) {
72
+ if (isLeftFlankingDelimiterRun && isRightFlankingDelimiterRun) {
73
+ if (_startIndex > startIndex &&
74
+ !isPunctuationCharacter(nodePoints[_startIndex - 1].codePoint)) {
75
+ isOpener = false;
76
+ }
77
+ const nextCodePosition = nodePoints[_endIndex];
78
+ if (!isPunctuationCharacter(nextCodePosition.codePoint)) {
79
+ isCloser = false;
80
+ }
81
+ }
67
82
  }
83
+ if (!isOpener && !isCloser)
84
+ break;
85
+ const thickness = _endIndex - _startIndex;
86
+ return {
87
+ type: isOpener ? (isCloser ? 'both' : 'opener') : 'closer',
88
+ startIndex: _startIndex,
89
+ endIndex: _endIndex,
90
+ thickness,
91
+ originalThickness: thickness,
92
+ };
68
93
  }
69
94
  }
70
- if (!isOpener && !isCloser)
71
- break;
72
- const thickness = _endIndex - _startIndex;
73
- return {
74
- type: isOpener ? (isCloser ? 'both' : 'opener') : 'closer',
75
- startIndex: _startIndex,
76
- endIndex: _endIndex,
77
- thickness,
78
- originalThickness: thickness,
79
- };
80
95
  }
96
+ return null;
81
97
  }
82
- }
83
- return null;
84
- }
85
- isDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, nodePoints) {
86
- if (nodePoints[openerDelimiter.startIndex].codePoint !==
87
- nodePoints[closerDelimiter.startIndex].codePoint ||
88
- ((openerDelimiter.type === 'both' || closerDelimiter.type === 'both') &&
89
- (openerDelimiter.originalThickness +
90
- closerDelimiter.originalThickness) %
91
- 3 ===
92
- 0 &&
93
- openerDelimiter.originalThickness % 3 !== 0)) {
94
- return { paired: false, opener: true, closer: true };
95
- }
96
- return { paired: true };
97
- }
98
- processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, nodePoints, api) {
99
- let thickness = 1;
100
- if (openerDelimiter.thickness > 1 && closerDelimiter.thickness > 1) {
101
- thickness = 2;
102
- }
103
- internalTokens = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex, nodePoints);
104
- const token = {
105
- nodeType: thickness === 1 ? EmphasisType : StrongType,
106
- startIndex: openerDelimiter.endIndex - thickness,
107
- endIndex: closerDelimiter.startIndex + thickness,
108
- thickness,
109
- children: internalTokens,
110
- };
111
- const remainOpenerDelimiter = openerDelimiter.thickness > thickness
112
- ? {
113
- type: openerDelimiter.type,
114
- startIndex: openerDelimiter.startIndex,
115
- endIndex: openerDelimiter.endIndex - thickness,
116
- thickness: openerDelimiter.thickness - thickness,
117
- originalThickness: openerDelimiter.originalThickness,
98
+ function isDelimiterPair(openerDelimiter, closerDelimiter) {
99
+ const nodePoints = api.getNodePoints();
100
+ if (nodePoints[openerDelimiter.startIndex].codePoint !==
101
+ nodePoints[closerDelimiter.startIndex].codePoint ||
102
+ ((openerDelimiter.type === 'both' || closerDelimiter.type === 'both') &&
103
+ (openerDelimiter.originalThickness + closerDelimiter.originalThickness) % 3 === 0 &&
104
+ openerDelimiter.originalThickness % 3 !== 0)) {
105
+ return { paired: false, opener: true, closer: true };
106
+ }
107
+ return { paired: true };
118
108
  }
119
- : undefined;
120
- const remainCloserDelimiter = closerDelimiter.thickness > thickness
121
- ? {
122
- type: closerDelimiter.type,
123
- startIndex: closerDelimiter.startIndex + thickness,
124
- endIndex: closerDelimiter.endIndex,
125
- thickness: closerDelimiter.thickness - thickness,
126
- originalThickness: closerDelimiter.originalThickness,
109
+ function processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens) {
110
+ let thickness = 1;
111
+ if (openerDelimiter.thickness > 1 && closerDelimiter.thickness > 1) {
112
+ thickness = 2;
113
+ }
114
+ internalTokens = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
115
+ const token = {
116
+ nodeType: thickness === 1 ? EmphasisType : StrongType,
117
+ startIndex: openerDelimiter.endIndex - thickness,
118
+ endIndex: closerDelimiter.startIndex + thickness,
119
+ thickness,
120
+ children: internalTokens,
121
+ };
122
+ const remainOpenerDelimiter = openerDelimiter.thickness > thickness
123
+ ? {
124
+ type: openerDelimiter.type,
125
+ startIndex: openerDelimiter.startIndex,
126
+ endIndex: openerDelimiter.endIndex - thickness,
127
+ thickness: openerDelimiter.thickness - thickness,
128
+ originalThickness: openerDelimiter.originalThickness,
129
+ }
130
+ : undefined;
131
+ const remainCloserDelimiter = closerDelimiter.thickness > thickness
132
+ ? {
133
+ type: closerDelimiter.type,
134
+ startIndex: closerDelimiter.startIndex + thickness,
135
+ endIndex: closerDelimiter.endIndex,
136
+ thickness: closerDelimiter.thickness - thickness,
137
+ originalThickness: closerDelimiter.originalThickness,
138
+ }
139
+ : undefined;
140
+ return {
141
+ tokens: [token],
142
+ remainOpenerDelimiter,
143
+ remainCloserDelimiter,
144
+ };
127
145
  }
128
- : undefined;
129
- return {
130
- tokens: [token],
131
- remainOpenerDelimiter,
132
- remainCloserDelimiter,
133
146
  };
134
- }
135
- processToken(token, children) {
136
- const result = {
137
- type: token.nodeType,
138
- children: children !== null && children !== void 0 ? children : [],
139
- };
140
- return result;
147
+ this.parse = () => ({
148
+ parse: (token, children) => {
149
+ const result = {
150
+ type: token.nodeType,
151
+ children,
152
+ };
153
+ return result;
154
+ },
155
+ });
141
156
  }
142
157
  }
143
158
 
@@ -1,5 +1,3 @@
1
- import { EmphasisTokenizer } from './tokenizer';
2
- export { EmphasisTokenizer } from './tokenizer';
1
+ export { EmphasisTokenizer, EmphasisTokenizer as default } from './tokenizer';
3
2
  export { uniqueName as EmphasisTokenizerName } from './types';
4
- export type { Token as EmphasisToken, TokenizerProps as EmphasisTokenizerProps, } from './types';
5
- export default EmphasisTokenizer;
3
+ export type { IToken as IEmphasisToken, ITokenizerProps as IEmphasisTokenizerProps } from './types';
@@ -1,34 +1,14 @@
1
- import type { YastNode } from '@yozora/ast';
2
- import type { NodePoint } from '@yozora/character';
3
- import type { MatchInlinePhaseApi, ResultOfIsDelimiterPair, ResultOfProcessDelimiterPair, Tokenizer, TokenizerMatchInlineHook, TokenizerParseInlineHook, YastInlineToken } 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 Emphasis and Strong Emphasis.
8
6
  *
9
7
  * @see https://github.com/syntax-tree/mdast#strong
10
8
  * @see https://github.github.com/gfm/#emphasis-and-strong-emphasis
11
9
  */
12
- export declare class EmphasisTokenizer extends BaseInlineTokenizer<Delimiter> implements Tokenizer, TokenizerMatchInlineHook<T, Delimiter, Token>, TokenizerParseInlineHook<T, Token, Node> {
13
- constructor(props?: TokenizerProps);
14
- /**
15
- * @override
16
- * @see BaseInlineTokenizer
17
- */
18
- protected _findDelimiter(startIndex: number, endIndex: number, nodePoints: ReadonlyArray<NodePoint>): Delimiter | null;
19
- /**
20
- * @override
21
- * @see TokenizerMatchInlineHook
22
- */
23
- isDelimiterPair(openerDelimiter: Delimiter, closerDelimiter: Delimiter, internalTokens: ReadonlyArray<YastInlineToken>, nodePoints: ReadonlyArray<NodePoint>): ResultOfIsDelimiterPair;
24
- /**
25
- * @override
26
- * @see TokenizerMatchInlineHook
27
- */
28
- processDelimiterPair(openerDelimiter: Delimiter, closerDelimiter: Delimiter, internalTokens: ReadonlyArray<YastInlineToken>, nodePoints: ReadonlyArray<NodePoint>, api: Readonly<MatchInlinePhaseApi>): ResultOfProcessDelimiterPair<T, Token, Delimiter>;
29
- /**
30
- * @override
31
- * @see TokenizerParseInlineHook
32
- */
33
- processToken(token: Token, children?: YastNode[]): Node;
10
+ export declare class EmphasisTokenizer extends BaseInlineTokenizer<T, IDelimiter, IToken, INode> implements IInlineTokenizer<T, IDelimiter, IToken, INode> {
11
+ constructor(props?: ITokenizerProps);
12
+ readonly match: IMatchInlineHookCreator<T, IDelimiter, IToken>;
13
+ readonly parse: IParseInlineHookCreator<T, IToken, INode>;
34
14
  }
@@ -1,15 +1,15 @@
1
- import type { Emphasis, EmphasisType, Strong, StrongType } from '@yozora/ast';
2
- import type { BaseInlineTokenizerProps, PartialYastInlineToken, YastTokenDelimiter } from '@yozora/core-tokenizer';
1
+ import type { EmphasisType, IEmphasis, IStrong, StrongType } from '@yozora/ast';
2
+ import type { IBaseInlineTokenizerProps, IPartialYastInlineToken, IYastTokenDelimiter } from '@yozora/core-tokenizer';
3
3
  export declare type T = EmphasisType | StrongType;
4
- export declare type Node = Emphasis | Strong;
4
+ export declare type INode = IEmphasis | IStrong;
5
5
  export declare const uniqueName = "@yozora/tokenizer-emphasis";
6
- export interface Token extends PartialYastInlineToken<T> {
6
+ export interface IToken extends IPartialYastInlineToken<T> {
7
7
  /**
8
- * Delimiter thickness.
8
+ * IDelimiter thickness.
9
9
  */
10
10
  thickness: number;
11
11
  }
12
- export interface Delimiter extends YastTokenDelimiter {
12
+ export interface IDelimiter extends IYastTokenDelimiter {
13
13
  /**
14
14
  * Thickness of the delimiter.
15
15
  */
@@ -19,4 +19,4 @@ export interface Delimiter extends YastTokenDelimiter {
19
19
  */
20
20
  originalThickness: number;
21
21
  }
22
- export declare type TokenizerProps = Partial<BaseInlineTokenizerProps>;
22
+ export declare type ITokenizerProps = Partial<IBaseInlineTokenizerProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yozora/tokenizer-emphasis",
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
  }