@peaceroad/markdown-it-strong-ja 0.3.1 → 0.3.3
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 +246 -90
- package/package.json +3 -2
- package/test/example-complex.txt +119 -0
- package/test/example-strong.txt +1 -2
- package/test/test.js +5 -4
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const hasBackslash = (state, start) => {
|
|
2
2
|
let slashNum = 0
|
|
3
3
|
let i = start - 1
|
|
4
4
|
while(i >= 0) {
|
|
@@ -12,7 +12,7 @@ const hasSlashFlag = (state, start) => {
|
|
|
12
12
|
const setToken = (state, inlines) => {
|
|
13
13
|
let i = 0
|
|
14
14
|
while (i < inlines.length) {
|
|
15
|
-
|
|
15
|
+
let type = inlines[i].type
|
|
16
16
|
const tag = type.replace(/(?:_open|_close)$/, '')
|
|
17
17
|
|
|
18
18
|
if (/_open$/.test(type)) {
|
|
@@ -20,20 +20,26 @@ const setToken = (state, inlines) => {
|
|
|
20
20
|
startToken.markup = tag === 'strong' ? '**' : '*'
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
if (type === 'html_inline') {
|
|
24
|
+
type = 'text'
|
|
25
|
+
}
|
|
23
26
|
if (type === 'text') {
|
|
24
27
|
const content = state.src.slice(inlines[i].s, inlines[i].e + 1)
|
|
25
28
|
if (/^\**$/.test(content)) {
|
|
26
29
|
//console.log('asterisk process::')
|
|
27
30
|
const asteriskToken = state.push(type, '', 0)
|
|
28
31
|
asteriskToken.content = content
|
|
29
|
-
//console.log('asteriskToken: ' + asteriskToken.content)
|
|
30
32
|
i++
|
|
31
33
|
continue
|
|
32
34
|
}
|
|
35
|
+
|
|
33
36
|
const childTokens = state.md.parseInline(content, state.env)
|
|
34
37
|
if (childTokens[0] && childTokens[0].children) {
|
|
38
|
+
//console.log(state.tokens)
|
|
39
|
+
//console.log(state.tokens[state.tokens.length - 1])
|
|
35
40
|
state.tokens[state.tokens.length - 1].children = childTokens[0].children
|
|
36
41
|
childTokens[0].children.forEach(t => {
|
|
42
|
+
//console.log('t.type: ' + t.type + ', t.tag: ' + t.tag + ', t.nesting: ' + t.nesting)
|
|
37
43
|
const token = state.push(t.type, t.tag, t.nesting)
|
|
38
44
|
token.attrs = t.attrs
|
|
39
45
|
token.map = t.map
|
|
@@ -58,52 +64,113 @@ const setToken = (state, inlines) => {
|
|
|
58
64
|
}
|
|
59
65
|
}
|
|
60
66
|
|
|
61
|
-
const inlinesPush = (inlines, s, e, len, type) => {
|
|
62
|
-
|
|
67
|
+
const inlinesPush = (inlines, s, e, len, type, tag, tagType) => {
|
|
68
|
+
const inline = {
|
|
63
69
|
s: s,
|
|
64
70
|
sp: s,
|
|
65
71
|
e: e,
|
|
66
72
|
ep: e,
|
|
67
73
|
len: len,
|
|
68
74
|
type: type,
|
|
69
|
-
}
|
|
75
|
+
}
|
|
76
|
+
if (tag) inline.tag = [tag, tagType]
|
|
77
|
+
inlines.push(inline)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const hasNextSymbol = (state, n, max, symbol, noMark) => {
|
|
81
|
+
let nextSymbolPos = -1
|
|
82
|
+
if (state.src.charCodeAt(n) === symbol && !hasBackslash(state, n)) {
|
|
83
|
+
let i = n + 1
|
|
84
|
+
let tempNoMark = noMark
|
|
85
|
+
while (i < max) {
|
|
86
|
+
tempNoMark += state.src[i]
|
|
87
|
+
if (state.src.charCodeAt(i) === symbol && !hasBackslash(state, i)) {
|
|
88
|
+
noMark += state.src[n]
|
|
89
|
+
nextSymbolPos = i
|
|
90
|
+
break
|
|
91
|
+
}
|
|
92
|
+
i++
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return nextSymbolPos
|
|
70
96
|
}
|
|
71
97
|
|
|
72
|
-
const
|
|
98
|
+
const createInlines = (state, start, max, opt) => {
|
|
73
99
|
let n = start
|
|
74
100
|
let inlines = []
|
|
75
101
|
let noMark = ''
|
|
76
|
-
let
|
|
77
|
-
let
|
|
102
|
+
let isInStartMark = true
|
|
103
|
+
let textStart = n
|
|
104
|
+
|
|
78
105
|
while (n < max) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
106
|
+
let nextSymbolPos = hasNextSymbol(state, n, max, 0x60, noMark) // '`'
|
|
107
|
+
if (nextSymbolPos !== -1) {
|
|
108
|
+
n = nextSymbolPos + 1
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
if (opt.dollarMath) {
|
|
112
|
+
nextSymbolPos = hasNextSymbol(state, n, max, 0x24, noMark) // '$'
|
|
113
|
+
if (nextSymbolPos !== -1) {
|
|
114
|
+
n = nextSymbolPos + 1
|
|
115
|
+
continue
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (state.md.options.html) {
|
|
120
|
+
if (state.src.charCodeAt(n) === 0x3C && !hasBackslash(state, n)) { // '<'
|
|
121
|
+
let i = n + 1
|
|
122
|
+
while (i < max) {
|
|
123
|
+
if (state.src.charCodeAt(i) === 0x3E && !hasBackslash(state, i)) { // '>'
|
|
124
|
+
if (noMark.length !== 0) {
|
|
125
|
+
inlinesPush(inlines, textStart, n - 1, n - textStart, 'text')
|
|
126
|
+
}
|
|
127
|
+
let tag = state.src.slice(n + 1, i)
|
|
128
|
+
let tagType = ''
|
|
129
|
+
if (/^\//.test(tag)) {
|
|
130
|
+
tag = tag.slice(1)
|
|
131
|
+
tagType = 'close'
|
|
132
|
+
} else {
|
|
133
|
+
tagType = 'open'
|
|
134
|
+
}
|
|
135
|
+
inlinesPush(inlines, n, i, i - n + 1, 'html_inline', tag, tagType)
|
|
136
|
+
textStart = i + 1
|
|
137
|
+
break
|
|
138
|
+
}
|
|
139
|
+
i++
|
|
85
140
|
}
|
|
86
|
-
n
|
|
141
|
+
n = i + 1
|
|
87
142
|
continue
|
|
88
143
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (state.src.charCodeAt(n) === 0x2A && !hasBackslash(state, n)) { // '*'
|
|
147
|
+
if (!isInStartMark) {
|
|
148
|
+
inlinesPush(inlines, textStart, n - 1, n - textStart, 'text')
|
|
93
149
|
}
|
|
94
|
-
noMark = ''
|
|
95
150
|
if (n === max - 1) {
|
|
96
|
-
inlinesPush(inlines, n
|
|
151
|
+
inlinesPush(inlines, n, n, 1 , '')
|
|
152
|
+
break
|
|
97
153
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
inlinesPush(inlines, n -
|
|
154
|
+
let i = n + 1
|
|
155
|
+
while (i < max) {
|
|
156
|
+
if (state.src.charCodeAt(i) === 0x2A) {
|
|
157
|
+
if (i === max - 1) inlinesPush(inlines, n, i, i - n + 1 , '')
|
|
158
|
+
i++
|
|
159
|
+
continue
|
|
160
|
+
}
|
|
161
|
+
inlinesPush(inlines, n, i - 1, i - n, '')
|
|
162
|
+
textStart = i
|
|
163
|
+
break
|
|
106
164
|
}
|
|
165
|
+
n = i
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
isInStartMark = false
|
|
169
|
+
noMark += state.src[n]
|
|
170
|
+
//console.log('noMark: ' + noMark)
|
|
171
|
+
if (n === max - 1 || max < 3) {
|
|
172
|
+
inlinesPush(inlines, textStart, n, n - textStart + 1, 'text')
|
|
173
|
+
break
|
|
107
174
|
}
|
|
108
175
|
n++
|
|
109
176
|
}
|
|
@@ -121,7 +188,6 @@ const marksPush = (marks, nest, s, e, len, outsideLen, type) => {
|
|
|
121
188
|
oLen: outsideLen,
|
|
122
189
|
type: type,
|
|
123
190
|
}
|
|
124
|
-
//let i = marks.findIndex(o => o.s > s)
|
|
125
191
|
let i = marks.findIndex(o => o.s > s)
|
|
126
192
|
if (i === -1) {
|
|
127
193
|
marks.push(np)
|
|
@@ -131,17 +197,27 @@ const marksPush = (marks, nest, s, e, len, outsideLen, type) => {
|
|
|
131
197
|
}
|
|
132
198
|
|
|
133
199
|
const setStrong = (inlines, marks, n, memo) => {
|
|
134
|
-
let i = n +
|
|
200
|
+
let i = n + 1
|
|
135
201
|
let j = 0
|
|
136
202
|
let nest = 0
|
|
203
|
+
let insideTagsIsClose = 1
|
|
137
204
|
while (i < inlines.length) {
|
|
138
|
-
if (inlines[i].len === 0) { i
|
|
205
|
+
if (inlines[i].len === 0) { i++; continue }
|
|
206
|
+
if (memo.html) {
|
|
207
|
+
if (inlines[i].type === 'html_inline') {
|
|
208
|
+
insideTagsIsClose = isJumpTag(inlines, i, memo)
|
|
209
|
+
//console.log('insideTagsIsClose: ' + insideTagsIsClose )
|
|
210
|
+
if (insideTagsIsClose === -1) return n, nest, memo
|
|
211
|
+
if (insideTagsIsClose === 0) { i++; continue }
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (inlines[i].type !== '') { i++; continue }
|
|
215
|
+
|
|
139
216
|
//console.log('n: ' + n + ' [strong]: inlines[n].len: ' + inlines[n].len + ', i: ' + i + ', inlines[i].len: ' + inlines[i].len)
|
|
140
217
|
|
|
141
218
|
nest = checkNest(inlines, marks, n, i)
|
|
142
219
|
//console.log('n: ' + n + ' [strong]: nest: ' + nest)
|
|
143
220
|
if (nest === -1) return n, nest, memo
|
|
144
|
-
|
|
145
221
|
if (inlines[i].len === 1 && inlines[n].len > 2) {
|
|
146
222
|
//console.log('n: ' + n + ' [strong]: check em inside strong: ' + nest)
|
|
147
223
|
marksPush(marks, nest, inlines[n].ep, inlines[n].ep, 1, inlines[n].len - 1, 'em_open')
|
|
@@ -150,17 +226,21 @@ const setStrong = (inlines, marks, n, memo) => {
|
|
|
150
226
|
inlines[n].ep -= 1
|
|
151
227
|
inlines[i].len = 0
|
|
152
228
|
inlines[i].sp += 1
|
|
153
|
-
if (i
|
|
154
|
-
i
|
|
229
|
+
if (i++ < inlines.length) {
|
|
230
|
+
i++
|
|
155
231
|
nest++
|
|
156
232
|
} else {
|
|
157
233
|
return n, nest, memo
|
|
158
234
|
}
|
|
159
|
-
|
|
160
|
-
//console.log('n: ' + n + ' [strong]: check em inside strong end.')
|
|
235
|
+
if (i > inlines.length - 1) return n, nest, memo
|
|
161
236
|
}
|
|
162
237
|
|
|
238
|
+
if (memo.html && !insideTagsIsClose && inlines[i].len !== 1) {
|
|
239
|
+
memo.htmlTags = {}
|
|
240
|
+
return n, nest, memo
|
|
241
|
+
}
|
|
163
242
|
let strongNum = Math.trunc(Math.min(inlines[n].len, inlines[i].len) / 2)
|
|
243
|
+
|
|
164
244
|
if (inlines[i].len > 1) {
|
|
165
245
|
//console.log('n: ' + n + ' [strong]: normal push, nest: ' + nest)
|
|
166
246
|
j = 0
|
|
@@ -175,16 +255,15 @@ const setStrong = (inlines, marks, n, memo) => {
|
|
|
175
255
|
//console.log(marks)
|
|
176
256
|
j++
|
|
177
257
|
}
|
|
178
|
-
|
|
179
258
|
if (inlines[n].len === 0) return n, nest, memo
|
|
180
259
|
}
|
|
181
260
|
|
|
182
261
|
if (inlines[n].len === 1) {
|
|
183
|
-
//console.log('check em that warp strong
|
|
262
|
+
//console.log('check em that warp strong.')
|
|
184
263
|
nest++
|
|
185
|
-
n, nest, memo= setEm(inlines, marks, n, memo, nest)
|
|
264
|
+
n, nest, memo = setEm(inlines, marks, n, memo, nest)
|
|
186
265
|
if (memo.hasEmThatWrapStrong) {
|
|
187
|
-
//console.log('
|
|
266
|
+
//console.log('set em that wrap strong.')
|
|
188
267
|
let k = 0
|
|
189
268
|
while (k < strongNum) {
|
|
190
269
|
marks[marks.length - 2 - k * 2 - 1].nest += 1
|
|
@@ -193,19 +272,57 @@ const setStrong = (inlines, marks, n, memo) => {
|
|
|
193
272
|
}
|
|
194
273
|
}
|
|
195
274
|
}
|
|
196
|
-
|
|
197
275
|
if (inlines[n].len === 0) return n, nest, memo
|
|
198
|
-
i
|
|
276
|
+
i++
|
|
199
277
|
}
|
|
200
278
|
return n, nest, memo
|
|
201
279
|
}
|
|
202
280
|
|
|
281
|
+
const isJumpTag = (inlines, n, memo) => {
|
|
282
|
+
//console.log(n, 'before::memo.htmlTags: ' + JSON.stringify(memo.htmlTags))
|
|
283
|
+
const hasSet = Object.keys(memo.htmlTags).some(tag => {
|
|
284
|
+
if (tag === inlines[n].tag[0]) {
|
|
285
|
+
if (inlines[n].tag[1] === 'open') {
|
|
286
|
+
memo.htmlTags[tag]++ }
|
|
287
|
+
else {
|
|
288
|
+
memo.htmlTags[tag]--
|
|
289
|
+
}
|
|
290
|
+
return true
|
|
291
|
+
}
|
|
292
|
+
return false
|
|
293
|
+
})
|
|
294
|
+
if (!hasSet && !memo.htmlTags[inlines[n].tag[0]]) {
|
|
295
|
+
if (inlines[n].tag[1] === 'close') {
|
|
296
|
+
memo.htmlTags = {}
|
|
297
|
+
return -1
|
|
298
|
+
}
|
|
299
|
+
memo.htmlTags[inlines[n].tag[0]] = 1
|
|
300
|
+
}
|
|
301
|
+
//console.log(n, 'after::memo.htmlTags: ' + JSON.stringify(memo.htmlTags))
|
|
302
|
+
const closeAllTags = Object.values(memo.htmlTags).every(val => val === 0)
|
|
303
|
+
//console.log('closeAllTags: ' + closeAllTags)
|
|
304
|
+
if (closeAllTags) return 1
|
|
305
|
+
//memo.htmlTags = {}
|
|
306
|
+
return 0
|
|
307
|
+
}
|
|
308
|
+
|
|
203
309
|
const setEm = (inlines, marks, n, memo, sNest) => {
|
|
204
|
-
let i = n +
|
|
310
|
+
let i = n + 1
|
|
205
311
|
let nest = 0
|
|
206
312
|
let strongPNum = 0
|
|
313
|
+
let insideTagsIsClose = 1
|
|
207
314
|
while (i < inlines.length) {
|
|
208
|
-
if (inlines[i].len === 0) { i
|
|
315
|
+
if (inlines[i].len === 0) { i++; continue }
|
|
316
|
+
if (memo.isEm && memo.html) {
|
|
317
|
+
if (inlines[i].type === 'html_inline') {
|
|
318
|
+
insideTagsIsClose = isJumpTag(inlines, i, memo)
|
|
319
|
+
//console.log('insideTagsIsClose: ' + insideTagsIsClose )
|
|
320
|
+
if (insideTagsIsClose === -1) return n, nest, memo
|
|
321
|
+
if (insideTagsIsClose === 0) { i++; continue }
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (inlines[i].type !== '') { i++; continue }
|
|
325
|
+
|
|
209
326
|
const emNum = Math.min(inlines[n].len, inlines[i].len)
|
|
210
327
|
if (memo.isEm && emNum !== 1) return n, sNest, memo
|
|
211
328
|
//console.log('n: ' + n + ' [em]: inlines[n].len: ' + inlines[n].len + ', i: ' + i, ', inlines[i].len: ' + inlines[i].len + ', isEm: ' + memo.isEm)
|
|
@@ -213,7 +330,7 @@ const setEm = (inlines, marks, n, memo, sNest) => {
|
|
|
213
330
|
|
|
214
331
|
if (memo.isEm && inlines[i].len === 2) {
|
|
215
332
|
strongPNum++
|
|
216
|
-
i
|
|
333
|
+
i++
|
|
217
334
|
continue
|
|
218
335
|
}
|
|
219
336
|
|
|
@@ -226,6 +343,11 @@ const setEm = (inlines, marks, n, memo, sNest) => {
|
|
|
226
343
|
if (nest === -1) return n, nest, memo
|
|
227
344
|
|
|
228
345
|
if (emNum === 1) {
|
|
346
|
+
//console.log(n, i, 'insideTagsIsClose: ' + insideTagsIsClose)
|
|
347
|
+
if (memo.html && !insideTagsIsClose && inlines[i].len !== 2) {
|
|
348
|
+
memo.htmlTags = {}
|
|
349
|
+
return n, nest, memo
|
|
350
|
+
}
|
|
229
351
|
//console.log('n: ' + n + ' [em]: normal push, nest: ' + nest)
|
|
230
352
|
//console.log('strongPNum: ' + strongPNum)
|
|
231
353
|
//console.log(inlines[n].ep, inlines[n].sp, inlines[n].s)
|
|
@@ -249,9 +371,8 @@ const setEm = (inlines, marks, n, memo, sNest) => {
|
|
|
249
371
|
if (inlines[n].len === 0) return n, nest, memo
|
|
250
372
|
}
|
|
251
373
|
|
|
252
|
-
i
|
|
374
|
+
i++
|
|
253
375
|
}
|
|
254
|
-
|
|
255
376
|
return n, nest, memo
|
|
256
377
|
}
|
|
257
378
|
|
|
@@ -302,90 +423,125 @@ const checkNest = (inlines, marks, n, i) => {
|
|
|
302
423
|
return nest
|
|
303
424
|
}
|
|
304
425
|
|
|
305
|
-
const createMarks = (inlines,
|
|
426
|
+
const createMarks = (inlines, start, end, memo) => {
|
|
306
427
|
let marks = []
|
|
307
|
-
let n =
|
|
308
|
-
while (n <
|
|
309
|
-
if (inlines[n].type !== '') { n
|
|
310
|
-
|
|
311
|
-
memo.isEm = inlines[n].len === 1 ? true : false
|
|
428
|
+
let n = start
|
|
429
|
+
while (n < end) {
|
|
430
|
+
if (inlines[n].type !== '' || inlines[n].len === 0) { n++; continue }
|
|
431
|
+
memo.isEm = inlines[n].len === 1 ? true : false
|
|
312
432
|
memo.wrapEm = 0
|
|
313
433
|
let nest = 0
|
|
314
|
-
|
|
434
|
+
//console.log('n: ' + n + ' ----- inlines.length: ' + inlines.length + ', memo.isEm: ' + memo.isEm)
|
|
315
435
|
if (!memo.isEm) {
|
|
316
436
|
n, nest, memo = setStrong(inlines, marks, n, memo)
|
|
317
437
|
}
|
|
318
438
|
n, nest, memo = setEm(inlines, marks, n, memo)
|
|
319
439
|
|
|
320
440
|
if (inlines[n].len !== 0) setText(inlines, marks, n, nest)
|
|
321
|
-
n
|
|
441
|
+
n++
|
|
322
442
|
}
|
|
323
443
|
return marks
|
|
324
444
|
}
|
|
325
445
|
|
|
326
|
-
const
|
|
327
|
-
const max = state.posMax
|
|
328
|
-
const start = state.pos
|
|
329
|
-
if (silent) return false
|
|
330
|
-
if (start > max) return false
|
|
331
|
-
if (state.src.charCodeAt(start) !== 0x2A) return false
|
|
332
|
-
if (hasSlashFlag(state, start)) return false
|
|
333
|
-
//console.log('state.src.length: ' + state.src.length + ', start: ' + start + ', state.src: ' + state.src)
|
|
334
|
-
let inlines = crateInlines(state, start, max)
|
|
335
|
-
//console.log('inlines: ')
|
|
336
|
-
//console.log(inlines)
|
|
337
|
-
|
|
338
|
-
const memo = {
|
|
339
|
-
isEm: false,
|
|
340
|
-
hasEmThatWrapStrong: false,
|
|
341
|
-
noSetStrongEnd: false,
|
|
342
|
-
inlineMarkStart: state.src.charCodeAt(0) === 0x2A ? true : false,
|
|
343
|
-
inlineMarkEnd: state.src.charCodeAt(max - 1) === 0x2A ? true : false,
|
|
344
|
-
}
|
|
345
|
-
let marks = createMarks(inlines, 0, inlines.length, memo)
|
|
346
|
-
//console.log('marks: ')
|
|
347
|
-
//console.log(marks)
|
|
348
|
-
|
|
446
|
+
const fixInlines = (inlines, marks) => {
|
|
349
447
|
let n = 0
|
|
350
448
|
while (n < inlines.length) {
|
|
351
449
|
if (inlines[n].type !== '') { n++; continue }
|
|
352
450
|
let i = 0
|
|
353
451
|
//console.log('n: ' + n + ', inlines[n].s: ' + inlines[n].s + ', inlines[n].e: ' + inlines[n].e)
|
|
354
|
-
let c = 0
|
|
355
452
|
while (i < marks.length) {
|
|
356
|
-
//console.log(marks[i].s, inlines[n].e, marks[i].e, inlines[n].e)
|
|
453
|
+
//console.log(marks[i].type, marks[i].s, inlines[n].e, marks[i].e, inlines[n].e)
|
|
357
454
|
//console.log(marks[i].s >= inlines[n].s , marks[i].e <= inlines[n].e)
|
|
358
455
|
if (marks[i].s >= inlines[n].s && marks[i].e <= inlines[n].e) {
|
|
359
456
|
//console.log('n: ' + n + ', i: ' + i + ', marks[i].type: ' + marks[i].type)
|
|
360
457
|
inlines.splice(n + i + 1, 0, marks[i])
|
|
361
|
-
c++
|
|
362
458
|
i++
|
|
363
459
|
continue
|
|
364
460
|
}
|
|
365
461
|
break
|
|
366
462
|
}
|
|
367
463
|
if (marks.length) {
|
|
368
|
-
marks.splice(0,
|
|
464
|
+
marks.splice(0, i)
|
|
369
465
|
inlines.splice(n, 1)
|
|
370
|
-
n +=
|
|
466
|
+
n += i
|
|
371
467
|
} else {
|
|
372
|
-
inlines[n].type = 'text'
|
|
468
|
+
//if (inlines[n].type === '') inlines[n].type = 'text'
|
|
373
469
|
n++
|
|
374
470
|
}
|
|
375
471
|
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const strongJa = (state, silent, opt) => {
|
|
475
|
+
if (silent) return false
|
|
476
|
+
const start = state.pos
|
|
477
|
+
let max = state.posMax
|
|
478
|
+
const hasCurlyAttributes = state.md.core.ruler.__rules__.filter(rule => {
|
|
479
|
+
rule.name === 'curly_attributes' // markdown-it-attrs
|
|
480
|
+
})
|
|
481
|
+
let attributesSrc
|
|
482
|
+
if (hasCurlyAttributes) {
|
|
483
|
+
attributesSrc = state.src.match(/( *){.*?}$/)
|
|
484
|
+
if (attributesSrc) {
|
|
485
|
+
max = state.src.slice(0, attributesSrc.index).length
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (start > max) return false
|
|
489
|
+
if (state.src.charCodeAt(start) !== 0x2A) return false
|
|
490
|
+
|
|
491
|
+
if (hasBackslash(state, start)) return false
|
|
492
|
+
//console.log('state.src.length: ' + state.src.length + ', start: ' + start + ', state.src: ' + state.src)
|
|
493
|
+
let inlines = createInlines(state, start, max, opt)
|
|
494
|
+
//console.log('inlines: ')
|
|
495
|
+
//console.log(inlines)
|
|
496
|
+
|
|
497
|
+
const memo = {
|
|
498
|
+
isEm: false,
|
|
499
|
+
hasEmThatWrapStrong: false,
|
|
500
|
+
noSetStrongEnd: false,
|
|
501
|
+
html: state.md.options.html,
|
|
502
|
+
htmlTags: {},
|
|
503
|
+
inlineMarkStart: state.src.charCodeAt(0) === 0x2A ? true : false,
|
|
504
|
+
inlineMarkEnd: state.src.charCodeAt(max - 1) === 0x2A ? true : false,
|
|
505
|
+
}
|
|
506
|
+
/*
|
|
507
|
+
if (memo.html) {
|
|
508
|
+
let beforeInlines = createInlines(state, 0, start - 1, opt)
|
|
509
|
+
isJumpTag(beforeInlines, beforeInlines.length - 1, memo)
|
|
510
|
+
}*/
|
|
511
|
+
let marks = createMarks(inlines, 0, inlines.length, memo)
|
|
512
|
+
//console.log('marks: ')
|
|
513
|
+
//console.log(marks)
|
|
376
514
|
|
|
515
|
+
fixInlines(inlines, marks)
|
|
377
516
|
//console.log('fix inlines:')
|
|
378
517
|
//console.log(inlines)
|
|
518
|
+
|
|
379
519
|
setToken(state, inlines)
|
|
380
520
|
|
|
381
|
-
|
|
382
|
-
|
|
521
|
+
if (attributesSrc) {
|
|
522
|
+
//console.log('attributesSrc[1].length: ' + attributesSrc[1].length)
|
|
523
|
+
if (attributesSrc[1].length > 1) {
|
|
524
|
+
state.pos = max + attributesSrc[1].length
|
|
525
|
+
} else {
|
|
526
|
+
state.pos = max
|
|
527
|
+
}
|
|
528
|
+
} else {
|
|
529
|
+
state.pos = max + 1
|
|
530
|
+
}
|
|
383
531
|
return true
|
|
384
532
|
}
|
|
385
533
|
|
|
386
|
-
const mditStrongJa = (md) => {
|
|
534
|
+
const mditStrongJa = (md, option) => {
|
|
535
|
+
const opt = {
|
|
536
|
+
dollarMath: true,
|
|
537
|
+
}
|
|
538
|
+
if (option !== undefined) {
|
|
539
|
+
for (let o in option) {
|
|
540
|
+
opt[o] = option[o]
|
|
541
|
+
}
|
|
542
|
+
}
|
|
387
543
|
md.inline.ruler.before('emphasis', 'strong_ja', (state, silent) => {
|
|
388
|
-
return strongJa(state, silent)
|
|
544
|
+
return strongJa(state, silent, opt)
|
|
389
545
|
})
|
|
390
546
|
}
|
|
391
547
|
export default mditStrongJa
|
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.3.
|
|
4
|
+
"version": "0.3.3",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"author": "peaceroad <peaceroad@gmail.com>",
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"markdown-it": "^14.1.0"
|
|
14
|
+
"markdown-it": "^14.1.0",
|
|
15
|
+
"markdown-it-attrs": "^4.2.0"
|
|
15
16
|
}
|
|
16
17
|
}
|
package/test/example-complex.txt
CHANGED
|
@@ -139,3 +139,122 @@ a[*](https://example.com)*b
|
|
|
139
139
|
a[*](https://example*.com)*b*
|
|
140
140
|
[HTML]
|
|
141
141
|
<p>a<a href="https://example*.com">*</a><em>b</em></p>
|
|
142
|
+
|
|
143
|
+
[Markdown]
|
|
144
|
+
**重要な文・文節や語句**は`**`を使って囲みます。*文脈上強調する語句*は`*`を使って囲みます。
|
|
145
|
+
[HTML]
|
|
146
|
+
<p><strong>重要な文・文節や語句</strong>は<code>**</code>を使って囲みます。<em>文脈上強調する語句</em>は<code>*</code>を使って囲みます。</p>
|
|
147
|
+
|
|
148
|
+
[Markdown]
|
|
149
|
+
インラインコードは`*`と`*`で*囲みます*。
|
|
150
|
+
[HTML]
|
|
151
|
+
<p>インラインコードは<code>*</code>と<code>*</code>で<em>囲みます</em>。</p>
|
|
152
|
+
|
|
153
|
+
[Markdown]
|
|
154
|
+
[node_modules/*](#)の*はすべてを意味します。
|
|
155
|
+
[HTML]
|
|
156
|
+
<p><a href="#">node_modules/*</a>の*はすべてを意味します。</p>
|
|
157
|
+
|
|
158
|
+
[Markdown]
|
|
159
|
+
**test** {.style}
|
|
160
|
+
[HTML]
|
|
161
|
+
<p class="style"><strong>test</strong></p>
|
|
162
|
+
|
|
163
|
+
[Markdown]
|
|
164
|
+
a**test**b {.style}
|
|
165
|
+
[HTML]
|
|
166
|
+
<p class="style">a<strong>test</strong>b</p>
|
|
167
|
+
|
|
168
|
+
[Markdown]
|
|
169
|
+
z*a**test**{.style}b*c*d
|
|
170
|
+
[HTML]
|
|
171
|
+
<p>z<em>a<strong class="style">test</strong>b</em>c*d</p>
|
|
172
|
+
|
|
173
|
+
[Markdown]
|
|
174
|
+
a**b**c{.style}
|
|
175
|
+
[HTML]
|
|
176
|
+
<p class="style">a<strong>b</strong>c</p>
|
|
177
|
+
|
|
178
|
+
[Markdown]
|
|
179
|
+
a**b**{.style}
|
|
180
|
+
[HTML]
|
|
181
|
+
<p>a<strong class="style">b</strong></p>
|
|
182
|
+
|
|
183
|
+
[Markdown]
|
|
184
|
+
a**b**c {.style}
|
|
185
|
+
[HTML]
|
|
186
|
+
<p class="style">a<strong>b</strong>c</p>
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
[Markdown]
|
|
190
|
+
z*a<span>b*c</span>d*e
|
|
191
|
+
[HTML]
|
|
192
|
+
<p>z<em>a<span>b</em>c</span>d*e</p>
|
|
193
|
+
[HTML:true]
|
|
194
|
+
<p>z*a<span>b*c</span>d*e</p>
|
|
195
|
+
|
|
196
|
+
[Markdown]
|
|
197
|
+
a<span>b*c</span>d*e*f
|
|
198
|
+
[HTML]
|
|
199
|
+
<p>a<span>b<em>c</span>d</em>e*f</p>
|
|
200
|
+
[HTML:true]
|
|
201
|
+
<p>a<span>b*c</span>d<em>e</em>f</p>
|
|
202
|
+
|
|
203
|
+
[Markdown]
|
|
204
|
+
a*a<span>b</span>c*c
|
|
205
|
+
[HTML:false]
|
|
206
|
+
<p>a<em>a<span>b</span>c</em>c</p>
|
|
207
|
+
[HTML:true]
|
|
208
|
+
<p>a<em>a<span>b</span>c</em>c</p>
|
|
209
|
+
|
|
210
|
+
[Markdown]
|
|
211
|
+
aa<span>b*ef*</span>cc
|
|
212
|
+
[HTML:false]
|
|
213
|
+
<p>aa<span>b<em>ef</em></span>cc</p>
|
|
214
|
+
[HTML:true]
|
|
215
|
+
<p>aa<span>b<em>ef</em></span>cc</p>
|
|
216
|
+
|
|
217
|
+
[Markdown]
|
|
218
|
+
a*a<span>b*ef*</span>c*c
|
|
219
|
+
[HTML:false]
|
|
220
|
+
<p>a<em>a<span>b</em>ef<em></span>c</em>c</p>
|
|
221
|
+
[HTML:true, commonmark: <p>a<em>a<span>b</em>ef*</span>c*c</p>]
|
|
222
|
+
<p>a<em>a<span>b<em>ef</em></span>c</em>c</p>
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
[Markdown]
|
|
226
|
+
a***a<span>b</span>c***c
|
|
227
|
+
[HTML:false]
|
|
228
|
+
<p>a<em><strong>a<span>b</span>c</strong></em>c</p>
|
|
229
|
+
[HTML:true]
|
|
230
|
+
<p>a<em><strong>a<span>b</span>c</strong></em>c</p>
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
[Markdown]
|
|
234
|
+
a***a<span>b***e</span>cc
|
|
235
|
+
[HTML:false]
|
|
236
|
+
<p>a<em><strong>a<span>b</strong></em>e</span>cc</p>
|
|
237
|
+
[HTML:true]
|
|
238
|
+
<p>a***a<span>b***e</span>cc</p>
|
|
239
|
+
|
|
240
|
+
[Markdown]
|
|
241
|
+
aa<span>b*e<s>f</s>*</span>cc
|
|
242
|
+
[HTML:false]
|
|
243
|
+
<p>aa<span>b<em>e<s>f</s></em></span>cc</p>
|
|
244
|
+
[HTML:true]
|
|
245
|
+
<p>aa<span>b<em>e<s>f</s></em></span>cc</p>
|
|
246
|
+
|
|
247
|
+
[Markdown]
|
|
248
|
+
**aa<span>b*e<s>f</s>*</span>cc**
|
|
249
|
+
[HTML:false]
|
|
250
|
+
<p><strong>aa<span>b<em>e<s>f</s></em></span>cc</strong></p>
|
|
251
|
+
[HTML:true]
|
|
252
|
+
<p><strong>aa<span>b<em>e<s>f</s></em></span>cc</strong></p>
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
[Markdown]
|
|
256
|
+
*aa<span>b**e<s>f</s>**</span>cc*
|
|
257
|
+
[HTML:false]
|
|
258
|
+
<p><em>aa<span>b<strong>e<s>f</s></strong></span>cc</em></p>
|
|
259
|
+
[HTML:true]
|
|
260
|
+
<p><em>aa<span>b<strong>e<s>f</s></strong></span>cc</em></p>
|
package/test/example-strong.txt
CHANGED
package/test/test.js
CHANGED
|
@@ -4,16 +4,18 @@ import path from 'path'
|
|
|
4
4
|
import url from 'url'
|
|
5
5
|
|
|
6
6
|
import mdit from 'markdown-it'
|
|
7
|
+
import mditAttrs from 'markdown-it-attrs'
|
|
7
8
|
import mditStrongJa from '../index.js'
|
|
8
9
|
|
|
9
10
|
const __dirname = path.dirname(url.fileURLToPath(import.meta.url)).replace(/\\/g, '/')
|
|
10
11
|
|
|
11
12
|
const check = (ms, example) => {
|
|
12
|
-
const md = mdit().use(mditStrongJa)
|
|
13
|
-
const mdWithHtml = mdit({html: true}).use(mditStrongJa)
|
|
13
|
+
const md = mdit().use(mditStrongJa).use(mditAttrs)
|
|
14
|
+
const mdWithHtml = mdit({html: true}).use(mditStrongJa).use(mditAttrs)
|
|
14
15
|
let n = 1
|
|
15
16
|
while (n < ms.length) {
|
|
16
|
-
|
|
17
|
+
//if (n !== 43 ) { n++; continue }
|
|
18
|
+
//if (n !== 36 ) { n++; continue }
|
|
17
19
|
const m = ms[n].markdown
|
|
18
20
|
console.log('Test [' + n + ', HTML: false] >>>')
|
|
19
21
|
const h = md.render(m)
|
|
@@ -22,7 +24,6 @@ const check = (ms, example) => {
|
|
|
22
24
|
} catch(e) {
|
|
23
25
|
console.log('Input: ' + ms[n].markdown + '\nConvert: ' + h + 'Correct: ' + ms[n].html)
|
|
24
26
|
}
|
|
25
|
-
|
|
26
27
|
if (ms[n].htmlWithHtmlTrue) {
|
|
27
28
|
console.log('Test [' + n + ', HTML: true] >>>')
|
|
28
29
|
const hh = mdWithHtml.render(m)
|