markdown-it-cjk-friendly 2.0.2 → 3.0.0-rc.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.
Files changed (2) hide show
  1. package/dist/index.js +42 -15
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2,37 +2,64 @@ import { eastAsianWidthType } from "get-east-asian-width";
2
2
  import { isMdAsciiPunct, isPunctChar, isWhiteSpace } from "markdown-it/lib/common/utils.mjs";
3
3
 
4
4
  //#region src/index.ts
5
- function isEmoji(uc) {
5
+ function isDefaultEmoji(uc) {
6
6
  return /^\p{Emoji_Presentation}/u.test(String.fromCodePoint(uc));
7
7
  }
8
+ function canBeEmoji(uc) {
9
+ return /^\p{Emoji}/u.test(String.fromCodePoint(uc));
10
+ }
8
11
  /**
9
12
  * Check if `uc` is CJK. Deferred (returns `null`) if IVS.
10
13
  *
11
14
  * @param uc code point
12
- * @returns `true` if `uc` is CJK, `false` if not, `null` if IVS
15
+ * @returns 0: not CJK, 1: CJK, 2: possibly variation selector, 3: emoji or CJK
13
16
  */
14
17
  function isCjkBase(uc) {
15
- if (uc < 4352) return false;
18
+ if (uc < 4352) return 0;
16
19
  switch (eastAsianWidthType(uc)) {
17
20
  case "fullwidth":
18
- case "halfwidth": return true;
19
- case "wide": return !isEmoji(uc);
20
- case "narrow": return false;
21
- case "ambiguous": return null;
22
- case "neutral": return /^\p{sc=Hangul}/u.test(String.fromCodePoint(uc));
21
+ case "halfwidth": return 1;
22
+ case "wide": return canBeEmoji(uc) ? 3 : 1;
23
+ case "narrow": return 0;
24
+ case "ambiguous": return 2;
25
+ case "neutral": return /^\p{sc=Hangul}/u.test(String.fromCodePoint(uc)) ? 1 : 0;
23
26
  }
24
27
  }
28
+ function isQuotationMark(uc) {
29
+ return uc === 8216 || uc === 8217 || uc === 8220 || uc === 8221;
30
+ }
25
31
  function is2PreviousCjk(uc, prev) {
26
- return isCjkBase(uc) ?? (prev === 65025 && isQuotationMark(uc));
27
- function isQuotationMark(uc$1) {
28
- return uc$1 === 8216 || uc$1 === 8217 || uc$1 === 8220 || uc$1 === 8221;
32
+ switch (isCjkBase(uc)) {
33
+ case 1: return true;
34
+ case 2: return prev === 65025 && isQuotationMark(uc);
35
+ case 3: return !isDefaultEmoji(uc) || prev === 65038;
29
36
  }
37
+ return false;
30
38
  }
31
39
  function isPreviousCjk(uc) {
32
- return isCjkBase(uc) ?? (917760 <= uc && uc <= 917999);
40
+ switch (isCjkBase(uc)) {
41
+ case 1: return true;
42
+ case 2: return 917760 <= uc && uc <= 917999;
43
+ case 3: return !isDefaultEmoji(uc);
44
+ }
45
+ return false;
33
46
  }
34
- function isNextCjk(uc) {
35
- return isCjkBase(uc) ?? false;
47
+ function isNextCjk(uc, src, nextPos, max) {
48
+ switch (isCjkBase(uc)) {
49
+ case 1: return true;
50
+ case 2:
51
+ if (!isQuotationMark(uc)) return false;
52
+ return getNextCharCode(src, nextPos + (uc > 65535 ? 2 : 1), max) === 65025;
53
+ case 3: {
54
+ const nextNext = getNextCharCode(src, nextPos + (uc > 65535 ? 2 : 1), max);
55
+ return isDefaultEmoji(uc) ? nextNext === 65038 : nextNext !== 65039;
56
+ }
57
+ }
58
+ return false;
59
+ function getNextCharCode(src$1, pos, max$1) {
60
+ if (pos >= max$1) return 32;
61
+ return src$1.codePointAt(pos) ?? 32;
62
+ }
36
63
  }
37
64
  function nonEmojiGeneralUseVS(uc) {
38
65
  return uc >= 65024 && uc <= 65038;
@@ -66,7 +93,7 @@ function markdownItCjkFriendlyPlugin(md) {
66
93
  let left_flanking = isLastPunctChar;
67
94
  let right_flanking = isNextPunctChar;
68
95
  if (canSplitWord) {
69
- const isEitherCJKChar = isNextCjk(nextChar) || (twoPrevChar !== null ? is2PreviousCjk(twoPrevChar, lastChar) : isPreviousCjk(lastChar));
96
+ const isEitherCJKChar = isNextCjk(nextChar, this.src, pos, max) || (twoPrevChar !== null ? is2PreviousCjk(twoPrevChar, lastChar) : isPreviousCjk(lastChar));
70
97
  left_flanking ||= isEitherCJKChar || !isNextPunctChar;
71
98
  right_flanking ||= isEitherCJKChar || !isLastPunctChar;
72
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-it-cjk-friendly",
3
- "version": "2.0.2",
3
+ "version": "3.0.0-rc.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {