@yozora/tokenizer-emphasis 1.3.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 -129
- package/lib/esm/index.js +135 -129
- package/lib/types/index.d.ts +2 -4
- package/lib/types/tokenizer.d.ts +6 -25
- package/lib/types/types.d.ts +7 -7
- package/package.json +5 -5
package/lib/cjs/index.js
CHANGED
|
@@ -15,145 +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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
+
}
|
|
79
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
|
+
};
|
|
80
97
|
}
|
|
81
98
|
}
|
|
82
|
-
if (!isOpener && !isCloser)
|
|
83
|
-
break;
|
|
84
|
-
const thickness = _endIndex - _startIndex;
|
|
85
|
-
return {
|
|
86
|
-
type: isOpener ? (isCloser ? 'both' : 'opener') : 'closer',
|
|
87
|
-
startIndex: _startIndex,
|
|
88
|
-
endIndex: _endIndex,
|
|
89
|
-
thickness,
|
|
90
|
-
originalThickness: thickness,
|
|
91
|
-
};
|
|
92
99
|
}
|
|
100
|
+
return null;
|
|
93
101
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
3 ===
|
|
105
|
-
0 &&
|
|
106
|
-
openerDelimiter.originalThickness % 3 !== 0)) {
|
|
107
|
-
return { paired: false, opener: true, closer: true };
|
|
108
|
-
}
|
|
109
|
-
return { paired: true };
|
|
110
|
-
}
|
|
111
|
-
processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, api) {
|
|
112
|
-
let thickness = 1;
|
|
113
|
-
if (openerDelimiter.thickness > 1 && closerDelimiter.thickness > 1) {
|
|
114
|
-
thickness = 2;
|
|
115
|
-
}
|
|
116
|
-
internalTokens = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
|
|
117
|
-
const token = {
|
|
118
|
-
nodeType: thickness === 1 ? ast.EmphasisType : ast.StrongType,
|
|
119
|
-
startIndex: openerDelimiter.endIndex - thickness,
|
|
120
|
-
endIndex: closerDelimiter.startIndex + thickness,
|
|
121
|
-
thickness,
|
|
122
|
-
children: internalTokens,
|
|
123
|
-
};
|
|
124
|
-
const remainOpenerDelimiter = openerDelimiter.thickness > thickness
|
|
125
|
-
? {
|
|
126
|
-
type: openerDelimiter.type,
|
|
127
|
-
startIndex: openerDelimiter.startIndex,
|
|
128
|
-
endIndex: openerDelimiter.endIndex - thickness,
|
|
129
|
-
thickness: openerDelimiter.thickness - thickness,
|
|
130
|
-
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 };
|
|
131
112
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
endIndex
|
|
138
|
-
|
|
139
|
-
|
|
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
|
+
};
|
|
140
149
|
}
|
|
141
|
-
: undefined;
|
|
142
|
-
return {
|
|
143
|
-
tokens: [token],
|
|
144
|
-
remainOpenerDelimiter,
|
|
145
|
-
remainCloserDelimiter,
|
|
146
150
|
};
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
151
|
+
this.parse = () => ({
|
|
152
|
+
parse: (token, children) => {
|
|
153
|
+
const result = {
|
|
154
|
+
type: token.nodeType,
|
|
155
|
+
children,
|
|
156
|
+
};
|
|
157
|
+
return result;
|
|
158
|
+
},
|
|
159
|
+
});
|
|
154
160
|
}
|
|
155
161
|
}
|
|
156
162
|
|
|
157
163
|
exports.EmphasisTokenizer = EmphasisTokenizer;
|
|
158
164
|
exports.EmphasisTokenizerName = uniqueName;
|
|
159
|
-
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,142 +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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
+
}
|
|
75
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
|
+
};
|
|
76
93
|
}
|
|
77
94
|
}
|
|
78
|
-
if (!isOpener && !isCloser)
|
|
79
|
-
break;
|
|
80
|
-
const thickness = _endIndex - _startIndex;
|
|
81
|
-
return {
|
|
82
|
-
type: isOpener ? (isCloser ? 'both' : 'opener') : 'closer',
|
|
83
|
-
startIndex: _startIndex,
|
|
84
|
-
endIndex: _endIndex,
|
|
85
|
-
thickness,
|
|
86
|
-
originalThickness: thickness,
|
|
87
|
-
};
|
|
88
95
|
}
|
|
96
|
+
return null;
|
|
89
97
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
3 ===
|
|
101
|
-
0 &&
|
|
102
|
-
openerDelimiter.originalThickness % 3 !== 0)) {
|
|
103
|
-
return { paired: false, opener: true, closer: true };
|
|
104
|
-
}
|
|
105
|
-
return { paired: true };
|
|
106
|
-
}
|
|
107
|
-
processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, api) {
|
|
108
|
-
let thickness = 1;
|
|
109
|
-
if (openerDelimiter.thickness > 1 && closerDelimiter.thickness > 1) {
|
|
110
|
-
thickness = 2;
|
|
111
|
-
}
|
|
112
|
-
internalTokens = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
|
|
113
|
-
const token = {
|
|
114
|
-
nodeType: thickness === 1 ? EmphasisType : StrongType,
|
|
115
|
-
startIndex: openerDelimiter.endIndex - thickness,
|
|
116
|
-
endIndex: closerDelimiter.startIndex + thickness,
|
|
117
|
-
thickness,
|
|
118
|
-
children: internalTokens,
|
|
119
|
-
};
|
|
120
|
-
const remainOpenerDelimiter = openerDelimiter.thickness > thickness
|
|
121
|
-
? {
|
|
122
|
-
type: openerDelimiter.type,
|
|
123
|
-
startIndex: openerDelimiter.startIndex,
|
|
124
|
-
endIndex: openerDelimiter.endIndex - thickness,
|
|
125
|
-
thickness: openerDelimiter.thickness - thickness,
|
|
126
|
-
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 };
|
|
127
108
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
endIndex
|
|
134
|
-
|
|
135
|
-
|
|
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
|
+
};
|
|
136
145
|
}
|
|
137
|
-
: undefined;
|
|
138
|
-
return {
|
|
139
|
-
tokens: [token],
|
|
140
|
-
remainOpenerDelimiter,
|
|
141
|
-
remainCloserDelimiter,
|
|
142
146
|
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
this.parse = () => ({
|
|
148
|
+
parse: (token, children) => {
|
|
149
|
+
const result = {
|
|
150
|
+
type: token.nodeType,
|
|
151
|
+
children,
|
|
152
|
+
};
|
|
153
|
+
return result;
|
|
154
|
+
},
|
|
155
|
+
});
|
|
150
156
|
}
|
|
151
157
|
}
|
|
152
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,33 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { MatchInlinePhaseApi, ResultOfIsDelimiterPair, ResultOfProcessDelimiterPair, Tokenizer, TokenizerMatchInlineHook, TokenizerParseInlineHook, YastInlineToken } 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 {
|
|
3
|
+
import type { IDelimiter, INode, IToken, ITokenizerProps, T } from './types';
|
|
5
4
|
/**
|
|
6
5
|
* Lexical Analyzer for Emphasis and Strong Emphasis.
|
|
7
6
|
*
|
|
8
7
|
* @see https://github.com/syntax-tree/mdast#strong
|
|
9
8
|
* @see https://github.github.com/gfm/#emphasis-and-strong-emphasis
|
|
10
9
|
*/
|
|
11
|
-
export declare class EmphasisTokenizer extends BaseInlineTokenizer<
|
|
12
|
-
constructor(props?:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* @see BaseInlineTokenizer
|
|
16
|
-
*/
|
|
17
|
-
protected _findDelimiter(startIndex: number, endIndex: number, api: Readonly<MatchInlinePhaseApi>): Delimiter | null;
|
|
18
|
-
/**
|
|
19
|
-
* @override
|
|
20
|
-
* @see TokenizerMatchInlineHook
|
|
21
|
-
*/
|
|
22
|
-
isDelimiterPair(openerDelimiter: Delimiter, closerDelimiter: Delimiter, internalTokens: ReadonlyArray<YastInlineToken>, api: Readonly<MatchInlinePhaseApi>): ResultOfIsDelimiterPair;
|
|
23
|
-
/**
|
|
24
|
-
* @override
|
|
25
|
-
* @see TokenizerMatchInlineHook
|
|
26
|
-
*/
|
|
27
|
-
processDelimiterPair(openerDelimiter: Delimiter, closerDelimiter: Delimiter, internalTokens: ReadonlyArray<YastInlineToken>, api: Readonly<MatchInlinePhaseApi>): ResultOfProcessDelimiterPair<T, Token, Delimiter>;
|
|
28
|
-
/**
|
|
29
|
-
* @override
|
|
30
|
-
* @see TokenizerParseInlineHook
|
|
31
|
-
*/
|
|
32
|
-
parseInline(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>;
|
|
33
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
|
}
|