@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 +135 -120
- package/lib/esm/index.js +135 -120
- package/lib/types/index.d.ts +2 -4
- package/lib/types/tokenizer.d.ts +6 -26
- package/lib/types/types.d.ts +7 -7
- package/package.json +5 -5
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
endIndex
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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[
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
endIndex
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export { EmphasisTokenizer } from './tokenizer';
|
|
1
|
+
export { EmphasisTokenizer, EmphasisTokenizer as default } from './tokenizer';
|
|
3
2
|
export { uniqueName as EmphasisTokenizerName } from './types';
|
|
4
|
-
export type {
|
|
5
|
-
export default EmphasisTokenizer;
|
|
3
|
+
export type { IToken as IEmphasisToken, ITokenizerProps as IEmphasisTokenizerProps } from './types';
|
package/lib/types/tokenizer.d.ts
CHANGED
|
@@ -1,34 +1,14 @@
|
|
|
1
|
-
import type {
|
|
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 {
|
|
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<
|
|
13
|
-
constructor(props?:
|
|
14
|
-
|
|
15
|
-
|
|
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
|
}
|
package/lib/types/types.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
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
|
|
4
|
+
export declare type INode = IEmphasis | IStrong;
|
|
5
5
|
export declare const uniqueName = "@yozora/tokenizer-emphasis";
|
|
6
|
-
export interface
|
|
6
|
+
export interface IToken extends IPartialYastInlineToken<T> {
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* IDelimiter thickness.
|
|
9
9
|
*/
|
|
10
10
|
thickness: number;
|
|
11
11
|
}
|
|
12
|
-
export interface
|
|
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
|
|
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": "
|
|
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": "^
|
|
39
|
-
"@yozora/character": "^
|
|
40
|
-
"@yozora/core-tokenizer": "^
|
|
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": "
|
|
42
|
+
"gitHead": "0171501339c49ffd02ed16a63447fa20a47a29a7"
|
|
43
43
|
}
|