@peaceroad/markdown-it-cjk-breaks-mod 0.1.8 → 0.1.10

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.
Files changed (2) hide show
  1. package/index.js +77 -46
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -239,7 +239,6 @@ function process_inlines(tokens, ctx, inlineToken) {
239
239
  }
240
240
 
241
241
  var lastTextContent = '';
242
- var hasLastText = false;
243
242
  var sawEmptySinceLast = false;
244
243
 
245
244
  for (i = 0; i < tokens.length; i++) {
@@ -261,7 +260,7 @@ function process_inlines(tokens, ctx, inlineToken) {
261
260
  skippedEmptyAfter = nextSkippedEmpty ? nextSkippedEmpty[i] : false;
262
261
  }
263
262
 
264
- if (hasLastText) {
263
+ if (lastTextContent) {
265
264
  c1 = lastTextContent.charCodeAt(lastTextContent.length - 2);
266
265
  c2 = lastTextContent.charCodeAt(lastTextContent.length - 1);
267
266
  last = lastTextContent.slice(is_surrogate(c1, c2) ? -2 : -1);
@@ -305,7 +304,7 @@ function process_inlines(tokens, ctx, inlineToken) {
305
304
 
306
305
  if (remove_break) {
307
306
  var insertPunctuationSpace = false;
308
- if (needsPunctuation && hasLastText && nextIdx !== -1 && next !== '\u200b') {
307
+ if (needsPunctuation && lastTextContent && nextIdx !== -1 && next !== '\u200b') {
309
308
  if (punctuationEndCharMap[last]) {
310
309
  if (matches_punctuation_sequence(lastTextContent, punctuationConfig, true)) {
311
310
  if (!nextWidthComputed) {
@@ -326,7 +325,6 @@ function process_inlines(tokens, ctx, inlineToken) {
326
325
  if (considerInlineBoundaries) sawEmptySinceLast = true;
327
326
  } else {
328
327
  lastTextContent = token.content;
329
- hasLastText = true;
330
328
  if (considerInlineBoundaries) sawEmptySinceLast = false;
331
329
  }
332
330
  }
@@ -352,10 +350,7 @@ function normalize_text_tokens(tokens) {
352
350
  normalized = tokens.slice(0, idx);
353
351
  }
354
352
 
355
- var replacement = split_text_token(token);
356
- for (var r = 0; r < replacement.length; r++) {
357
- normalized.push(replacement[r]);
358
- }
353
+ append_split_text_token(normalized, token);
359
354
  }
360
355
 
361
356
  if (normalized) {
@@ -367,28 +362,33 @@ function normalize_text_tokens(tokens) {
367
362
  }
368
363
 
369
364
 
370
- function split_text_token(token) {
365
+ function append_split_text_token(target, token) {
371
366
  var TokenConstructor = token.constructor;
372
- var parts = [];
373
367
  var content = token.content;
374
368
  var start = 0;
369
+ var reusedToken = false;
370
+
371
+ function push_text_part(text) {
372
+ if (!text) return;
373
+ if (!reusedToken) {
374
+ token.content = text;
375
+ target.push(token);
376
+ reusedToken = true;
377
+ return;
378
+ }
379
+ target.push(clone_text_token(TokenConstructor, token, text));
380
+ }
375
381
 
376
382
  for (var pos = 0; pos < content.length; pos++) {
377
383
  if (content.charCodeAt(pos) !== 0x0A) continue;
378
384
 
379
- if (pos > start) {
380
- parts.push(clone_text_token(TokenConstructor, token, content.slice(start, pos)));
381
- }
385
+ if (pos > start) push_text_part(content.slice(start, pos));
382
386
 
383
- parts.push(create_softbreak_token(TokenConstructor, token));
387
+ target.push(create_softbreak_token(TokenConstructor, token));
384
388
  start = pos + 1;
385
389
  }
386
390
 
387
- if (start < content.length) {
388
- parts.push(clone_text_token(TokenConstructor, token, content.slice(start)));
389
- }
390
-
391
- return parts;
391
+ if (start < content.length) push_text_part(content.slice(start));
392
392
  }
393
393
 
394
394
 
@@ -450,7 +450,13 @@ function apply_missing_punctuation_spacing(tokens, inlineToken, punctuationSpace
450
450
  if (nextInfo.token.type === 'text' && has_leading_whitespace(nextInfo.token.content)) continue;
451
451
  if (nextInfo.hasActiveBreak) continue;
452
452
 
453
- if (!raw_boundary_includes_newline(inlineToken.content, tokens, idx, nextInfo.index, nextInfo.fragment, rawSearchState)) {
453
+ if (!raw_boundary_includes_newline(
454
+ inlineToken.content,
455
+ current.content,
456
+ nextInfo.betweenMarkup,
457
+ nextInfo.fragment,
458
+ rawSearchState
459
+ )) {
454
460
  continue;
455
461
  }
456
462
 
@@ -460,37 +466,33 @@ function apply_missing_punctuation_spacing(tokens, inlineToken, punctuationSpace
460
466
 
461
467
  }
462
468
 
463
- function raw_boundary_includes_newline(source, tokens, fromIdx, nextIdx, afterFragment, state) {
469
+ function raw_boundary_includes_newline(source, beforeFragment, betweenFragment, afterFragment, state) {
464
470
  if (!source || !afterFragment) return false;
465
- var beforeFragment = tokens[fromIdx].content || '';
466
- var betweenFragment = '';
467
- for (var k = fromIdx + 1; k < nextIdx; k++) {
468
- if (tokens[k].markup) betweenFragment += tokens[k].markup;
471
+ if (!beforeFragment) return false;
472
+ var beforeBoundary = betweenFragment ? beforeFragment + betweenFragment : beforeFragment;
473
+ var newlinePositions = get_newline_positions(source, state);
474
+ var startIndex = state.newlineIndex || 0;
475
+
476
+ while (startIndex < newlinePositions.length && newlinePositions[startIndex] < state.pos) {
477
+ startIndex++;
469
478
  }
470
- if (Array.isArray(afterFragment)) {
471
- for (var i = 0; i < afterFragment.length; i++) {
472
- var fragment = afterFragment[i];
473
- if (!fragment) continue;
474
- var candidate = beforeFragment + betweenFragment + '\n' + fragment;
475
- var startPos = source.indexOf(candidate, state.pos);
476
- if (startPos === -1) continue;
477
- state.pos = startPos + candidate.length - fragment.length;
478
- return true;
479
- }
480
- return false;
479
+ state.newlineIndex = startIndex;
480
+
481
+ for (var idx = startIndex; idx < newlinePositions.length; idx++) {
482
+ var newlinePos = newlinePositions[idx];
483
+ if (!matches_raw_newline_boundary(source, newlinePos, beforeBoundary, afterFragment)) continue;
484
+ state.pos = newlinePos + 1;
485
+ state.newlineIndex = idx + 1;
486
+ return true;
481
487
  }
482
- var fragment = afterFragment;
483
- if (!fragment) return false;
484
- var candidate = beforeFragment + betweenFragment + '\n' + fragment;
485
- var startPos = source.indexOf(candidate, state.pos);
486
- if (startPos === -1) return false;
487
- state.pos = startPos + candidate.length - fragment.length;
488
- return true;
488
+
489
+ return false;
489
490
  }
490
491
 
491
492
 
492
493
  function find_next_visible_token(tokens, startIdx) {
493
494
  var hasActiveBreak = false;
495
+ var betweenMarkup = '';
494
496
  for (var idx = startIdx; idx < tokens.length; idx++) {
495
497
  var token = tokens[idx];
496
498
  if (!token) continue;
@@ -498,13 +500,43 @@ function find_next_visible_token(tokens, startIdx) {
498
500
  hasActiveBreak = true;
499
501
  }
500
502
  var fragment = derive_after_fragment(token);
501
- if (!fragment) continue;
502
- return { index: idx, token: token, fragment: fragment, hasActiveBreak: hasActiveBreak };
503
+ if (!fragment) {
504
+ if (token.markup) betweenMarkup += token.markup;
505
+ continue;
506
+ }
507
+ return { index: idx, token: token, fragment: fragment, hasActiveBreak: hasActiveBreak, betweenMarkup: betweenMarkup };
503
508
  }
504
509
  return null;
505
510
  }
506
511
 
507
512
 
513
+ function get_newline_positions(source, state) {
514
+ if (state.newlinePositions) return state.newlinePositions;
515
+ var newlinePositions = [];
516
+ for (var idx = 0; idx < source.length; idx++) {
517
+ if (source.charCodeAt(idx) === 0x0A) newlinePositions.push(idx);
518
+ }
519
+ state.newlinePositions = newlinePositions;
520
+ return newlinePositions;
521
+ }
522
+
523
+
524
+ function matches_raw_newline_boundary(source, newlinePos, beforeBoundary, afterFragment) {
525
+ var beforeStart = newlinePos - beforeBoundary.length;
526
+ if (beforeStart < 0) return false;
527
+ if (!source.startsWith(beforeBoundary, beforeStart)) return false;
528
+ var afterStart = newlinePos + 1;
529
+ if (Array.isArray(afterFragment)) {
530
+ for (var i = 0; i < afterFragment.length; i++) {
531
+ var fragment = afterFragment[i];
532
+ if (fragment && source.startsWith(fragment, afterStart)) return true;
533
+ }
534
+ return false;
535
+ }
536
+ return source.startsWith(afterFragment, afterStart);
537
+ }
538
+
539
+
508
540
  function derive_after_fragment(token) {
509
541
  if (!token) return '';
510
542
  if (token.type === 'text' || token.type === 'html_inline' || token.type === 'code_inline') {
@@ -555,7 +587,6 @@ function apply_single_text_token_spacing(tokens, inlineToken, punctuationSpace,
555
587
  if (maxPunctuationLength <= 0) return;
556
588
 
557
589
  var segments = inlineToken.content.split('\n');
558
- if (segments.length < 2) return;
559
590
  var cumulativeLength = 0;
560
591
  var offsetDelta = 0;
561
592
  var updatedContent = token.content;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-cjk-breaks-mod",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "description": "Suppress linebreaks between east asian (Especially Japanese) characters",
6
6
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "eastasianwidth": "^0.3.0"
23
23
  },
24
24
  "devDependencies": {
25
- "@peaceroad/markdown-it-strong-ja": "^0.8.0",
25
+ "@peaceroad/markdown-it-strong-ja": "^0.8.1",
26
26
  "markdown-it": "^14.1.0"
27
27
  }
28
28
  }