@peaceroad/markdown-it-strong-ja 0.3.4 → 0.3.6
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/README.md +35 -4
- package/index.js +189 -77
- package/package.json +2 -1
- package/test/example-complex.txt +34 -9
- package/test/example-with-linebreak.txt +282 -0
- package/test/mditNoAttrs/example-complex.txt +393 -0
- package/test/mditNoAttrs/example-em.txt +205 -0
- package/test/mditNoAttrs/example-strong.txt +219 -0
- package/test/mditNoAttrs/example-with-linebreak.txt +289 -0
- package/test/test.js +70 -31
package/README.md
CHANGED
|
@@ -2,22 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
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
4
|
|
|
5
|
-
Notice: this is slightly different from the commonmark processing.
|
|
6
|
-
|
|
7
5
|
## Use
|
|
8
6
|
|
|
9
7
|
```js
|
|
10
8
|
import mdit from 'markdown-it'
|
|
11
9
|
import mditStrongJa from '@peaceroad/markdown-it-strong-ja'
|
|
12
|
-
|
|
10
|
+
import mditAttrs from 'markdown-it-attrs'
|
|
11
|
+
const md = mdit().use(mditStrongJa).use(mditAttrs)
|
|
13
12
|
|
|
14
13
|
md.render('HTMLは**「HyperText Markup Language」**の略です。')
|
|
15
14
|
// <p>HTMLは<strong>「HyperText Markup Language」</strong>の略です。</p>
|
|
16
15
|
|
|
16
|
+
|
|
17
17
|
md.render('HTMLは*「HyperText Markup Language」*の略です。')
|
|
18
18
|
// <p>HTMLは<em>「HyperText Markup Language」</em>の略です。</p>
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
+
Notice. Basically, it is assumed that you will use markdown-it-attrs in conjunction with this. If you do not use it, please use `use(mditStrongJa, {mditAttrs: false})`.
|
|
22
|
+
|
|
21
23
|
## Example
|
|
22
24
|
|
|
23
25
|
The following examples is for strong. The process for em is roughly the same.
|
|
@@ -150,4 +152,33 @@ a****b
|
|
|
150
152
|
a****
|
|
151
153
|
[HTML]
|
|
152
154
|
<p>a****</p>
|
|
153
|
-
~~~
|
|
155
|
+
~~~
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
Warning. Commonmark converts it as follows, but the current plugin cannot convert it. (It converts it based on the first *. I don't think it's that bad in terms of appearance...)
|
|
160
|
+
|
|
161
|
+
~~~
|
|
162
|
+
[Markdown]
|
|
163
|
+
z*<>*a*b
|
|
164
|
+
[HTML:false]
|
|
165
|
+
<p>z*<><em>a</em>b</p>
|
|
166
|
+
[HTML:true]
|
|
167
|
+
<p>z*<><em>a</em>b</p>
|
|
168
|
+
|
|
169
|
+
[Markdown]
|
|
170
|
+
z*<span>*a*b
|
|
171
|
+
[HTML:false]
|
|
172
|
+
<p>z*<span><em>a</em>b</p>
|
|
173
|
+
[HTML:true]
|
|
174
|
+
<p>z*<span><em>a</em>b</p>
|
|
175
|
+
|
|
176
|
+
[Markdown]
|
|
177
|
+
z*<span>*a*b</span>
|
|
178
|
+
[HTML:false]
|
|
179
|
+
<p>z*<span><em>a</em>b</span></p>
|
|
180
|
+
[HTML:true]
|
|
181
|
+
<p>z*<span><em>a</em>b</span></p>
|
|
182
|
+
~~~
|
|
183
|
+
|
|
184
|
+
---
|
package/index.js
CHANGED
|
@@ -2,44 +2,91 @@ const hasBackslash = (state, start) => {
|
|
|
2
2
|
let slashNum = 0
|
|
3
3
|
let i = start - 1
|
|
4
4
|
while(i >= 0) {
|
|
5
|
-
/// if (state.src.charCodeAt(i) === 0x2A) { i--; continue }
|
|
6
5
|
if (state.src.charCodeAt(i) === 0x5C) { slashNum++; i--; continue }
|
|
7
6
|
break
|
|
8
7
|
}
|
|
9
8
|
return slashNum % 2 === 1 ? true : false
|
|
10
9
|
}
|
|
11
10
|
|
|
12
|
-
const setToken = (state, inlines) => {
|
|
11
|
+
const setToken = (state, inlines, opt) => {
|
|
13
12
|
let i = 0
|
|
13
|
+
let attrsIsText = {
|
|
14
|
+
val: false,
|
|
15
|
+
tag: '',
|
|
16
|
+
}
|
|
14
17
|
while (i < inlines.length) {
|
|
15
18
|
let type = inlines[i].type
|
|
19
|
+
//console.log(i, type)
|
|
16
20
|
const tag = type.replace(/(?:_open|_close)$/, '')
|
|
17
21
|
|
|
18
22
|
if (/_open$/.test(type)) {
|
|
19
23
|
const startToken = state.push(type, tag, 1)
|
|
20
24
|
startToken.markup = tag === 'strong' ? '**' : '*'
|
|
25
|
+
attrsIsText = {
|
|
26
|
+
val: true,
|
|
27
|
+
tag: tag,
|
|
28
|
+
}
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
if (type === 'html_inline') {
|
|
24
32
|
type = 'text'
|
|
25
33
|
}
|
|
26
34
|
if (type === 'text') {
|
|
27
|
-
|
|
28
|
-
|
|
35
|
+
let content = state.src.slice(inlines[i].s, inlines[i].e + 1)
|
|
36
|
+
//console.log('content: ' + content)
|
|
37
|
+
if (/^\*+$/.test(content)) {
|
|
29
38
|
//console.log('asterisk process::')
|
|
30
39
|
const asteriskToken = state.push(type, '', 0)
|
|
31
40
|
asteriskToken.content = content
|
|
32
41
|
i++
|
|
33
42
|
continue
|
|
34
43
|
}
|
|
44
|
+
if (opt.mditAttrs && attrsIsText.val && i + 1 < inlines.length) {
|
|
45
|
+
const hasImmediatelyAfterAsteriskClose = inlines[i+1].type === attrsIsText.tag + '_close'
|
|
46
|
+
//console.log(hasImmediatelyAfterAsteriskClose, inlines[i+1].type, /^[\s\S]*{[^{}\n!@#%^&*()]+?}$/.test(content))
|
|
47
|
+
if (hasImmediatelyAfterAsteriskClose && /{[^{}\n!@#%^&*()]+?}$/.test(content)) {
|
|
48
|
+
const attrsToken = state.push(type, '', 0)
|
|
49
|
+
|
|
50
|
+
const hasBackslashBeforeCurlyAttribute = content.match(/(\\+){/)
|
|
51
|
+
if (hasBackslashBeforeCurlyAttribute) {
|
|
52
|
+
if (hasBackslashBeforeCurlyAttribute[1].length === 1) {
|
|
53
|
+
attrsToken.content = content.replace(/\\{/, '{')
|
|
54
|
+
} else {
|
|
55
|
+
let backSlashNum = Math.floor(hasBackslashBeforeCurlyAttribute[1].length / 2)
|
|
56
|
+
let k = 0
|
|
57
|
+
let backSlash = ''
|
|
58
|
+
while (k < backSlashNum) {
|
|
59
|
+
backSlash += '\\'
|
|
60
|
+
k++
|
|
61
|
+
}
|
|
62
|
+
//console.log(backSlashNum, backSlash)
|
|
63
|
+
attrsToken.content = content.replace(/\\+{/, backSlash + '{')
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
attrsToken.content = content
|
|
67
|
+
}
|
|
68
|
+
attrsIsText.val = false
|
|
69
|
+
i++
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
}
|
|
35
73
|
|
|
36
74
|
const childTokens = state.md.parseInline(content, state.env)
|
|
75
|
+
//console.log(childTokens)
|
|
76
|
+
//console.log(childTokens[0].children)
|
|
37
77
|
if (childTokens[0] && childTokens[0].children) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
78
|
+
let j = 0
|
|
79
|
+
while (j < childTokens[0].children.length) {
|
|
80
|
+
const t = childTokens[0].children[j]
|
|
81
|
+
if (t.type === 'softbreak') {
|
|
82
|
+
t.type = 'text'
|
|
83
|
+
t.tag = ''
|
|
84
|
+
t.content = '\n'
|
|
85
|
+
}
|
|
86
|
+
if (!opt.mditAttrs && t.tag === 'br') {
|
|
87
|
+
t.tag = ''
|
|
88
|
+
t.content = '\n'
|
|
89
|
+
}
|
|
43
90
|
const token = state.push(t.type, t.tag, t.nesting)
|
|
44
91
|
token.attrs = t.attrs
|
|
45
92
|
token.map = t.map
|
|
@@ -51,13 +98,18 @@ const setToken = (state, inlines) => {
|
|
|
51
98
|
token.meta = t.meta
|
|
52
99
|
token.block = t.block
|
|
53
100
|
token.hidden = t.hidden
|
|
54
|
-
|
|
101
|
+
j++
|
|
102
|
+
}
|
|
55
103
|
}
|
|
56
104
|
}
|
|
57
105
|
|
|
58
106
|
if (/_close$/.test(type)) {
|
|
59
107
|
const closeToken = state.push(type, tag, -1)
|
|
60
108
|
closeToken.markup = tag === 'strong' ? '**' : '*'
|
|
109
|
+
attrsIsText = {
|
|
110
|
+
val: false,
|
|
111
|
+
tag: '',
|
|
112
|
+
}
|
|
61
113
|
}
|
|
62
114
|
|
|
63
115
|
i++
|
|
@@ -85,32 +137,40 @@ const hasNextSymbol = (state, n, max, symbol, noMark) => {
|
|
|
85
137
|
while (i < max) {
|
|
86
138
|
tempNoMark += state.src[i]
|
|
87
139
|
if (state.src.charCodeAt(i) === symbol && !hasBackslash(state, i)) {
|
|
88
|
-
noMark += state.src
|
|
140
|
+
noMark += state.src.substring(n, i + 1)
|
|
89
141
|
nextSymbolPos = i
|
|
90
142
|
break
|
|
91
143
|
}
|
|
92
144
|
i++
|
|
93
145
|
}
|
|
94
146
|
}
|
|
95
|
-
return nextSymbolPos
|
|
147
|
+
return [nextSymbolPos, noMark]
|
|
96
148
|
}
|
|
97
149
|
|
|
98
150
|
const createInlines = (state, start, max, opt) => {
|
|
99
151
|
let n = start
|
|
100
152
|
let inlines = []
|
|
101
153
|
let noMark = ''
|
|
102
|
-
let isInStartMark = true
|
|
103
154
|
let textStart = n
|
|
104
|
-
|
|
105
155
|
while (n < max) {
|
|
106
|
-
|
|
156
|
+
//console.log('n: ' + n + ', state.src[n]: ' + state.src[n] + ', noMark: ' + noMark)
|
|
157
|
+
let nextSymbolPos = -1;
|
|
158
|
+
[nextSymbolPos, noMark] = hasNextSymbol(state, n, max, 0x60, noMark) // '`'
|
|
107
159
|
if (nextSymbolPos !== -1) {
|
|
160
|
+
if (nextSymbolPos === max - 1) {
|
|
161
|
+
inlinesPush(inlines, textStart, nextSymbolPos, nextSymbolPos - textStart + 1, 'text')
|
|
162
|
+
break
|
|
163
|
+
}
|
|
108
164
|
n = nextSymbolPos + 1
|
|
109
165
|
continue
|
|
110
166
|
}
|
|
111
167
|
if (opt.dollarMath) {
|
|
112
|
-
nextSymbolPos = hasNextSymbol(state, n, max, 0x24, noMark) // '$'
|
|
168
|
+
[nextSymbolPos, noMark] = hasNextSymbol(state, n, max, 0x24, noMark) // '$'
|
|
113
169
|
if (nextSymbolPos !== -1) {
|
|
170
|
+
if (nextSymbolPos === max - 1) {
|
|
171
|
+
inlinesPush(inlines, textStart, nextSymbolPos, nextSymbolPos - textStart + 1, 'text')
|
|
172
|
+
break
|
|
173
|
+
}
|
|
114
174
|
n = nextSymbolPos + 1
|
|
115
175
|
continue
|
|
116
176
|
}
|
|
@@ -122,7 +182,9 @@ const createInlines = (state, start, max, opt) => {
|
|
|
122
182
|
while (i < max) {
|
|
123
183
|
if (state.src.charCodeAt(i) === 0x3E && !hasBackslash(state, i)) { // '>'
|
|
124
184
|
if (noMark.length !== 0) {
|
|
185
|
+
// Add the text before the tag to inlines
|
|
125
186
|
inlinesPush(inlines, textStart, n - 1, n - textStart, 'text')
|
|
187
|
+
noMark = ''
|
|
126
188
|
}
|
|
127
189
|
let tag = state.src.slice(n + 1, i)
|
|
128
190
|
let tagType = ''
|
|
@@ -144,8 +206,18 @@ const createInlines = (state, start, max, opt) => {
|
|
|
144
206
|
}
|
|
145
207
|
|
|
146
208
|
if (state.src.charCodeAt(n) === 0x2A && !hasBackslash(state, n)) { // '*'
|
|
147
|
-
|
|
209
|
+
/*
|
|
210
|
+
if (/[!-)+-/:-@[-`{-~]/.test(state.src[n + 1])) {
|
|
211
|
+
inlinesPush(inlines, textStart, n, n - textStart + 1, 'text')
|
|
212
|
+
noMark = ''
|
|
213
|
+
n++
|
|
214
|
+
textStart = n
|
|
215
|
+
continue
|
|
216
|
+
}*/
|
|
217
|
+
if (n !== 0) {
|
|
218
|
+
//Add text before asterisk to inlines
|
|
148
219
|
inlinesPush(inlines, textStart, n - 1, n - textStart, 'text')
|
|
220
|
+
noMark = ''
|
|
149
221
|
}
|
|
150
222
|
if (n === max - 1) {
|
|
151
223
|
inlinesPush(inlines, n, n, 1 , '')
|
|
@@ -165,10 +237,9 @@ const createInlines = (state, start, max, opt) => {
|
|
|
165
237
|
n = i
|
|
166
238
|
continue
|
|
167
239
|
}
|
|
168
|
-
|
|
240
|
+
|
|
169
241
|
noMark += state.src[n]
|
|
170
|
-
|
|
171
|
-
if (n === max - 1 || max < 3) {
|
|
242
|
+
if (n === max - 1) {
|
|
172
243
|
inlinesPush(inlines, textStart, n, n - textStart + 1, 'text')
|
|
173
244
|
break
|
|
174
245
|
}
|
|
@@ -196,16 +267,17 @@ const marksPush = (marks, nest, s, e, len, outsideLen, type) => {
|
|
|
196
267
|
}
|
|
197
268
|
}
|
|
198
269
|
|
|
199
|
-
const setStrong = (inlines, marks, n, memo) => {
|
|
270
|
+
const setStrong = (state, inlines, marks, n, memo, opt) => {
|
|
200
271
|
let i = n + 1
|
|
201
272
|
let j = 0
|
|
202
273
|
let nest = 0
|
|
203
274
|
let insideTagsIsClose = 1
|
|
275
|
+
let prevHtmlTags = {...memo.htmlTags}
|
|
204
276
|
while (i < inlines.length) {
|
|
205
277
|
if (inlines[i].len === 0) { i++; continue }
|
|
206
278
|
if (memo.html) {
|
|
207
279
|
if (inlines[i].type === 'html_inline') {
|
|
208
|
-
insideTagsIsClose = isJumpTag(inlines, i, memo)
|
|
280
|
+
insideTagsIsClose = isJumpTag(inlines, i, memo, prevHtmlTags)
|
|
209
281
|
//console.log('insideTagsIsClose: ' + insideTagsIsClose )
|
|
210
282
|
if (insideTagsIsClose === -1) return n, nest, memo
|
|
211
283
|
if (insideTagsIsClose === 0) { i++; continue }
|
|
@@ -236,9 +308,11 @@ const setStrong = (inlines, marks, n, memo) => {
|
|
|
236
308
|
}
|
|
237
309
|
|
|
238
310
|
//console.log('memo.html: ' + memo.html + 'insideTagsIsClose: ' + insideTagsIsClose + 'inlines[i].len: ' + inlines[i].len)
|
|
239
|
-
if (memo.html && !insideTagsIsClose && inlines[i].len !== 1) {
|
|
240
|
-
|
|
311
|
+
//if (memo.html && !insideTagsIsClose && inlines[i].len !== 1) {
|
|
312
|
+
if (memo.html && inlines[i].len < 2) {
|
|
313
|
+
i++; continue;
|
|
241
314
|
}
|
|
315
|
+
|
|
242
316
|
let strongNum = Math.trunc(Math.min(inlines[n].len, inlines[i].len) / 2)
|
|
243
317
|
|
|
244
318
|
if (inlines[i].len > 1) {
|
|
@@ -262,13 +336,13 @@ const setStrong = (inlines, marks, n, memo) => {
|
|
|
262
336
|
if ((inlines[n].len > 0 && inlines[i] === 1) || (inlines[n].len === 1 && inlines[i].len > 0)) {
|
|
263
337
|
//console.log('check em that warp strong.')
|
|
264
338
|
nest++
|
|
265
|
-
n, nest, memo = setEm(inlines, marks, n, memo, nest)
|
|
339
|
+
n, nest, memo = setEm(state, inlines, marks, n, memo, opt, nest)
|
|
266
340
|
if (memo.hasEmThatWrapStrong) {
|
|
267
341
|
//console.log('set em that wrap strong.')
|
|
268
342
|
let k = 0
|
|
269
343
|
while (k < strongNum) {
|
|
270
|
-
|
|
271
|
-
|
|
344
|
+
marks[marks.length - 2 - k * 2 - 1].nest += 1
|
|
345
|
+
marks[marks.length - 2 - k * 2].nest += 1
|
|
272
346
|
k++
|
|
273
347
|
}
|
|
274
348
|
}
|
|
@@ -279,49 +353,56 @@ const setStrong = (inlines, marks, n, memo) => {
|
|
|
279
353
|
return n, nest, memo
|
|
280
354
|
}
|
|
281
355
|
|
|
282
|
-
const isJumpTag = (inlines, n, memo) => {
|
|
356
|
+
const isJumpTag = (inlines, n, memo, prevHtmlTags) => {
|
|
283
357
|
//console.log(n, 'before::memo.htmlTags: ' + JSON.stringify(memo.htmlTags))
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if (!hasSet && !memo.htmlTags[inlines[n].tag[0]]) {
|
|
296
|
-
if (inlines[n].tag[1] === 'close') {
|
|
297
|
-
memo.htmlTags = {}
|
|
298
|
-
return -1
|
|
299
|
-
}
|
|
300
|
-
memo.htmlTags[inlines[n].tag[0]] = 1
|
|
358
|
+
if (inlines[n].tag === undefined) return 0
|
|
359
|
+
if (memo.htmlTags[inlines[n].tag[0]] === undefined) {
|
|
360
|
+
memo.htmlTags[inlines[n].tag[0]] = 0
|
|
361
|
+
}
|
|
362
|
+
//console.log('prevHtmlTags: ' + JSON.stringify(prevHtmlTags))
|
|
363
|
+
//console.log('memo.htmlTags: ' + JSON.stringify(memo.htmlTags) + ', inlines[n].tag[1]: ' + inlines[n].tag[1])
|
|
364
|
+
if (inlines[n].tag[1] === 'open') {
|
|
365
|
+
memo.htmlTags[inlines[n].tag[0]] += 1
|
|
366
|
+
}
|
|
367
|
+
if (inlines[n].tag[1] === 'close') {
|
|
368
|
+
memo.htmlTags[inlines[n].tag[0]] -= 1
|
|
301
369
|
}
|
|
302
|
-
|
|
370
|
+
//console.log('prevHtmlTags: ' + JSON.stringify(prevHtmlTags))
|
|
371
|
+
//console.log('memo.htmlTags: ' + JSON.stringify(memo.htmlTags))
|
|
372
|
+
if (prevHtmlTags[inlines[n].tag[0]] === undefined) prevHtmlTags[inlines[n].tag[0]] = 0
|
|
373
|
+
if (memo.htmlTags[inlines[n].tag[0]] < prevHtmlTags[inlines[n].tag[0]]) {
|
|
374
|
+
return -1
|
|
375
|
+
}
|
|
376
|
+
//console.log(n, 'after::memo.htmlTags: ' + JSON.stringify(memo.htmlTags))
|
|
303
377
|
const closeAllTags = Object.values(memo.htmlTags).every(val => val === 0)
|
|
304
378
|
//console.log('closeAllTags: ' + closeAllTags)
|
|
305
379
|
if (closeAllTags) return 1
|
|
380
|
+
// if (inlines[n].tag[1] === 'close') return -1
|
|
306
381
|
//memo.htmlTags = {}
|
|
307
382
|
return 0
|
|
308
383
|
}
|
|
309
384
|
|
|
310
|
-
const setEm = (inlines, marks, n, memo, sNest) => {
|
|
385
|
+
const setEm = (state, inlines, marks, n, memo, opt, sNest) => {
|
|
311
386
|
let i = n + 1
|
|
312
387
|
let nest = 0
|
|
313
388
|
let strongPNum = 0
|
|
314
|
-
let insideTagsIsClose = 1
|
|
389
|
+
let insideTagsIsClose = 1 //true
|
|
390
|
+
let prevHtmlTags = {...memo.htmlTags}
|
|
391
|
+
//console.log('memo.prevHtmlTags: ' + JSON.stringify(memo.prevHtmlTags))
|
|
315
392
|
while (i < inlines.length) {
|
|
393
|
+
//console.log('i: ' + i + ', src: ' + state.src.slice(inlines[i].sp, inlines[i].ep + 1) + ', inlines[i]: ' + JSON.stringify(inlines[i]))
|
|
316
394
|
if (inlines[i].len === 0) { i++; continue }
|
|
395
|
+
//console.log(' memo.isEm: ' + memo.isEm + ', memo.html: ' + memo.html + ', inlines[i].type: ' + inlines[i].type)
|
|
317
396
|
if (memo.isEm && memo.html) {
|
|
318
397
|
if (inlines[i].type === 'html_inline') {
|
|
319
|
-
insideTagsIsClose = isJumpTag(inlines, i, memo)
|
|
320
|
-
//console.log('insideTagsIsClose: ' + insideTagsIsClose
|
|
398
|
+
insideTagsIsClose = isJumpTag(inlines, i, memo, prevHtmlTags)
|
|
399
|
+
//console.log('insideTagsIsClose: ' + insideTagsIsClose)
|
|
321
400
|
if (insideTagsIsClose === -1) return n, nest, memo
|
|
322
401
|
if (insideTagsIsClose === 0) { i++; continue }
|
|
323
402
|
}
|
|
324
403
|
}
|
|
404
|
+
|
|
405
|
+
|
|
325
406
|
if (inlines[i].type !== '') { i++; continue }
|
|
326
407
|
|
|
327
408
|
const emNum = Math.min(inlines[n].len, inlines[i].len)
|
|
@@ -329,7 +410,23 @@ const setEm = (inlines, marks, n, memo, sNest) => {
|
|
|
329
410
|
//console.log('n: ' + n + ' [em]: inlines[n].len: ' + inlines[n].len + ', i: ' + i, ', inlines[i].len: ' + inlines[i].len + ', isEm: ' + memo.isEm)
|
|
330
411
|
//console.log(marks)
|
|
331
412
|
|
|
332
|
-
|
|
413
|
+
let curlyProcess = false
|
|
414
|
+
if (opt.mditAttrs) {
|
|
415
|
+
const checkText = state.src.slice(inlines[i-1].sp, inlines[i-1].ep + 1)
|
|
416
|
+
if (/{[^{}\n!@#%^&*()]+?}$/.test(checkText)) {
|
|
417
|
+
curlyProcess = true
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
//if (memo.isEm && !curlyProcess && inlines[i].len === 2 && !memo.inlineMarkStart) {
|
|
421
|
+
const hasMarkersAtStartAndEnd = (i) => {
|
|
422
|
+
let flag = memo.inlineMarkStart
|
|
423
|
+
if (!flag) return false
|
|
424
|
+
inlines.length - 1 === i ? flag = true : flag = false
|
|
425
|
+
if (!flag) return false
|
|
426
|
+
inlines[i].len > 1 ? flag = true : flag = false
|
|
427
|
+
return flag
|
|
428
|
+
}
|
|
429
|
+
if (memo.isEm && !curlyProcess && inlines[i].len === 2 && !hasMarkersAtStartAndEnd(i)) {
|
|
333
430
|
strongPNum++
|
|
334
431
|
i++
|
|
335
432
|
continue
|
|
@@ -344,12 +441,12 @@ const setEm = (inlines, marks, n, memo, sNest) => {
|
|
|
344
441
|
if (nest === -1) return n, nest, memo
|
|
345
442
|
|
|
346
443
|
if (emNum === 1) {
|
|
347
|
-
//console.log(n, i, 'insideTagsIsClose: ' + insideTagsIsClose)
|
|
348
|
-
if (memo.html &&
|
|
444
|
+
//console.log(n, i, 'insideTagsIsClose: ' + insideTagsIsClose, !insideTagsIsClose, inlines[i].len)
|
|
445
|
+
if (memo.html && inlines[i].len < 1) {
|
|
349
446
|
i++; continue;
|
|
350
447
|
}
|
|
351
|
-
|
|
352
|
-
//console.log('strongPNum: ' + strongPNum)
|
|
448
|
+
|
|
449
|
+
//console.log('n: ' + n + ' [em]: Normal push, nest: ' + nest, ', strongPNum: ' + strongPNum)
|
|
353
450
|
//console.log(inlines[n].ep, inlines[n].sp, inlines[n].s)
|
|
354
451
|
|
|
355
452
|
marksPush(marks, nest, inlines[n].ep, inlines[n].ep, 1, inlines[n].len - 1, 'em_open')
|
|
@@ -363,7 +460,6 @@ const setEm = (inlines, marks, n, memo, sNest) => {
|
|
|
363
460
|
marksPush(marks, nest, inlines[i].ep, inlines[i].ep, 1, inlines[i].len - 1, 'em_close')
|
|
364
461
|
inlines[i].sp = inlines[i].ep - 1
|
|
365
462
|
inlines[i].ep -= 1
|
|
366
|
-
|
|
367
463
|
}
|
|
368
464
|
inlines[i].len -= 1
|
|
369
465
|
//console.log(marks)
|
|
@@ -378,9 +474,7 @@ const setEm = (inlines, marks, n, memo, sNest) => {
|
|
|
378
474
|
|
|
379
475
|
const setText = (inlines, marks, n, nest) => {
|
|
380
476
|
//console.log('n: ' + n + ' [text]: inlines[n].len: ' + inlines[n].len)
|
|
381
|
-
//marksPush(marks, -1, inlines[n].sp + 1, inlines[n].ep, inlines[n].len, -1, 'text')
|
|
382
477
|
marksPush(marks, nest, inlines[n].sp, inlines[n].ep, inlines[n].len, -1, 'text')
|
|
383
|
-
//inlines[n].sp += 1
|
|
384
478
|
inlines[n].len = 0
|
|
385
479
|
}
|
|
386
480
|
|
|
@@ -430,7 +524,7 @@ const checkNest = (inlines, marks, n, i) => {
|
|
|
430
524
|
return nest
|
|
431
525
|
}
|
|
432
526
|
|
|
433
|
-
const createMarks = (inlines, start, end, memo) => {
|
|
527
|
+
const createMarks = (state, inlines, start, end, memo, opt) => {
|
|
434
528
|
let marks = []
|
|
435
529
|
let n = start
|
|
436
530
|
while (n < end) {
|
|
@@ -438,11 +532,11 @@ const createMarks = (inlines, start, end, memo) => {
|
|
|
438
532
|
memo.isEm = inlines[n].len === 1 ? true : false
|
|
439
533
|
memo.wrapEm = 0
|
|
440
534
|
let nest = 0
|
|
441
|
-
//console.log('n: ' + n + ' ----- inlines.length: ' + inlines.length + ', memo.isEm: ' + memo.isEm)
|
|
535
|
+
//console.log('n: ' + n + ' ----- inlines:: src: ' + state.src.slice(inlines[n].sp, inlines[n].ep + 1) + ', inlines[n].sp: ' + inlines[n].sp + ', inlines.length: ' + inlines.length + ', memo.isEm: ' + memo.isEm)
|
|
442
536
|
if (!memo.isEm) {
|
|
443
|
-
n, nest, memo = setStrong(inlines, marks, n, memo)
|
|
537
|
+
n, nest, memo = setStrong(state, inlines, marks, n, memo, opt)
|
|
444
538
|
}
|
|
445
|
-
n, nest, memo = setEm(inlines, marks, n, memo)
|
|
539
|
+
n, nest, memo = setEm(state, inlines, marks, n, memo, opt)
|
|
446
540
|
if (inlines[n].len !== 0) setText(inlines, marks, n, nest)
|
|
447
541
|
//console.log(marks)
|
|
448
542
|
n++
|
|
@@ -482,21 +576,30 @@ const strongJa = (state, silent, opt) => {
|
|
|
482
576
|
if (silent) return false
|
|
483
577
|
const start = state.pos
|
|
484
578
|
let max = state.posMax
|
|
485
|
-
const hasCurlyAttributes = state.md.core.ruler.__rules__.filter(rule => {
|
|
486
|
-
rule.name === 'curly_attributes' // markdown-it-attrs
|
|
487
|
-
})
|
|
488
579
|
let attributesSrc
|
|
489
|
-
if (
|
|
490
|
-
|
|
491
|
-
|
|
580
|
+
if (start > max) return false
|
|
581
|
+
if (state.src.charCodeAt(start) !== 0x2A) return false
|
|
582
|
+
if (hasBackslash(state, start)) return false
|
|
583
|
+
|
|
584
|
+
if (opt.mditAttrs) {
|
|
585
|
+
attributesSrc = state.src.match(/((\n)? *){([^{}\n!@#%^&*()]+?)} *$/)
|
|
586
|
+
if (attributesSrc && attributesSrc[3] !== '.') {
|
|
492
587
|
max = state.src.slice(0, attributesSrc.index).length
|
|
588
|
+
if (attributesSrc[2] === '\n') {
|
|
589
|
+
max = state.src.slice(0, attributesSrc.index - 1).length
|
|
590
|
+
}
|
|
591
|
+
if(hasBackslash(state, attributesSrc.index) && attributesSrc[2] === '' && attributesSrc[1].length === 0) {
|
|
592
|
+
max = state.posMax
|
|
593
|
+
}
|
|
594
|
+
} else {
|
|
595
|
+
let endCurlyKet = state.src.match(/(\n *){([^{}\n!@#%^&*()]*?)}.*(} *?)$/)
|
|
596
|
+
if (endCurlyKet) {
|
|
597
|
+
max -= endCurlyKet[3].length
|
|
598
|
+
}
|
|
493
599
|
}
|
|
494
600
|
}
|
|
495
|
-
if (start > max) return false
|
|
496
|
-
if (state.src.charCodeAt(start) !== 0x2A) return false
|
|
497
601
|
|
|
498
|
-
|
|
499
|
-
//console.log('state.src.length: ' + state.src.length + ', start: ' + start + ', state.src: ' + state.src)
|
|
602
|
+
//console.log('state.src.length(max): ' + state.src.length + (state.src.length === max ? '' : '(' + max + ')') + ', start: ' + start + ', state.src: ' + state.src)
|
|
500
603
|
let inlines = createInlines(state, start, max, opt)
|
|
501
604
|
//console.log('inlines: ')
|
|
502
605
|
//console.log(inlines)
|
|
@@ -510,7 +613,8 @@ const strongJa = (state, silent, opt) => {
|
|
|
510
613
|
inlineMarkStart: state.src.charCodeAt(0) === 0x2A ? true : false,
|
|
511
614
|
inlineMarkEnd: state.src.charCodeAt(max - 1) === 0x2A ? true : false,
|
|
512
615
|
}
|
|
513
|
-
|
|
616
|
+
|
|
617
|
+
let marks = createMarks(state, inlines, 0, inlines.length, memo, opt)
|
|
514
618
|
//console.log('marks: ')
|
|
515
619
|
//console.log(marks)
|
|
516
620
|
|
|
@@ -518,10 +622,16 @@ const strongJa = (state, silent, opt) => {
|
|
|
518
622
|
//console.log('fix inlines:')
|
|
519
623
|
//console.log(inlines)
|
|
520
624
|
|
|
521
|
-
setToken(state, inlines)
|
|
625
|
+
setToken(state, inlines, opt)
|
|
626
|
+
|
|
627
|
+
//console.log ('End process:: max:' + max + ', state.posMax: ' + state.posMax + ', opt.mditAttrs: ' + opt.mditAttrs)
|
|
522
628
|
|
|
523
|
-
if (
|
|
524
|
-
|
|
629
|
+
if (opt.mditAttrs && max !== state.posMax) {
|
|
630
|
+
if (!attributesSrc) {
|
|
631
|
+
state.pos = max
|
|
632
|
+
return true
|
|
633
|
+
}
|
|
634
|
+
//console.log('start: ' + start + ', attributesSrc[0]::' + attributesSrc[0] + ', attributesSrc[1].length: ' + attributesSrc[1].length)
|
|
525
635
|
if (attributesSrc[1].length > 1) {
|
|
526
636
|
state.pos = max + attributesSrc[1].length
|
|
527
637
|
} else {
|
|
@@ -530,12 +640,14 @@ const strongJa = (state, silent, opt) => {
|
|
|
530
640
|
} else {
|
|
531
641
|
state.pos = max + 1
|
|
532
642
|
}
|
|
643
|
+
//console.log(state.tokens)
|
|
533
644
|
return true
|
|
534
645
|
}
|
|
535
646
|
|
|
536
647
|
const mditStrongJa = (md, option) => {
|
|
537
648
|
const opt = {
|
|
538
|
-
dollarMath: true,
|
|
649
|
+
dollarMath: true, //inline math $...$
|
|
650
|
+
mditAttrs: true, //markdown-it-attrs
|
|
539
651
|
}
|
|
540
652
|
if (option !== undefined) {
|
|
541
653
|
for (let o in option) {
|
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.6",
|
|
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
|
+
"@sup39/markdown-it-cjk-breaks": "^1.2.0",
|
|
14
15
|
"markdown-it": "^14.1.0",
|
|
15
16
|
"markdown-it-attrs": "^4.2.0"
|
|
16
17
|
}
|
package/test/example-complex.txt
CHANGED
|
@@ -85,11 +85,10 @@ HTMLは**「*HyperText* Markup *Language*」**の略です。
|
|
|
85
85
|
[HTML]
|
|
86
86
|
<p><em>a<strong>b</strong>c<strong>d</strong>e</em></p>
|
|
87
87
|
|
|
88
|
-
|
|
89
88
|
[Markdown]
|
|
90
89
|
*aa**
|
|
91
|
-
[HTML
|
|
92
|
-
<p
|
|
90
|
+
[HTML]
|
|
91
|
+
<p><em>aa</em>*</p>
|
|
93
92
|
|
|
94
93
|
[Markdown]
|
|
95
94
|
a*aa**
|
|
@@ -185,13 +184,12 @@ a**b**c {.style}
|
|
|
185
184
|
[HTML]
|
|
186
185
|
<p class="style">a<strong>b</strong>c</p>
|
|
187
186
|
|
|
188
|
-
|
|
189
187
|
[Markdown]
|
|
190
188
|
z*a<span>b*c</span>d*e
|
|
191
189
|
[HTML]
|
|
192
190
|
<p>z<em>a<span>b</em>c</span>d*e</p>
|
|
193
191
|
[HTML:true]
|
|
194
|
-
<p>z<em>a<span>b
|
|
192
|
+
<p>z<em>a<span>b</em>c</span>d*e</p>
|
|
195
193
|
|
|
196
194
|
[Markdown]
|
|
197
195
|
a<span>b*c</span>d*e*f
|
|
@@ -218,9 +216,8 @@ aa<span>b*ef*</span>cc
|
|
|
218
216
|
a*a<span>b*ef*</span>c*c
|
|
219
217
|
[HTML:false]
|
|
220
218
|
<p>a<em>a<span>b</em>ef<em></span>c</em>c</p>
|
|
221
|
-
[HTML:true
|
|
222
|
-
<p>a<em>a<span>b
|
|
223
|
-
|
|
219
|
+
[HTML:true]
|
|
220
|
+
<p>a<em>a<span>b</em>ef*</span>c*c</p>
|
|
224
221
|
|
|
225
222
|
[Markdown]
|
|
226
223
|
a***a<span>b</span>c***c
|
|
@@ -235,7 +232,7 @@ a***a<span>b***e</span>cc
|
|
|
235
232
|
[HTML:false]
|
|
236
233
|
<p>a<em><strong>a<span>b</strong></em>e</span>cc</p>
|
|
237
234
|
[HTML:true]
|
|
238
|
-
<p>a
|
|
235
|
+
<p>a<em><strong>a<span>b</strong></em>e</span>cc</p>
|
|
239
236
|
|
|
240
237
|
[Markdown]
|
|
241
238
|
aa<span>b*e<s>f</s>*</span>cc
|
|
@@ -336,3 +333,31 @@ b<span>c</span>*
|
|
|
336
333
|
<p>*<em><em><span>a</span></em>b</em></p>
|
|
337
334
|
|
|
338
335
|
|
|
336
|
+
[Markdown]
|
|
337
|
+
*`a`*
|
|
338
|
+
[HTML]
|
|
339
|
+
<p><em><code>a</code></em></p>
|
|
340
|
+
|
|
341
|
+
[Markdown]
|
|
342
|
+
**z`a`b**
|
|
343
|
+
[HTML]
|
|
344
|
+
<p><strong>z<code>a</code>b</strong></p>
|
|
345
|
+
|
|
346
|
+
[Markdown]
|
|
347
|
+
**`a`
|
|
348
|
+
[HTML]
|
|
349
|
+
<p>**<code>a</code></p>
|
|
350
|
+
|
|
351
|
+
[Markdown]
|
|
352
|
+
**<b>a</b>**
|
|
353
|
+
[HTML:false]
|
|
354
|
+
<p><strong><b>a</b></strong></p>
|
|
355
|
+
[HTML:true]
|
|
356
|
+
<p><strong><b>a</b></strong></p>
|
|
357
|
+
|
|
358
|
+
[Markdown]
|
|
359
|
+
**<br>
|
|
360
|
+
[HTML:false]
|
|
361
|
+
<p>**<br></p>
|
|
362
|
+
[HTML:true]
|
|
363
|
+
<p>**<br></p>
|