@yozora/tokenizer-link 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 +114 -106
- package/lib/esm/index.js +114 -106
- package/lib/types/index.d.ts +2 -4
- package/lib/types/tokenizer.d.ts +6 -31
- package/lib/types/types.d.ts +12 -12
- package/lib/types/util/check-brackets.d.ts +3 -3
- package/lib/types/util/link-destination.d.ts +2 -2
- package/lib/types/util/link-title.d.ts +2 -2
- package/package.json +5 -5
package/lib/cjs/index.js
CHANGED
|
@@ -2,11 +2,9 @@
|
|
|
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');
|
|
7
6
|
var coreTokenizer = require('@yozora/core-tokenizer');
|
|
8
|
-
|
|
9
|
-
const uniqueName = '@yozora/tokenizer-link';
|
|
7
|
+
var ast = require('@yozora/ast');
|
|
10
8
|
|
|
11
9
|
const checkBalancedBracketsStatus = (startIndex, endIndex, internalTokens, nodePoints) => {
|
|
12
10
|
let i = startIndex;
|
|
@@ -153,6 +151,8 @@ function eatLinkTitle(nodePoints, startIndex, endIndex) {
|
|
|
153
151
|
return -1;
|
|
154
152
|
}
|
|
155
153
|
|
|
154
|
+
const uniqueName = '@yozora/tokenizer-link';
|
|
155
|
+
|
|
156
156
|
class LinkTokenizer extends coreTokenizer.BaseInlineTokenizer {
|
|
157
157
|
constructor(props = {}) {
|
|
158
158
|
var _a, _b;
|
|
@@ -160,119 +160,127 @@ class LinkTokenizer extends coreTokenizer.BaseInlineTokenizer {
|
|
|
160
160
|
name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
|
|
161
161
|
priority: (_b = props.priority) !== null && _b !== void 0 ? _b : coreTokenizer.TokenizerPriority.LINKS,
|
|
162
162
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
163
|
+
this.match = api => {
|
|
164
|
+
return {
|
|
165
|
+
findDelimiter: () => coreTokenizer.genFindDelimiter(_findDelimiter),
|
|
166
|
+
isDelimiterPair,
|
|
167
|
+
processDelimiterPair,
|
|
168
|
+
};
|
|
169
|
+
function _findDelimiter(startIndex, endIndex) {
|
|
170
|
+
const nodePoints = api.getNodePoints();
|
|
171
|
+
const blockEndIndex = api.getBlockEndIndex();
|
|
172
|
+
for (let i = startIndex; i < endIndex; ++i) {
|
|
173
|
+
const p = nodePoints[i];
|
|
174
|
+
switch (p.codePoint) {
|
|
175
|
+
case character.AsciiCodePoint.BACKSLASH:
|
|
176
|
+
i += 1;
|
|
177
|
+
break;
|
|
178
|
+
case character.AsciiCodePoint.OPEN_BRACKET: {
|
|
179
|
+
const delimiter = {
|
|
180
|
+
type: 'opener',
|
|
181
|
+
startIndex: i,
|
|
182
|
+
endIndex: i + 1,
|
|
183
|
+
};
|
|
184
|
+
return delimiter;
|
|
185
|
+
}
|
|
186
|
+
case character.AsciiCodePoint.CLOSE_BRACKET: {
|
|
187
|
+
if (i + 1 >= endIndex ||
|
|
188
|
+
nodePoints[i + 1].codePoint !== character.AsciiCodePoint.OPEN_PARENTHESIS)
|
|
189
|
+
break;
|
|
190
|
+
const destinationStartIndex = coreTokenizer.eatOptionalWhitespaces(nodePoints, i + 2, blockEndIndex);
|
|
191
|
+
const destinationEndIndex = eatLinkDestination(nodePoints, destinationStartIndex, blockEndIndex);
|
|
192
|
+
if (destinationEndIndex < 0)
|
|
193
|
+
break;
|
|
194
|
+
const titleStartIndex = coreTokenizer.eatOptionalWhitespaces(nodePoints, destinationEndIndex, blockEndIndex);
|
|
195
|
+
const titleEndIndex = eatLinkTitle(nodePoints, titleStartIndex, blockEndIndex);
|
|
196
|
+
if (titleEndIndex < 0)
|
|
197
|
+
break;
|
|
198
|
+
const _startIndex = i;
|
|
199
|
+
const _endIndex = coreTokenizer.eatOptionalWhitespaces(nodePoints, titleEndIndex, blockEndIndex) + 1;
|
|
200
|
+
if (_endIndex > blockEndIndex ||
|
|
201
|
+
nodePoints[_endIndex - 1].codePoint !== character.AsciiCodePoint.CLOSE_PARENTHESIS)
|
|
202
|
+
break;
|
|
203
|
+
return {
|
|
204
|
+
type: 'closer',
|
|
205
|
+
startIndex: _startIndex,
|
|
206
|
+
endIndex: _endIndex,
|
|
207
|
+
destinationContent: destinationStartIndex < destinationEndIndex
|
|
208
|
+
? {
|
|
209
|
+
startIndex: destinationStartIndex,
|
|
210
|
+
endIndex: destinationEndIndex,
|
|
211
|
+
}
|
|
212
|
+
: undefined,
|
|
213
|
+
titleContent: titleStartIndex < titleEndIndex
|
|
214
|
+
? { startIndex: titleStartIndex, endIndex: titleEndIndex }
|
|
215
|
+
: undefined,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
180
219
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
character.AsciiCodePoint.CLOSE_PARENTHESIS)
|
|
198
|
-
break;
|
|
199
|
-
return {
|
|
200
|
-
type: 'closer',
|
|
201
|
-
startIndex: _startIndex,
|
|
202
|
-
endIndex: _endIndex,
|
|
203
|
-
destinationContent: destinationStartIndex < destinationEndIndex
|
|
204
|
-
? {
|
|
205
|
-
startIndex: destinationStartIndex,
|
|
206
|
-
endIndex: destinationEndIndex,
|
|
207
|
-
}
|
|
208
|
-
: undefined,
|
|
209
|
-
titleContent: titleStartIndex < titleEndIndex
|
|
210
|
-
? { startIndex: titleStartIndex, endIndex: titleEndIndex }
|
|
211
|
-
: undefined,
|
|
212
|
-
};
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
function isDelimiterPair(openerDelimiter, closerDelimiter, internalTokens) {
|
|
223
|
+
const nodePoints = api.getNodePoints();
|
|
224
|
+
const hasInternalLinkToken = internalTokens.find(coreTokenizer.isLinkToken) != null;
|
|
225
|
+
if (hasInternalLinkToken) {
|
|
226
|
+
return { paired: false, opener: false, closer: false };
|
|
227
|
+
}
|
|
228
|
+
const balancedBracketsStatus = checkBalancedBracketsStatus(openerDelimiter.endIndex, closerDelimiter.startIndex, internalTokens, nodePoints);
|
|
229
|
+
switch (balancedBracketsStatus) {
|
|
230
|
+
case -1:
|
|
231
|
+
return { paired: false, opener: false, closer: true };
|
|
232
|
+
case 0:
|
|
233
|
+
return { paired: true };
|
|
234
|
+
case 1:
|
|
235
|
+
return { paired: false, opener: true, closer: false };
|
|
213
236
|
}
|
|
214
237
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
case -1:
|
|
227
|
-
return { paired: false, opener: false, closer: true };
|
|
228
|
-
case 0:
|
|
229
|
-
return { paired: true };
|
|
230
|
-
case 1:
|
|
231
|
-
return { paired: false, opener: true, closer: false };
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, api) {
|
|
235
|
-
const children = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
|
|
236
|
-
const token = {
|
|
237
|
-
nodeType: ast.LinkType,
|
|
238
|
-
startIndex: openerDelimiter.startIndex,
|
|
239
|
-
endIndex: closerDelimiter.endIndex,
|
|
240
|
-
destinationContent: closerDelimiter.destinationContent,
|
|
241
|
-
titleContent: closerDelimiter.titleContent,
|
|
242
|
-
children,
|
|
243
|
-
};
|
|
244
|
-
return { tokens: [token] };
|
|
245
|
-
}
|
|
246
|
-
parseInline(token, children, api) {
|
|
247
|
-
const nodePoints = api.getNodePoints();
|
|
248
|
-
let url = '';
|
|
249
|
-
if (token.destinationContent != null) {
|
|
250
|
-
let { startIndex, endIndex } = token.destinationContent;
|
|
251
|
-
if (nodePoints[startIndex].codePoint === character.AsciiCodePoint.OPEN_ANGLE) {
|
|
252
|
-
startIndex += 1;
|
|
253
|
-
endIndex -= 1;
|
|
238
|
+
function processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens) {
|
|
239
|
+
const children = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
|
|
240
|
+
const token = {
|
|
241
|
+
nodeType: ast.LinkType,
|
|
242
|
+
startIndex: openerDelimiter.startIndex,
|
|
243
|
+
endIndex: closerDelimiter.endIndex,
|
|
244
|
+
destinationContent: closerDelimiter.destinationContent,
|
|
245
|
+
titleContent: closerDelimiter.titleContent,
|
|
246
|
+
children,
|
|
247
|
+
};
|
|
248
|
+
return { tokens: [token] };
|
|
254
249
|
}
|
|
255
|
-
const destination = character.calcEscapedStringFromNodePoints(nodePoints, startIndex, endIndex, true);
|
|
256
|
-
url = coreTokenizer.encodeLinkDestination(destination);
|
|
257
|
-
}
|
|
258
|
-
let title;
|
|
259
|
-
if (token.titleContent != null) {
|
|
260
|
-
const { startIndex, endIndex } = token.titleContent;
|
|
261
|
-
title = character.calcEscapedStringFromNodePoints(nodePoints, startIndex + 1, endIndex - 1);
|
|
262
|
-
}
|
|
263
|
-
const result = {
|
|
264
|
-
type: ast.LinkType,
|
|
265
|
-
url,
|
|
266
|
-
title,
|
|
267
|
-
children,
|
|
268
250
|
};
|
|
269
|
-
|
|
251
|
+
this.parse = api => ({
|
|
252
|
+
parse: (token, children) => {
|
|
253
|
+
const nodePoints = api.getNodePoints();
|
|
254
|
+
let url = '';
|
|
255
|
+
if (token.destinationContent != null) {
|
|
256
|
+
let { startIndex, endIndex } = token.destinationContent;
|
|
257
|
+
if (nodePoints[startIndex].codePoint === character.AsciiCodePoint.OPEN_ANGLE) {
|
|
258
|
+
startIndex += 1;
|
|
259
|
+
endIndex -= 1;
|
|
260
|
+
}
|
|
261
|
+
const destination = character.calcEscapedStringFromNodePoints(nodePoints, startIndex, endIndex, true);
|
|
262
|
+
url = coreTokenizer.encodeLinkDestination(destination);
|
|
263
|
+
}
|
|
264
|
+
let title;
|
|
265
|
+
if (token.titleContent != null) {
|
|
266
|
+
const { startIndex, endIndex } = token.titleContent;
|
|
267
|
+
title = character.calcEscapedStringFromNodePoints(nodePoints, startIndex + 1, endIndex - 1);
|
|
268
|
+
}
|
|
269
|
+
const result = {
|
|
270
|
+
type: ast.LinkType,
|
|
271
|
+
url,
|
|
272
|
+
title,
|
|
273
|
+
children,
|
|
274
|
+
};
|
|
275
|
+
return result;
|
|
276
|
+
},
|
|
277
|
+
});
|
|
270
278
|
}
|
|
271
279
|
}
|
|
272
280
|
|
|
273
281
|
exports.LinkTokenizer = LinkTokenizer;
|
|
274
282
|
exports.LinkTokenizerName = uniqueName;
|
|
275
283
|
exports.checkBalancedBracketsStatus = checkBalancedBracketsStatus;
|
|
276
|
-
exports[
|
|
284
|
+
exports["default"] = LinkTokenizer;
|
|
277
285
|
exports.eatLinkDestination = eatLinkDestination;
|
|
278
286
|
exports.eatLinkTitle = eatLinkTitle;
|
package/lib/esm/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { LinkType } from '@yozora/ast';
|
|
2
1
|
import { AsciiCodePoint, isWhitespaceCharacter, isAsciiControlCharacter, VirtualCodePoint, calcEscapedStringFromNodePoints } from '@yozora/character';
|
|
3
|
-
import { eatOptionalBlankLines, BaseInlineTokenizer, TokenizerPriority, eatOptionalWhitespaces, isLinkToken, encodeLinkDestination } from '@yozora/core-tokenizer';
|
|
4
|
-
|
|
5
|
-
const uniqueName = '@yozora/tokenizer-link';
|
|
2
|
+
import { eatOptionalBlankLines, BaseInlineTokenizer, TokenizerPriority, genFindDelimiter, eatOptionalWhitespaces, isLinkToken, encodeLinkDestination } from '@yozora/core-tokenizer';
|
|
3
|
+
import { LinkType } from '@yozora/ast';
|
|
6
4
|
|
|
7
5
|
const checkBalancedBracketsStatus = (startIndex, endIndex, internalTokens, nodePoints) => {
|
|
8
6
|
let i = startIndex;
|
|
@@ -149,6 +147,8 @@ function eatLinkTitle(nodePoints, startIndex, endIndex) {
|
|
|
149
147
|
return -1;
|
|
150
148
|
}
|
|
151
149
|
|
|
150
|
+
const uniqueName = '@yozora/tokenizer-link';
|
|
151
|
+
|
|
152
152
|
class LinkTokenizer extends BaseInlineTokenizer {
|
|
153
153
|
constructor(props = {}) {
|
|
154
154
|
var _a, _b;
|
|
@@ -156,113 +156,121 @@ class LinkTokenizer extends BaseInlineTokenizer {
|
|
|
156
156
|
name: (_a = props.name) !== null && _a !== void 0 ? _a : uniqueName,
|
|
157
157
|
priority: (_b = props.priority) !== null && _b !== void 0 ? _b : TokenizerPriority.LINKS,
|
|
158
158
|
});
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
159
|
+
this.match = api => {
|
|
160
|
+
return {
|
|
161
|
+
findDelimiter: () => genFindDelimiter(_findDelimiter),
|
|
162
|
+
isDelimiterPair,
|
|
163
|
+
processDelimiterPair,
|
|
164
|
+
};
|
|
165
|
+
function _findDelimiter(startIndex, endIndex) {
|
|
166
|
+
const nodePoints = api.getNodePoints();
|
|
167
|
+
const blockEndIndex = api.getBlockEndIndex();
|
|
168
|
+
for (let i = startIndex; i < endIndex; ++i) {
|
|
169
|
+
const p = nodePoints[i];
|
|
170
|
+
switch (p.codePoint) {
|
|
171
|
+
case AsciiCodePoint.BACKSLASH:
|
|
172
|
+
i += 1;
|
|
173
|
+
break;
|
|
174
|
+
case AsciiCodePoint.OPEN_BRACKET: {
|
|
175
|
+
const delimiter = {
|
|
176
|
+
type: 'opener',
|
|
177
|
+
startIndex: i,
|
|
178
|
+
endIndex: i + 1,
|
|
179
|
+
};
|
|
180
|
+
return delimiter;
|
|
181
|
+
}
|
|
182
|
+
case AsciiCodePoint.CLOSE_BRACKET: {
|
|
183
|
+
if (i + 1 >= endIndex ||
|
|
184
|
+
nodePoints[i + 1].codePoint !== AsciiCodePoint.OPEN_PARENTHESIS)
|
|
185
|
+
break;
|
|
186
|
+
const destinationStartIndex = eatOptionalWhitespaces(nodePoints, i + 2, blockEndIndex);
|
|
187
|
+
const destinationEndIndex = eatLinkDestination(nodePoints, destinationStartIndex, blockEndIndex);
|
|
188
|
+
if (destinationEndIndex < 0)
|
|
189
|
+
break;
|
|
190
|
+
const titleStartIndex = eatOptionalWhitespaces(nodePoints, destinationEndIndex, blockEndIndex);
|
|
191
|
+
const titleEndIndex = eatLinkTitle(nodePoints, titleStartIndex, blockEndIndex);
|
|
192
|
+
if (titleEndIndex < 0)
|
|
193
|
+
break;
|
|
194
|
+
const _startIndex = i;
|
|
195
|
+
const _endIndex = eatOptionalWhitespaces(nodePoints, titleEndIndex, blockEndIndex) + 1;
|
|
196
|
+
if (_endIndex > blockEndIndex ||
|
|
197
|
+
nodePoints[_endIndex - 1].codePoint !== AsciiCodePoint.CLOSE_PARENTHESIS)
|
|
198
|
+
break;
|
|
199
|
+
return {
|
|
200
|
+
type: 'closer',
|
|
201
|
+
startIndex: _startIndex,
|
|
202
|
+
endIndex: _endIndex,
|
|
203
|
+
destinationContent: destinationStartIndex < destinationEndIndex
|
|
204
|
+
? {
|
|
205
|
+
startIndex: destinationStartIndex,
|
|
206
|
+
endIndex: destinationEndIndex,
|
|
207
|
+
}
|
|
208
|
+
: undefined,
|
|
209
|
+
titleContent: titleStartIndex < titleEndIndex
|
|
210
|
+
? { startIndex: titleStartIndex, endIndex: titleEndIndex }
|
|
211
|
+
: undefined,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
176
215
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
AsciiCodePoint.CLOSE_PARENTHESIS)
|
|
194
|
-
break;
|
|
195
|
-
return {
|
|
196
|
-
type: 'closer',
|
|
197
|
-
startIndex: _startIndex,
|
|
198
|
-
endIndex: _endIndex,
|
|
199
|
-
destinationContent: destinationStartIndex < destinationEndIndex
|
|
200
|
-
? {
|
|
201
|
-
startIndex: destinationStartIndex,
|
|
202
|
-
endIndex: destinationEndIndex,
|
|
203
|
-
}
|
|
204
|
-
: undefined,
|
|
205
|
-
titleContent: titleStartIndex < titleEndIndex
|
|
206
|
-
? { startIndex: titleStartIndex, endIndex: titleEndIndex }
|
|
207
|
-
: undefined,
|
|
208
|
-
};
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
function isDelimiterPair(openerDelimiter, closerDelimiter, internalTokens) {
|
|
219
|
+
const nodePoints = api.getNodePoints();
|
|
220
|
+
const hasInternalLinkToken = internalTokens.find(isLinkToken) != null;
|
|
221
|
+
if (hasInternalLinkToken) {
|
|
222
|
+
return { paired: false, opener: false, closer: false };
|
|
223
|
+
}
|
|
224
|
+
const balancedBracketsStatus = checkBalancedBracketsStatus(openerDelimiter.endIndex, closerDelimiter.startIndex, internalTokens, nodePoints);
|
|
225
|
+
switch (balancedBracketsStatus) {
|
|
226
|
+
case -1:
|
|
227
|
+
return { paired: false, opener: false, closer: true };
|
|
228
|
+
case 0:
|
|
229
|
+
return { paired: true };
|
|
230
|
+
case 1:
|
|
231
|
+
return { paired: false, opener: true, closer: false };
|
|
209
232
|
}
|
|
210
233
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
case -1:
|
|
223
|
-
return { paired: false, opener: false, closer: true };
|
|
224
|
-
case 0:
|
|
225
|
-
return { paired: true };
|
|
226
|
-
case 1:
|
|
227
|
-
return { paired: false, opener: true, closer: false };
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens, api) {
|
|
231
|
-
const children = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
|
|
232
|
-
const token = {
|
|
233
|
-
nodeType: LinkType,
|
|
234
|
-
startIndex: openerDelimiter.startIndex,
|
|
235
|
-
endIndex: closerDelimiter.endIndex,
|
|
236
|
-
destinationContent: closerDelimiter.destinationContent,
|
|
237
|
-
titleContent: closerDelimiter.titleContent,
|
|
238
|
-
children,
|
|
239
|
-
};
|
|
240
|
-
return { tokens: [token] };
|
|
241
|
-
}
|
|
242
|
-
parseInline(token, children, api) {
|
|
243
|
-
const nodePoints = api.getNodePoints();
|
|
244
|
-
let url = '';
|
|
245
|
-
if (token.destinationContent != null) {
|
|
246
|
-
let { startIndex, endIndex } = token.destinationContent;
|
|
247
|
-
if (nodePoints[startIndex].codePoint === AsciiCodePoint.OPEN_ANGLE) {
|
|
248
|
-
startIndex += 1;
|
|
249
|
-
endIndex -= 1;
|
|
234
|
+
function processDelimiterPair(openerDelimiter, closerDelimiter, internalTokens) {
|
|
235
|
+
const children = api.resolveInternalTokens(internalTokens, openerDelimiter.endIndex, closerDelimiter.startIndex);
|
|
236
|
+
const token = {
|
|
237
|
+
nodeType: LinkType,
|
|
238
|
+
startIndex: openerDelimiter.startIndex,
|
|
239
|
+
endIndex: closerDelimiter.endIndex,
|
|
240
|
+
destinationContent: closerDelimiter.destinationContent,
|
|
241
|
+
titleContent: closerDelimiter.titleContent,
|
|
242
|
+
children,
|
|
243
|
+
};
|
|
244
|
+
return { tokens: [token] };
|
|
250
245
|
}
|
|
251
|
-
const destination = calcEscapedStringFromNodePoints(nodePoints, startIndex, endIndex, true);
|
|
252
|
-
url = encodeLinkDestination(destination);
|
|
253
|
-
}
|
|
254
|
-
let title;
|
|
255
|
-
if (token.titleContent != null) {
|
|
256
|
-
const { startIndex, endIndex } = token.titleContent;
|
|
257
|
-
title = calcEscapedStringFromNodePoints(nodePoints, startIndex + 1, endIndex - 1);
|
|
258
|
-
}
|
|
259
|
-
const result = {
|
|
260
|
-
type: LinkType,
|
|
261
|
-
url,
|
|
262
|
-
title,
|
|
263
|
-
children,
|
|
264
246
|
};
|
|
265
|
-
|
|
247
|
+
this.parse = api => ({
|
|
248
|
+
parse: (token, children) => {
|
|
249
|
+
const nodePoints = api.getNodePoints();
|
|
250
|
+
let url = '';
|
|
251
|
+
if (token.destinationContent != null) {
|
|
252
|
+
let { startIndex, endIndex } = token.destinationContent;
|
|
253
|
+
if (nodePoints[startIndex].codePoint === AsciiCodePoint.OPEN_ANGLE) {
|
|
254
|
+
startIndex += 1;
|
|
255
|
+
endIndex -= 1;
|
|
256
|
+
}
|
|
257
|
+
const destination = calcEscapedStringFromNodePoints(nodePoints, startIndex, endIndex, true);
|
|
258
|
+
url = encodeLinkDestination(destination);
|
|
259
|
+
}
|
|
260
|
+
let title;
|
|
261
|
+
if (token.titleContent != null) {
|
|
262
|
+
const { startIndex, endIndex } = token.titleContent;
|
|
263
|
+
title = calcEscapedStringFromNodePoints(nodePoints, startIndex + 1, endIndex - 1);
|
|
264
|
+
}
|
|
265
|
+
const result = {
|
|
266
|
+
type: LinkType,
|
|
267
|
+
url,
|
|
268
|
+
title,
|
|
269
|
+
children,
|
|
270
|
+
};
|
|
271
|
+
return result;
|
|
272
|
+
},
|
|
273
|
+
});
|
|
266
274
|
}
|
|
267
275
|
}
|
|
268
276
|
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { LinkTokenizer } from './tokenizer';
|
|
2
1
|
export * from './util/check-brackets';
|
|
3
2
|
export * from './util/link-destination';
|
|
4
3
|
export * from './util/link-title';
|
|
5
|
-
export { LinkTokenizer } from './tokenizer';
|
|
4
|
+
export { LinkTokenizer, LinkTokenizer as default } from './tokenizer';
|
|
6
5
|
export { uniqueName as LinkTokenizerName } from './types';
|
|
7
|
-
export type {
|
|
8
|
-
export default LinkTokenizer;
|
|
6
|
+
export type { IToken as ILinkToken, ITokenizerProps as ILinkTokenizerProps } from './types';
|
package/lib/types/tokenizer.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { MatchInlinePhaseApi, ParseInlinePhaseApi, 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 InlineLink.
|
|
7
6
|
*
|
|
@@ -32,32 +31,8 @@ import type { Delimiter, Node, T, Token, TokenizerProps } from './types';
|
|
|
32
31
|
* @see https://github.com/syntax-tree/mdast#link
|
|
33
32
|
* @see https://github.github.com/gfm/#links
|
|
34
33
|
*/
|
|
35
|
-
export declare class LinkTokenizer extends BaseInlineTokenizer<
|
|
36
|
-
constructor(props?:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* parenthesis '(', optional whitespace, an optional link destination, an
|
|
40
|
-
* optional link title separated from the link destination by whitespace,
|
|
41
|
-
* optional whitespace, and a right parenthesis ')'
|
|
42
|
-
* @see https://github.github.com/gfm/#inline-link
|
|
43
|
-
*
|
|
44
|
-
* @override
|
|
45
|
-
* @see TokenizerMatchInlineHook
|
|
46
|
-
*/
|
|
47
|
-
protected _findDelimiter(startIndex: number, endIndex: number, api: Readonly<MatchInlinePhaseApi>): Delimiter | null;
|
|
48
|
-
/**
|
|
49
|
-
* @override
|
|
50
|
-
* @see TokenizerMatchInlineHook
|
|
51
|
-
*/
|
|
52
|
-
isDelimiterPair(openerDelimiter: Delimiter, closerDelimiter: Delimiter, internalTokens: ReadonlyArray<YastInlineToken>, api: Readonly<MatchInlinePhaseApi>): ResultOfIsDelimiterPair;
|
|
53
|
-
/**
|
|
54
|
-
* @override
|
|
55
|
-
* @see TokenizerMatchInlineHook
|
|
56
|
-
*/
|
|
57
|
-
processDelimiterPair(openerDelimiter: Delimiter, closerDelimiter: Delimiter, internalTokens: ReadonlyArray<YastInlineToken>, api: Readonly<MatchInlinePhaseApi>): ResultOfProcessDelimiterPair<T, Token, Delimiter>;
|
|
58
|
-
/**
|
|
59
|
-
* @override
|
|
60
|
-
* @see TokenizerParseInlineHook
|
|
61
|
-
*/
|
|
62
|
-
parseInline(token: Token, children: YastNode[], api: Readonly<ParseInlinePhaseApi>): Node;
|
|
34
|
+
export declare class LinkTokenizer extends BaseInlineTokenizer<T, IDelimiter, IToken, INode> implements IInlineTokenizer<T, IDelimiter, IToken, INode> {
|
|
35
|
+
constructor(props?: ITokenizerProps);
|
|
36
|
+
readonly match: IMatchInlineHookCreator<T, IDelimiter, IToken>;
|
|
37
|
+
readonly parse: IParseInlineHookCreator<T, IToken, INode>;
|
|
63
38
|
}
|
package/lib/types/types.d.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
1
|
+
import type { ILink, LinkType } from '@yozora/ast';
|
|
2
|
+
import type { INodeInterval } from '@yozora/character';
|
|
3
|
+
import type { IBaseInlineTokenizerProps, IPartialYastInlineToken, IYastTokenDelimiter } from '@yozora/core-tokenizer';
|
|
4
4
|
export declare type T = LinkType;
|
|
5
|
-
export declare type
|
|
5
|
+
export declare type INode = ILink;
|
|
6
6
|
export declare const uniqueName = "@yozora/tokenizer-link";
|
|
7
|
-
export interface
|
|
7
|
+
export interface IToken extends IPartialYastInlineToken<T> {
|
|
8
8
|
/**
|
|
9
9
|
* Link destination interval.
|
|
10
10
|
*/
|
|
11
|
-
destinationContent?:
|
|
11
|
+
destinationContent?: INodeInterval;
|
|
12
12
|
/**
|
|
13
13
|
* Link title interval.
|
|
14
14
|
*/
|
|
15
|
-
titleContent?:
|
|
15
|
+
titleContent?: INodeInterval;
|
|
16
16
|
}
|
|
17
|
-
export interface
|
|
17
|
+
export interface IDelimiter extends IYastTokenDelimiter {
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* IDelimiter type.
|
|
20
20
|
*/
|
|
21
21
|
type: 'opener' | 'closer';
|
|
22
22
|
/**
|
|
23
23
|
* Link destination interval.
|
|
24
24
|
*/
|
|
25
|
-
destinationContent?:
|
|
25
|
+
destinationContent?: INodeInterval;
|
|
26
26
|
/**
|
|
27
27
|
* Link title interval.
|
|
28
28
|
*/
|
|
29
|
-
titleContent?:
|
|
29
|
+
titleContent?: INodeInterval;
|
|
30
30
|
}
|
|
31
|
-
export declare type
|
|
31
|
+
export declare type ITokenizerProps = Partial<IBaseInlineTokenizerProps>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { INodePoint } from '@yozora/character';
|
|
2
|
+
import type { IYastInlineToken } from '@yozora/core-tokenizer';
|
|
3
3
|
/**
|
|
4
4
|
* The link text may contain balanced brackets, but not unbalanced ones,
|
|
5
5
|
* unless they are escaped
|
|
@@ -9,4 +9,4 @@ import type { YastInlineToken } from '@yozora/core-tokenizer';
|
|
|
9
9
|
* @see https://github.github.com/gfm/#example-522
|
|
10
10
|
* @see https://github.github.com/gfm/#example-523
|
|
11
11
|
*/
|
|
12
|
-
export declare const checkBalancedBracketsStatus: (startIndex: number, endIndex: number, internalTokens: ReadonlyArray<
|
|
12
|
+
export declare const checkBalancedBracketsStatus: (startIndex: number, endIndex: number, internalTokens: ReadonlyArray<IYastInlineToken>, nodePoints: ReadonlyArray<INodePoint>) => -1 | 0 | 1;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { INodePoint } from '@yozora/character';
|
|
2
2
|
/**
|
|
3
3
|
* A link destination consists of either
|
|
4
4
|
* - a sequence of zero or more characters between an opening '<' and a closing '>'
|
|
@@ -12,4 +12,4 @@ import type { NodePoint } from '@yozora/character';
|
|
|
12
12
|
* @see https://github.github.com/gfm/#link-destination
|
|
13
13
|
* @return position at next iteration
|
|
14
14
|
*/
|
|
15
|
-
export declare function eatLinkDestination(nodePoints: ReadonlyArray<
|
|
15
|
+
export declare function eatLinkDestination(nodePoints: ReadonlyArray<INodePoint>, startIndex: number, endIndex: number): number;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { INodePoint } from '@yozora/character';
|
|
2
2
|
/**
|
|
3
3
|
* A link title consists of either
|
|
4
4
|
*
|
|
@@ -11,4 +11,4 @@ import type { NodePoint } from '@yozora/character';
|
|
|
11
11
|
* - a sequence of zero or more characters between matching parentheses '(...)',
|
|
12
12
|
* including a '(' or ')' character only if it is backslash-escaped.
|
|
13
13
|
*/
|
|
14
|
-
export declare function eatLinkTitle(nodePoints: ReadonlyArray<
|
|
14
|
+
export declare function eatLinkTitle(nodePoints: ReadonlyArray<INodePoint>, startIndex: number, endIndex: number): number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yozora/tokenizer-link",
|
|
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
|
}
|