@peaceroad/markdown-it-cjk-breaks-mod 0.1.9 → 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 +49 -29
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -350,10 +350,7 @@ function normalize_text_tokens(tokens) {
350
350
  normalized = tokens.slice(0, idx);
351
351
  }
352
352
 
353
- var replacement = split_text_token(token);
354
- for (var r = 0; r < replacement.length; r++) {
355
- normalized.push(replacement[r]);
356
- }
353
+ append_split_text_token(normalized, token);
357
354
  }
358
355
 
359
356
  if (normalized) {
@@ -365,9 +362,8 @@ function normalize_text_tokens(tokens) {
365
362
  }
366
363
 
367
364
 
368
- function split_text_token(token) {
365
+ function append_split_text_token(target, token) {
369
366
  var TokenConstructor = token.constructor;
370
- var parts = [];
371
367
  var content = token.content;
372
368
  var start = 0;
373
369
  var reusedToken = false;
@@ -376,11 +372,11 @@ function split_text_token(token) {
376
372
  if (!text) return;
377
373
  if (!reusedToken) {
378
374
  token.content = text;
379
- parts.push(token);
375
+ target.push(token);
380
376
  reusedToken = true;
381
377
  return;
382
378
  }
383
- parts.push(clone_text_token(TokenConstructor, token, text));
379
+ target.push(clone_text_token(TokenConstructor, token, text));
384
380
  }
385
381
 
386
382
  for (var pos = 0; pos < content.length; pos++) {
@@ -388,13 +384,11 @@ function split_text_token(token) {
388
384
 
389
385
  if (pos > start) push_text_part(content.slice(start, pos));
390
386
 
391
- parts.push(create_softbreak_token(TokenConstructor, token));
387
+ target.push(create_softbreak_token(TokenConstructor, token));
392
388
  start = pos + 1;
393
389
  }
394
390
 
395
391
  if (start < content.length) push_text_part(content.slice(start));
396
-
397
- return parts;
398
392
  }
399
393
 
400
394
 
@@ -475,25 +469,24 @@ function apply_missing_punctuation_spacing(tokens, inlineToken, punctuationSpace
475
469
  function raw_boundary_includes_newline(source, beforeFragment, betweenFragment, afterFragment, state) {
476
470
  if (!source || !afterFragment) return false;
477
471
  if (!beforeFragment) return false;
478
- betweenFragment = betweenFragment || '';
479
- if (Array.isArray(afterFragment)) {
480
- for (var i = 0; i < afterFragment.length; i++) {
481
- var fragment = afterFragment[i];
482
- if (!fragment) continue;
483
- var candidate = beforeFragment + betweenFragment + '\n' + fragment;
484
- var startPos = source.indexOf(candidate, state.pos);
485
- if (startPos === -1) continue;
486
- state.pos = startPos + candidate.length - fragment.length;
487
- return true;
488
- }
489
- 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++;
478
+ }
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;
490
487
  }
491
- var fragment = afterFragment;
492
- var candidate = beforeFragment + betweenFragment + '\n' + fragment;
493
- var startPos = source.indexOf(candidate, state.pos);
494
- if (startPos === -1) return false;
495
- state.pos = startPos + candidate.length - fragment.length;
496
- return true;
488
+
489
+ return false;
497
490
  }
498
491
 
499
492
 
@@ -517,6 +510,33 @@ function find_next_visible_token(tokens, startIdx) {
517
510
  }
518
511
 
519
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
+
520
540
  function derive_after_fragment(token) {
521
541
  if (!token) return '';
522
542
  if (token.type === 'text' || token.type === 'html_inline' || token.type === 'code_inline') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-cjk-breaks-mod",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "description": "Suppress linebreaks between east asian (Especially Japanese) characters",
6
6
  "repository": {