@peaceroad/markdown-it-cjk-breaks-mod 0.1.3 → 0.1.4
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/index.js +50 -30
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -55,10 +55,15 @@ function resolve_punctuation_targets(opts) {
|
|
|
55
55
|
if (!opts) return DEFAULT_PUNCTUATION_CONFIG;
|
|
56
56
|
|
|
57
57
|
var hasCustomTargets = Object.prototype.hasOwnProperty.call(opts, 'spaceAfterPunctuationTargets');
|
|
58
|
+
var addTargets = opts.spaceAfterPunctuationTargetsAdd;
|
|
59
|
+
var removeTargets = opts.spaceAfterPunctuationTargetsRemove;
|
|
60
|
+
if (!hasCustomTargets && addTargets === undefined && removeTargets === undefined) {
|
|
61
|
+
return DEFAULT_PUNCTUATION_CONFIG;
|
|
62
|
+
}
|
|
58
63
|
var baseTargets;
|
|
59
64
|
|
|
60
65
|
if (!hasCustomTargets) {
|
|
61
|
-
baseTargets = DEFAULT_PUNCTUATION_TARGETS
|
|
66
|
+
baseTargets = DEFAULT_PUNCTUATION_TARGETS;
|
|
62
67
|
} else {
|
|
63
68
|
var customTargets = opts.spaceAfterPunctuationTargets;
|
|
64
69
|
if (customTargets === null || customTargets === false) return null;
|
|
@@ -67,13 +72,12 @@ function resolve_punctuation_targets(opts) {
|
|
|
67
72
|
baseTargets = [ customTargets ];
|
|
68
73
|
} else if (Array.isArray(customTargets)) {
|
|
69
74
|
if (customTargets.length === 0) return null;
|
|
70
|
-
baseTargets = customTargets
|
|
75
|
+
baseTargets = customTargets;
|
|
71
76
|
} else {
|
|
72
|
-
baseTargets = DEFAULT_PUNCTUATION_TARGETS
|
|
77
|
+
baseTargets = DEFAULT_PUNCTUATION_TARGETS;
|
|
73
78
|
}
|
|
74
79
|
}
|
|
75
80
|
|
|
76
|
-
var addTargets = opts.spaceAfterPunctuationTargetsAdd;
|
|
77
81
|
if (addTargets !== undefined) {
|
|
78
82
|
var addList = [];
|
|
79
83
|
if (typeof addTargets === 'string') {
|
|
@@ -86,7 +90,6 @@ function resolve_punctuation_targets(opts) {
|
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
|
|
89
|
-
var removeTargets = opts.spaceAfterPunctuationTargetsRemove;
|
|
90
93
|
if (removeTargets !== undefined) {
|
|
91
94
|
var removeList = [];
|
|
92
95
|
if (typeof removeTargets === 'string') {
|
|
@@ -174,7 +177,7 @@ function build_next_text_info(tokens, trackSkippedEmpty) {
|
|
|
174
177
|
|
|
175
178
|
|
|
176
179
|
function process_inlines(tokens, state, ctx, inlineToken) {
|
|
177
|
-
var i, last,
|
|
180
|
+
var i, last, next, c1, c2, remove_break;
|
|
178
181
|
var either = ctx.either;
|
|
179
182
|
var normalizeSoftBreaks = ctx.normalizeSoftBreaks;
|
|
180
183
|
var punctuationSpace = ctx.punctuationSpace;
|
|
@@ -183,15 +186,27 @@ function process_inlines(tokens, state, ctx, inlineToken) {
|
|
|
183
186
|
var considerInlineBoundaries = ctx.considerInlineBoundaries;
|
|
184
187
|
var needsPunctuation = punctuationSpace && punctuationConfig && maxPunctuationLength > 0;
|
|
185
188
|
|
|
189
|
+
if (!tokens || tokens.length === 0) return;
|
|
186
190
|
if (normalizeSoftBreaks) normalize_text_tokens(tokens);
|
|
187
191
|
|
|
188
|
-
var
|
|
189
|
-
var
|
|
190
|
-
var
|
|
191
|
-
|
|
192
|
-
var widthCache = Object.create(null);
|
|
192
|
+
var nextTextIndex = null;
|
|
193
|
+
var nextSkippedEmpty = null;
|
|
194
|
+
var widthCache = null;
|
|
193
195
|
function get_cached_width_class(ch) {
|
|
194
196
|
if (!ch) return '';
|
|
197
|
+
if (!widthCache) {
|
|
198
|
+
var firstWidth = get_cjk_width_class(ch);
|
|
199
|
+
if (firstWidth === '') {
|
|
200
|
+
var codePoint = ch.codePointAt(0);
|
|
201
|
+
if (codePoint !== undefined && codePoint <= ASCII_PRINTABLE_MAX) return '';
|
|
202
|
+
widthCache = Object.create(null);
|
|
203
|
+
widthCache[ch] = '';
|
|
204
|
+
return '';
|
|
205
|
+
}
|
|
206
|
+
widthCache = Object.create(null);
|
|
207
|
+
widthCache[ch] = firstWidth;
|
|
208
|
+
return firstWidth;
|
|
209
|
+
}
|
|
195
210
|
var cached = widthCache[ch];
|
|
196
211
|
if (cached !== undefined) return cached;
|
|
197
212
|
var width = get_cjk_width_class(ch);
|
|
@@ -208,11 +223,13 @@ function process_inlines(tokens, state, ctx, inlineToken) {
|
|
|
208
223
|
var isSoftbreakToken = token.type === 'softbreak';
|
|
209
224
|
var isTextBreakToken = token.type === 'text' && token.content === '\n';
|
|
210
225
|
if (isSoftbreakToken || isTextBreakToken) {
|
|
226
|
+
if (!nextTextIndex) {
|
|
227
|
+
var nextInfo = build_next_text_info(tokens, considerInlineBoundaries);
|
|
228
|
+
nextTextIndex = nextInfo.nextTextIndex;
|
|
229
|
+
nextSkippedEmpty = nextInfo.nextSkippedEmpty;
|
|
230
|
+
}
|
|
211
231
|
// default last/next character to space
|
|
212
232
|
last = next = ' ';
|
|
213
|
-
trailing = '';
|
|
214
|
-
var trailingMatchesPunctuation = false;
|
|
215
|
-
|
|
216
233
|
var skippedEmptyBefore = false;
|
|
217
234
|
var skippedEmptyAfter = false;
|
|
218
235
|
if (considerInlineBoundaries) {
|
|
@@ -224,10 +241,6 @@ function process_inlines(tokens, state, ctx, inlineToken) {
|
|
|
224
241
|
c1 = lastTextContent.charCodeAt(lastTextContent.length - 2);
|
|
225
242
|
c2 = lastTextContent.charCodeAt(lastTextContent.length - 1);
|
|
226
243
|
last = lastTextContent.slice(is_surrogate(c1, c2) ? -2 : -1);
|
|
227
|
-
if (needsPunctuation) {
|
|
228
|
-
trailing = lastTextContent.slice(-maxPunctuationLength);
|
|
229
|
-
trailingMatchesPunctuation = matches_punctuation_sequence(trailing, punctuationConfig);
|
|
230
|
-
}
|
|
231
244
|
}
|
|
232
245
|
|
|
233
246
|
var nextIdx = nextTextIndex[i];
|
|
@@ -270,12 +283,15 @@ function process_inlines(tokens, state, ctx, inlineToken) {
|
|
|
270
283
|
|
|
271
284
|
if (remove_break) {
|
|
272
285
|
var insertPunctuationSpace = false;
|
|
273
|
-
if (needsPunctuation &&
|
|
274
|
-
|
|
275
|
-
|
|
286
|
+
if (needsPunctuation && last && next && next !== '\u200b') {
|
|
287
|
+
var trailing = hasLastText ? lastTextContent.slice(-maxPunctuationLength) : '';
|
|
288
|
+
if (matches_punctuation_sequence(trailing, punctuationConfig)) {
|
|
289
|
+
if (!nextWidthComputed) {
|
|
290
|
+
nextWidthClass = get_cached_width_class(next);
|
|
291
|
+
}
|
|
292
|
+
var nextIsFullwidthOrWide = nextWidthClass === 'F' || nextWidthClass === 'W';
|
|
293
|
+
if (nextIsFullwidthOrWide || is_printable_ascii(next)) insertPunctuationSpace = true;
|
|
276
294
|
}
|
|
277
|
-
var nextIsFullwidthOrWide = nextWidthClass === 'F' || nextWidthClass === 'W';
|
|
278
|
-
if (is_printable_ascii(next) || nextIsFullwidthOrWide) insertPunctuationSpace = true;
|
|
279
295
|
}
|
|
280
296
|
token.type = 'text';
|
|
281
297
|
token.content = insertPunctuationSpace ? punctuationSpace : '';
|
|
@@ -284,11 +300,11 @@ function process_inlines(tokens, state, ctx, inlineToken) {
|
|
|
284
300
|
|
|
285
301
|
if (token.type === 'text') {
|
|
286
302
|
if (!token.content) {
|
|
287
|
-
sawEmptySinceLast = true;
|
|
303
|
+
if (considerInlineBoundaries) sawEmptySinceLast = true;
|
|
288
304
|
} else {
|
|
289
305
|
lastTextContent = token.content;
|
|
290
306
|
hasLastText = true;
|
|
291
|
-
sawEmptySinceLast = false;
|
|
307
|
+
if (considerInlineBoundaries) sawEmptySinceLast = false;
|
|
292
308
|
}
|
|
293
309
|
}
|
|
294
310
|
}
|
|
@@ -389,6 +405,8 @@ function apply_missing_punctuation_spacing(tokens, inlineToken, punctuationSpace
|
|
|
389
405
|
if (!inlineToken || !inlineToken.content) return;
|
|
390
406
|
if (inlineToken.content.indexOf('\n') === -1) return;
|
|
391
407
|
if (!tokens || tokens.length === 0) return;
|
|
408
|
+
var maxPunctuationLength = punctuationConfig.maxLength;
|
|
409
|
+
if (maxPunctuationLength <= 0) return;
|
|
392
410
|
|
|
393
411
|
var rawSearchState = { pos: 0 };
|
|
394
412
|
|
|
@@ -396,9 +414,7 @@ function apply_missing_punctuation_spacing(tokens, inlineToken, punctuationSpace
|
|
|
396
414
|
var current = tokens[idx];
|
|
397
415
|
if (!current || current.type !== 'text' || !current.content) continue;
|
|
398
416
|
|
|
399
|
-
var trailing =
|
|
400
|
-
current.content.slice(-punctuationConfig.maxLength) :
|
|
401
|
-
current.content.slice(-1);
|
|
417
|
+
var trailing = current.content.slice(-maxPunctuationLength);
|
|
402
418
|
if (!matches_punctuation_sequence(trailing, punctuationConfig)) continue;
|
|
403
419
|
if (WHITESPACE_TRAIL_RE.test(current.content)) continue;
|
|
404
420
|
|
|
@@ -414,7 +430,9 @@ function apply_missing_punctuation_spacing(tokens, inlineToken, punctuationSpace
|
|
|
414
430
|
idx = nextInfo.index;
|
|
415
431
|
}
|
|
416
432
|
|
|
417
|
-
|
|
433
|
+
if (tokens.length === 1) {
|
|
434
|
+
apply_single_text_token_spacing(tokens, inlineToken, punctuationSpace, punctuationConfig);
|
|
435
|
+
}
|
|
418
436
|
}
|
|
419
437
|
|
|
420
438
|
|
|
@@ -484,6 +502,8 @@ function apply_single_text_token_spacing(tokens, inlineToken, punctuationSpace,
|
|
|
484
502
|
if (inlineToken.content.indexOf('\n') === -1) return;
|
|
485
503
|
var token = tokens[0];
|
|
486
504
|
if (!token || token.type !== 'text' || !token.content) return;
|
|
505
|
+
var maxPunctuationLength = punctuationConfig.maxLength;
|
|
506
|
+
if (maxPunctuationLength <= 0) return;
|
|
487
507
|
|
|
488
508
|
var segments = inlineToken.content.split('\n');
|
|
489
509
|
if (segments.length < 2) return;
|
|
@@ -493,7 +513,7 @@ function apply_single_text_token_spacing(tokens, inlineToken, punctuationSpace,
|
|
|
493
513
|
for (var segIdx = 0; segIdx < segments.length - 1; segIdx++) {
|
|
494
514
|
var leftRaw = segments[segIdx];
|
|
495
515
|
var rightRaw = segments[segIdx + 1];
|
|
496
|
-
var tail = extract_visible_tail(leftRaw,
|
|
516
|
+
var tail = extract_visible_tail(leftRaw, maxPunctuationLength);
|
|
497
517
|
var nextChar = extract_visible_head(rightRaw);
|
|
498
518
|
var shouldInsert = tail &&
|
|
499
519
|
matches_punctuation_sequence(tail, punctuationConfig) &&
|