@peaceroad/markdown-it-strong-ja 0.7.1 → 0.7.2

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-strong-ja",
3
3
  "description": "This is a plugin for markdown-it. It is an alternative to the standard `**` (strong) and `*` (em) processing. It also processes strings that cannot be converted by the standard.",
4
- "version": "0.7.1",
4
+ "version": "0.7.2",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "files": [
@@ -7,6 +7,12 @@ import {
7
7
  moveRuleAfter
8
8
  } from './token-utils.js'
9
9
 
10
+ const isAsciiWordCode = (code) => {
11
+ return (code >= 0x30 && code <= 0x39) ||
12
+ (code >= 0x41 && code <= 0x5A) ||
13
+ (code >= 0x61 && code <= 0x7A)
14
+ }
15
+
10
16
  const registerTokenCompat = (md, baseOpt) => {
11
17
  const hasTextJoinRule = Array.isArray(md.core?.ruler?.__rules__)
12
18
  ? md.core.ruler.__rules__.some((rule) => rule && rule.name === 'text_join')
@@ -60,20 +66,21 @@ const registerTokenCompat = (md, baseOpt) => {
60
66
  break
61
67
  }
62
68
  }
69
+ if (!hasEmphasis) continue
63
70
  for (let j = 0; j < token.children.length; j++) {
64
71
  const child = token.children[j]
65
72
  if (!child) continue
66
73
  if (child.type === 'softbreak') {
67
- if (!hasEmphasis) continue
68
74
  const prevToken = token.children[j - 1]
69
75
  const nextToken = token.children[j + 1]
70
76
  if (!prevToken || !nextToken) continue
71
77
  if (prevToken.type !== 'text' || !prevToken.content) continue
72
78
  if (nextToken.type !== 'text' || !nextToken.content) continue
73
- const prevChar = prevToken.content.slice(-1)
74
- const nextChar = nextToken.content.charAt(0)
75
- const isAsciiWord = nextChar >= '0' && nextChar <= 'z' && /[A-Za-z0-9]/.test(nextChar)
76
- const shouldReplace = isAsciiWord && nextChar !== '{' && nextChar !== '\\' && isJapaneseChar(prevChar) && !isJapaneseChar(nextChar)
79
+ const prevCharCode = prevToken.content.charCodeAt(prevToken.content.length - 1)
80
+ const nextCharCode = nextToken.content.charCodeAt(0)
81
+ const isAsciiWord = isAsciiWordCode(nextCharCode)
82
+ const shouldReplace = isAsciiWord && nextCharCode !== 0x7B && nextCharCode !== 0x5C &&
83
+ isJapaneseChar(prevCharCode) && !isJapaneseChar(nextCharCode)
77
84
  if (!shouldReplace) continue
78
85
  child.type = 'text'
79
86
  child.tag = ''
@@ -89,10 +96,11 @@ const registerTokenCompat = (md, baseOpt) => {
89
96
  for (let idx = 0; idx < child.content.length; idx++) {
90
97
  const ch = child.content[idx]
91
98
  if (ch === '\n') {
92
- const prevChar = idx > 0 ? child.content[idx - 1] : ''
93
- const nextChar = idx + 1 < child.content.length ? child.content[idx + 1] : ''
94
- const isAsciiWord = nextChar && nextChar >= '0' && nextChar <= 'z' && /[A-Za-z0-9]/.test(nextChar)
95
- const shouldReplace = isAsciiWord && nextChar !== '{' && nextChar !== '\\' && isJapaneseChar(prevChar) && !isJapaneseChar(nextChar)
99
+ const prevCharCode = idx > 0 ? child.content.charCodeAt(idx - 1) : 0
100
+ const nextCharCode = idx + 1 < child.content.length ? child.content.charCodeAt(idx + 1) : 0
101
+ const isAsciiWord = isAsciiWordCode(nextCharCode)
102
+ const shouldReplace = isAsciiWord && nextCharCode !== 0x7B && nextCharCode !== 0x5C &&
103
+ isJapaneseChar(prevCharCode) && !isJapaneseChar(nextCharCode)
96
104
  if (shouldReplace) {
97
105
  normalized += ' '
98
106
  continue
@@ -127,27 +135,26 @@ const registerTokenCompat = (md, baseOpt) => {
127
135
  const token = state.tokens[i]
128
136
  if (!token || token.type !== 'inline' || !token.children || token.children.length === 0) continue
129
137
  const children = token.children
138
+ let prevTextCharCode = 0
130
139
  for (let j = 0; j < children.length; j++) {
131
140
  const child = children[j]
132
- if (!child || child.type !== 'text' || child.content !== '') continue
133
- let prevChar = ''
134
- for (let k = j - 1; k >= 0; k--) {
135
- const prev = children[k]
136
- if (prev && prev.type === 'text' && prev.content) {
137
- prevChar = prev.content.charAt(prev.content.length - 1)
138
- break
141
+ if (!child) continue
142
+ if (child.type === 'text') {
143
+ if (child.content === '') {
144
+ if (!prevTextCharCode || !isJapaneseChar(prevTextCharCode)) continue
145
+ const next = children[j + 1]
146
+ if (!next || next.type !== 'text' || !next.content) continue
147
+ const nextCharCode = next.content.charCodeAt(0)
148
+ if (nextCharCode !== 0x7B) continue
149
+ child.type = 'softbreak'
150
+ child.tag = ''
151
+ child.content = '\n'
152
+ child.markup = ''
153
+ child.info = ''
154
+ continue
139
155
  }
156
+ prevTextCharCode = child.content.charCodeAt(child.content.length - 1)
140
157
  }
141
- if (!prevChar || !isJapaneseChar(prevChar)) continue
142
- const next = children[j + 1]
143
- if (!next || next.type !== 'text' || !next.content) continue
144
- const nextChar = next.content.charAt(0)
145
- if (nextChar !== '{') continue
146
- child.type = 'softbreak'
147
- child.tag = ''
148
- child.content = '\n'
149
- child.markup = ''
150
- child.info = ''
151
158
  }
152
159
  }
153
160
  }
@@ -217,29 +217,23 @@ const registerTokenPostprocess = (md, baseOpt, getNoLinkMdInstance) => {
217
217
  const scanState = { depth: 0, brokenEnd: false }
218
218
  for (let j = 0; j < children.length; j++) {
219
219
  const child = children[j]
220
- if (!child || child.type !== 'text' || !child.content) continue
220
+ if (!child) continue
221
+ if (!hasEmphasis &&
222
+ (child.type === 'strong_open' || child.type === 'strong_close' || child.type === 'em_open' || child.type === 'em_close')) {
223
+ hasEmphasis = true
224
+ }
225
+ if (!hasLinkClose && child.type === 'link_close') {
226
+ hasLinkClose = true
227
+ }
228
+ if (child.type !== 'text' || !child.content) continue
229
+ if (!hasBracketText && (child.content.indexOf('[') !== -1 || child.content.indexOf(']') !== -1)) {
230
+ hasBracketText = true
231
+ }
221
232
  if (scanBrokenRefState(child.content, scanState).brokenEnd) {
222
233
  maxReparse++
223
234
  }
224
235
  }
225
- if (maxReparse === 0) {
226
- for (let j = 0; j < children.length; j++) {
227
- const child = children[j]
228
- if (!child) continue
229
- if (child.type === 'text' && child.content) {
230
- if (!hasBracketText && (child.content.indexOf('[') !== -1 || child.content.indexOf(']') !== -1)) {
231
- hasBracketText = true
232
- }
233
- }
234
- if (!hasEmphasis &&
235
- (child.type === 'strong_open' || child.type === 'strong_close' || child.type === 'em_open' || child.type === 'em_close')) {
236
- hasEmphasis = true
237
- }
238
- if (!hasLinkClose && child.type === 'link_close') {
239
- hasLinkClose = true
240
- }
241
- }
242
- } else {
236
+ if (maxReparse !== 0) {
243
237
  let allowReparse = true
244
238
  while (true) {
245
239
  let didReparse = false