@peaceroad/markdown-it-strong-ja 0.3.0 → 0.3.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/index.js +66 -11
- package/package.json +3 -2
- package/test/example-complex.txt +45 -0
- package/test/test.js +4 -3
- package/.vscode/settings.json +0 -5
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) {
|
|
@@ -31,9 +31,12 @@ const setToken = (state, inlines) => {
|
|
|
31
31
|
continue
|
|
32
32
|
}
|
|
33
33
|
const childTokens = state.md.parseInline(content, state.env)
|
|
34
|
+
|
|
34
35
|
if (childTokens[0] && childTokens[0].children) {
|
|
36
|
+
//console.log(state.tokens[state.tokens.length - 1])
|
|
35
37
|
state.tokens[state.tokens.length - 1].children = childTokens[0].children
|
|
36
38
|
childTokens[0].children.forEach(t => {
|
|
39
|
+
//console.log('t.type: ' + t.type + ', t.tag: ' + t.tag + ', t.nesting: ' + t.nesting)
|
|
37
40
|
const token = state.push(t.type, t.tag, t.nesting)
|
|
38
41
|
token.attrs = t.attrs
|
|
39
42
|
token.map = t.map
|
|
@@ -47,6 +50,25 @@ const setToken = (state, inlines) => {
|
|
|
47
50
|
token.hidden = t.hidden
|
|
48
51
|
})
|
|
49
52
|
}
|
|
53
|
+
//console.log(childTokens[0].children)
|
|
54
|
+
/*
|
|
55
|
+
if (childTokens[0] && childTokens[0].children) {
|
|
56
|
+
state.tokens[state.tokens.length - 1].children = childTokens[0].children
|
|
57
|
+
childTokens[0].children.forEach(t => {
|
|
58
|
+
//console.log('t.type: ' + t.type + ', t.tag: ' + t.tag + ', t.nesting: ' + t.nesting)
|
|
59
|
+
const token = state.push(t.type, t.tag, t.nesting)
|
|
60
|
+
token.attrs = t.attrs
|
|
61
|
+
token.map = t.map
|
|
62
|
+
token.level = t.level
|
|
63
|
+
token.children = t.children
|
|
64
|
+
token.content = t.content
|
|
65
|
+
token.markup = t.markup
|
|
66
|
+
token.info = t.info
|
|
67
|
+
token.meta = t.meta
|
|
68
|
+
token.block = t.block
|
|
69
|
+
token.hidden = t.hidden
|
|
70
|
+
})
|
|
71
|
+
}*/
|
|
50
72
|
}
|
|
51
73
|
|
|
52
74
|
if (/_close$/.test(type)) {
|
|
@@ -69,15 +91,25 @@ const inlinesPush = (inlines, s, e, len, type) => {
|
|
|
69
91
|
})
|
|
70
92
|
}
|
|
71
93
|
|
|
72
|
-
const
|
|
94
|
+
const createInlines = (state, start, max, opt) => {
|
|
73
95
|
let n = start
|
|
74
96
|
let inlines = []
|
|
75
97
|
let noMark = ''
|
|
76
98
|
let mark = ''
|
|
99
|
+
let isInCode = false
|
|
100
|
+
let isInMath = false
|
|
77
101
|
let beforeIsMark = true
|
|
78
102
|
while (n < max) {
|
|
79
|
-
if (state.src.charCodeAt(n) ===
|
|
80
|
-
if (
|
|
103
|
+
if (state.src.charCodeAt(n) === 0x60) {
|
|
104
|
+
if (!hasBackslash(state, n)) isInCode = isInCode ? false : true
|
|
105
|
+
}
|
|
106
|
+
if (opt.dollarMath) {
|
|
107
|
+
if (state.src.charCodeAt(n) === 0x24) {
|
|
108
|
+
if (!hasBackslash(state, n)) isInMath = isInMath ? false : true
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (state.src.charCodeAt(n) === 0x2A && !isInCode && !isInMath) {
|
|
112
|
+
if (hasBackslash(state, n)) {
|
|
81
113
|
beforeIsMark = false
|
|
82
114
|
noMark += state.src[n]
|
|
83
115
|
if (n === max - 1) {
|
|
@@ -323,15 +355,25 @@ const createMarks = (inlines, inlinesStart, inlinesEnd, memo) => {
|
|
|
323
355
|
return marks
|
|
324
356
|
}
|
|
325
357
|
|
|
326
|
-
const strongJa = (state, silent) => {
|
|
327
|
-
const max = state.posMax
|
|
328
|
-
const start = state.pos
|
|
358
|
+
const strongJa = (state, silent, opt) => {
|
|
329
359
|
if (silent) return false
|
|
360
|
+
const start = state.pos
|
|
361
|
+
let max = state.posMax
|
|
362
|
+
const hasCurlyAttributes = state.md.core.ruler.__rules__.filter(rule => {
|
|
363
|
+
rule.name === 'curly_attributes' // markdown-it-attrs
|
|
364
|
+
})
|
|
365
|
+
let attributesSrc
|
|
366
|
+
if (hasCurlyAttributes) {
|
|
367
|
+
attributesSrc = state.src.match(/( *){.*?}$/)
|
|
368
|
+
if (attributesSrc) {
|
|
369
|
+
max = state.src.slice(0, attributesSrc.index).length
|
|
370
|
+
}
|
|
371
|
+
}
|
|
330
372
|
if (start > max) return false
|
|
331
373
|
if (state.src.charCodeAt(start) !== 0x2A) return false
|
|
332
|
-
if (
|
|
374
|
+
if (hasBackslash(state, start)) return false
|
|
333
375
|
//console.log('state.src.length: ' + state.src.length + ', start: ' + start + ', state.src: ' + state.src)
|
|
334
|
-
let inlines =
|
|
376
|
+
let inlines = createInlines(state, start, max, opt)
|
|
335
377
|
//console.log('inlines: ')
|
|
336
378
|
//console.log(inlines)
|
|
337
379
|
|
|
@@ -380,12 +422,25 @@ const strongJa = (state, silent) => {
|
|
|
380
422
|
|
|
381
423
|
//console.log('state.pos: ' + state.pos + ', inlines[inlines.length - 1].e + 1: ' + (inlines[inlines.length - 1].e + 1) + ', max: ' + max)
|
|
382
424
|
state.pos = inlines[inlines.length - 1].e + 1
|
|
425
|
+
if (attributesSrc) {
|
|
426
|
+
if (attributesSrc[1].length > 1) {
|
|
427
|
+
state.pos += attributesSrc[1].length
|
|
428
|
+
}
|
|
429
|
+
}
|
|
383
430
|
return true
|
|
384
431
|
}
|
|
385
432
|
|
|
386
|
-
const mditStrongJa = (md) => {
|
|
433
|
+
const mditStrongJa = (md, option) => {
|
|
434
|
+
const opt = {
|
|
435
|
+
dollarMath: true
|
|
436
|
+
}
|
|
437
|
+
if (option !== undefined) {
|
|
438
|
+
for (let o in option) {
|
|
439
|
+
opt[o] = option[o]
|
|
440
|
+
}
|
|
441
|
+
}
|
|
387
442
|
md.inline.ruler.before('emphasis', 'strong_ja', (state, silent) => {
|
|
388
|
-
return strongJa(state, silent)
|
|
443
|
+
return strongJa(state, silent, opt)
|
|
389
444
|
})
|
|
390
445
|
}
|
|
391
446
|
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.2",
|
|
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,48 @@ 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
|
+
インライン数式は$x * y = z$のように*書きます*。
|
|
150
|
+
[HTML]
|
|
151
|
+
<p>インライン数式は$x * y = z$のように<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>
|
package/test/test.js
CHANGED
|
@@ -4,16 +4,17 @@ 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 != 29 ) { n++; continue }
|
|
17
18
|
const m = ms[n].markdown
|
|
18
19
|
console.log('Test [' + n + ', HTML: false] >>>')
|
|
19
20
|
const h = md.render(m)
|