@peaceroad/markdown-it-strong-ja 0.4.0 → 0.4.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 CHANGED
@@ -1,6 +1,7 @@
1
1
  const REG_ASTERISKS = /^\*+$/
2
2
  const REG_ATTRS = /{[^{}\n!@#%^&*()]+?}$/
3
3
  const REG_PUNCTUATION = /[!-/:-@[-`{-~ ]/
4
+ const REG_JAPANESE = /[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\uFF01-\uFF60\uFFE0-\uFFE6]/ //漢字、ひらがな、カタカナ(半角含む)、全角英数字、絵文字、全角記号:/(?:[\p{Hiragana}\p{Katakana}\p{Han}]|[\uFF66-\uFF9F]|[A-Za-z0-9]|[\p{Emoji}]|[\uFF01-\uFF60\uFFE0-\uFFE6])/u;/(?:[\p{Hiragana}\p{Katakana}\p{Han}]|[\uFF66-\uFF9F]|[A-Za-z0-9]|[\p{Emoji}])/u
4
5
 
5
6
  const hasBackslash = (state, start) => {
6
7
  let slashNum = 0
@@ -84,7 +85,7 @@ const setToken = (state, inlines, opt) => {
84
85
  let j = 0
85
86
  while (j < childTokens[0].children.length) {
86
87
  const t = childTokens[0].children[j]
87
- if (t.type === 'softbreak') {
88
+ if (t.type === 'softbreak' && !opt.mdBreaks) {
88
89
  t.type = 'text'
89
90
  t.tag = ''
90
91
  t.content = '\n'
@@ -314,19 +315,20 @@ const setStrong = (state, inlines, marks, n, memo, opt) => {
314
315
  let strongNum = Math.trunc(Math.min(inlines[n].len, inlines[i].len) / 2)
315
316
 
316
317
  if (inlines[i].len > 1) {
317
- //console.log(' hasPunctuation: ' + hasPunctuation(state, inlines, n, i) + ', memo.inlineMarkEnd: ' + memo.inlineMarkEnd)
318
- if (hasPunctuation(state, inlines, n, i)) {
318
+ //console.log(' hasPunctuationOrNonJapanese: ' + hasPunctuationOrNonJapanese(state, inlines, n, i) + ', memo.inlineMarkEnd: ' + memo.inlineMarkEnd)
319
+
320
+ if (hasPunctuationOrNonJapanese(state, inlines, n, i)) {
319
321
  if (memo.inlineMarkEnd) {
320
322
  //console.log('check nest em.')
321
323
  //console.log('~~~~~~~~~~~~~~~~~')
322
- marks.push(...createMarks(state, inlines, i, inlines.length - 1, memo, opt))
324
+ marks.push(...createMarks(state, inlines, i, inlines.length - 1, memo, opt))
323
325
  //console.log('~~~~~~~~~~~~~~~~~')
324
326
  if (inlines[i].len === 0) { i++; continue }
325
327
  } else {
326
328
  return n, nest
327
329
  }
328
330
  }
329
- //console.log(' ===> strong normal push. n: ' + n + ', i: ' + i + ' , nest: ' + nest + ',strongNum: ' + strongNum)
331
+ //console.log(' ===> strong normal push. n: ' + n + ', i: ' + i + ' , nest: ' + nest + ',strongNum: ' + strongNum)
330
332
 
331
333
  j = 0
332
334
  while (j < strongNum) {
@@ -357,7 +359,7 @@ const setStrong = (state, inlines, marks, n, memo, opt) => {
357
359
  }
358
360
 
359
361
  if (inlines[n].len === 1 && inlines[i].len > 0) {
360
- //console.log(' check em that warp strong.')
362
+ //console.log(' check em that warp strong.')
361
363
  nest++
362
364
  n, nest = setEm(state, inlines, marks, n, memo, opt, nest)
363
365
  }
@@ -402,26 +404,22 @@ const checkInsideTags = (inlines, i, memo) => {
402
404
  const isPunctuation = (ch) => {
403
405
  return REG_PUNCTUATION.test(ch)
404
406
  }
407
+ const isJapanese = (ch) => {
408
+ return REG_JAPANESE.test(ch)
409
+ }
405
410
 
406
- const hasPunctuation = (state, inlines, n, i) => {
411
+ const hasPunctuationOrNonJapanese = (state, inlines, n, i) => {
407
412
  const src = state.src
408
- const openNextChar = isPunctuation(src[inlines[n].e + 1] || '')
409
- //const openPrevChar = isPunctuation(src[inlines[n].s - 1] || '') || n === 0
410
- let closePrevChar = isPunctuation(src[inlines[i].s - 1] || '')
411
- if (i + 1 < inlines.length) {
412
- //closePrevChar = closePrevChar && inlines[i+1] !== 'html_inline'
413
- }
414
- let closeNextChar = isPunctuation(src[inlines[i].e + 1] || '') || i === inlines.length - 1
415
- //const lastCharIsAsterisk = memo.inlineMarkEnd
416
- //const firstCharIsAsterisk = memo.inlineMarkStart
417
-
418
- //console.log('openPrevChar: ' + openPrevChar + ', openNextChar: ' + openNextChar + ', closePrevChar: ' + closePrevChar + ', closeNextChar: ' + closeNextChar + ', lastCharIsAsterisk: ' + lastCharIsAsterisk + ', firstCharIsAsterisk: ' + firstCharIsAsterisk + ', next condition: ' + ((openNextChar || closePrevChar) && !closeNextChar))
419
- //if ((openNextChar || closePrevChar) && !closeNextChar) {
420
- if ((openNextChar || closePrevChar) && !closeNextChar) {
421
- return true
422
- } else {
423
- return false
424
- }
413
+ const openPrevChar = src[inlines[n].s - 1] || ''
414
+ //const checkOpenPrevChar =
415
+ const openNextChar = src[inlines[n].e + 1] || ''
416
+ const checkOpenNextChar = isPunctuation(openNextChar)
417
+ const closePrevChar = src[inlines[i].s - 1] || ''
418
+ const checkClosePrevChar = isPunctuation(closePrevChar)
419
+ const closeNextChar = src[inlines[i].e + 1] || ''
420
+ const checkCloseNextChar = (isPunctuation(closeNextChar) || i === inlines.length - 1)
421
+ if ((checkOpenNextChar || checkClosePrevChar) && !checkCloseNextChar && !(isJapanese(openPrevChar) || isJapanese(closeNextChar))) return true
422
+ return false
425
423
  }
426
424
 
427
425
  const setEm = (state, inlines, marks, n, memo, opt, sNest) => {
@@ -470,8 +468,8 @@ const setEm = (state, inlines, marks, n, memo, opt, sNest) => {
470
468
  if (nest === -1) return n, nest
471
469
 
472
470
  if (emNum === 1) {
473
- //console.log(' hasPunctuation: ' + hasPunctuation(state, inlines, n, i) + ', memo.inlineMarkEnd: ' + memo.inlineMarkEnd)
474
- if (hasPunctuation(state, inlines, n, i)) {
471
+ //console.log(' hasPunctuationOrNonJapanese: ' + hasPunctuationOrNonJapanese(state, inlines, n, i) + ', memo.inlineMarkEnd: ' + memo.inlineMarkEnd)
472
+ if (hasPunctuationOrNonJapanese(state, inlines, n, i)) {
475
473
  if (memo.inlineMarkEnd) {
476
474
  //console.log('check nest em.')
477
475
  //console.log('~~~~~~~~~~~~~~~~~')
@@ -707,6 +705,7 @@ const mditStrongJa = (md, option) => {
707
705
  const opt = {
708
706
  dollarMath: true, //inline math $...$
709
707
  mditAttrs: true, //markdown-it-attrs
708
+ mdBreaks: md.options.breaks,
710
709
  }
711
710
  if (option) Object.assign(opt, option)
712
711
 
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.4.0",
4
+ "version": "0.4.2",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -478,4 +478,29 @@ e*z<span> *a*b*
478
478
  [HTML:false]
479
479
  <p>*<strong>&lt;sPan&gt;a&lt;/spaN&gt;b</strong></p>
480
480
  [HTML:true]
481
- <p>*<strong><sPan>a</spaN>b</strong></p>
481
+ <p>*<strong><sPan>a</spaN>b</strong></p>
482
+
483
+ [Markdown]
484
+ a**`b`**a
485
+ [HTML:false]
486
+ <p>a**<code>b</code>**a</p>
487
+
488
+ [Markdown]
489
+ a(**`b`**)a
490
+ [HTML:false]
491
+ <p>a(<strong><code>b</code></strong>)a</p>
492
+
493
+ [Markdown]
494
+ あ**`b`**あ
495
+ [HTML:false :: commommark: <p>あ**<code>b</code>**あ</p>]
496
+ <p>あ<strong><code>b</code></strong>あ</p>
497
+
498
+ [Markdown]
499
+ あ(**`b`**)あ
500
+ [HTML:false]
501
+ <p>あ(<strong><code>b</code></strong>)あ</p>
502
+
503
+ [Markdown]
504
+ (**`b`**)あ
505
+ [HTML:false]
506
+ <p>(<strong><code>b</code></strong>)あ</p>
@@ -477,4 +477,36 @@ e*z<span> *a*b*
477
477
  [HTML:false]
478
478
  <p>*<strong>&lt;sPan&gt;a&lt;/spaN&gt;b</strong></p>
479
479
  [HTML:true]
480
- <p>*<strong><sPan>a</spaN>b</strong></p>
480
+ <p>*<strong><sPan>a</spaN>b</strong></p>
481
+
482
+
483
+ [Markdown]
484
+ a**`b`**a
485
+ [HTML:false]
486
+ <p>a**<code>b</code>**a</p>
487
+
488
+ [Markdown]
489
+ a(**`b`**)a
490
+ [HTML:false]
491
+ <p>a(<strong><code>b</code></strong>)a</p>
492
+
493
+ [Markdown]
494
+ あ**`b`**あ
495
+ [HTML:false :: commommark: <p>あ**<code>b</code>**あ</p>]
496
+ <p>あ<strong><code>b</code></strong>あ</p>
497
+
498
+ [Markdown]
499
+ あ(**`b`**)あ
500
+ [HTML:false]
501
+ <p>あ(<strong><code>b</code></strong>)あ</p>
502
+
503
+ [Markdown]
504
+ (**`b`**)あ
505
+ [HTML:false]
506
+ <p>(<strong><code>b</code></strong>)あ</p>
507
+
508
+
509
+
510
+
511
+
512
+
@@ -0,0 +1,46 @@
1
+ [Markdown]
2
+ 吾輩は猫である。名前は<span>まだ</span>無い。
3
+ どこで生れたか*とんと見当*が<span>つかぬ</span>。
4
+ 何でも薄暗い*じめじめした所*でニャーニャー泣いていた事だけは記憶している。
5
+ [HTML:false]
6
+ <p>吾輩は猫である。名前は&lt;span&gt;まだ&lt;/span&gt;無い。<br>
7
+ どこで生れたか<em>とんと見当</em>が&lt;span&gt;つかぬ&lt;/span&gt;。<br>
8
+ 何でも薄暗い<em>じめじめした所</em>でニャーニャー泣いていた事だけは記憶している。</p>
9
+ [HTML:true]
10
+ <p>吾輩は猫である。名前は<span>まだ</span>無い。<br>
11
+ どこで生れたか<em>とんと見当</em>が<span>つかぬ</span>。<br>
12
+ 何でも薄暗い<em>じめじめした所</em>でニャーニャー泣いていた事だけは記憶している。</p>
13
+
14
+ [Markdown]
15
+ a*b
16
+ c*d
17
+ [HTML:false]
18
+ <p>a<em>b<br>
19
+ c</em>d</p>
20
+ [HTML:true]
21
+ <p>a<em>b<br>
22
+ c</em>d</p>
23
+
24
+ [Markdown]
25
+ a**b
26
+ c*d*e**f
27
+ [HTML:false]
28
+ <p>a<strong>b<br>
29
+ c<em>d</em>e</strong>f</p>
30
+ [HTML:true]
31
+ <p>a<strong>b<br>
32
+ c<em>d</em>e</strong>f</p>
33
+
34
+ [Markdown]
35
+ - *aあ {.li-style}
36
+ \{.ul-style} aa {.next-li-style}
37
+ [HTML:false]
38
+ <ul>
39
+ <li>*aあ {.li-style}<br>
40
+ {.ul-style} aa {.next-li-style}</li>
41
+ </ul>
42
+ [HTML:true]
43
+ <ul>
44
+ <li>*aあ {.li-style}<br>
45
+ {.ul-style} aa {.next-li-style}</li>
46
+ </ul>
package/test/test.js CHANGED
@@ -20,10 +20,14 @@ const mditNoAttrsWithHtml = mdit({html: true}).use(mditStrongJa, {mditAttrs: fal
20
20
  const mditNoAttrsCJKBreaks = mdit().use(mditStrongJa, {mditAttrs: false}).use(mditCJKBreaks, {either: true})
21
21
  const mditNoAttrsCJKBreaksWithHtml = mdit({html: true}).use(mditStrongJa, {mditAttrs: false}).use(mditCJKBreaks, {either: true})
22
22
 
23
+ const mditNoAttrsLinebreak = mdit({breaks: true}).use(mditStrongJa, {mditAttrs: false})
24
+ const mditNoAttrsLinebreakWithHtml = mdit({html: true, breaks: true}).use(mditStrongJa, {mditAttrs: false})
25
+
26
+
23
27
  const check = (ms, example, allPass, useAttrs) => {
24
28
  let n = 1
25
29
  while (n < ms.length) {
26
- //if (n !== 72) { n++; continue }
30
+ //if (n !== 81) { n++; continue }
27
31
  const m = ms[n].markdown
28
32
  let h = ''
29
33
  if (example === 'withLineBreak') {
@@ -58,6 +62,34 @@ const check = (ms, example, allPass, useAttrs) => {
58
62
  return allPass
59
63
  }
60
64
 
65
+ const checkBreaks = (ms, example, allPass, useAttrs) => {
66
+ let n = 1
67
+ while (n < ms.length) {
68
+ //if (n !== 81) { n++; continue }
69
+ const m = ms[n].markdown
70
+ const h = mditNoAttrsLinebreak.render(ms[n].markdown);
71
+ try {
72
+ assert.strictEqual(h, ms[n].html);
73
+ } catch(e) {
74
+ console.log('Test [linebreak, HTML: false, useAttrs: ' + useAttrs + '] >>>');
75
+ console.log('Input: ' + ms[n].markdown + '\nOutput: ' + h + ' Correct: ' + ms[n].html)
76
+ allPass = false
77
+ }
78
+ if (ms[n].htmlWithHtmlTrue) {
79
+ let hh = mditNoAttrsLinebreakWithHtml.render(ms[n].markdown)
80
+ try {
81
+ assert.strictEqual(hh, ms[n].htmlWithHtmlTrue)
82
+ } catch(e) {
83
+ console.log('Test [' + n + ', HTML: true, useAttrs: ' + useAttrs + '] >>>')
84
+ console.log('Input: ' + ms[n].markdown + '\nOutput: ' + hh + 'Correct: ' + ms[n].htmlWithHtmlTrue)
85
+ allPass = false
86
+ }
87
+ }
88
+ n++
89
+ }
90
+ return allPass
91
+ }
92
+
61
93
  const examples = {
62
94
  strong: __dirname + '/example-strong.txt',
63
95
  em: __dirname + '/example-em.txt',
@@ -72,7 +104,11 @@ const examplesMditNoAttrs = {
72
104
  withLineBreak: __dirname + '/mditNoAttrs/example-with-linebreak.txt',
73
105
  }
74
106
 
75
- const runTests = (examples, useAttrs) => {
107
+ const examplesMditBreaks = {
108
+ linebreak: __dirname + '/mditNoAttrs/example-mdit-linebrek.txt',
109
+ }
110
+
111
+ const runTests = (examples, checkFunction, useAttrs) => {
76
112
  let allPass = true
77
113
  for (let example in examples) {
78
114
  const exampleCont = fs.readFileSync(examples[example], 'utf-8').trim()
@@ -98,12 +134,13 @@ const runTests = (examples, useAttrs) => {
98
134
  n++
99
135
  }
100
136
  console.log('Check ' + example + ' process [mditAttrs: ' + useAttrs + '] =======================')
101
- allPass = check(ms, example, allPass, useAttrs)
137
+ allPass = checkFunction(ms, example, allPass, useAttrs)
102
138
  }
103
139
  return allPass
104
140
  }
105
141
 
106
- let allPass = runTests(examples, true)
107
- allPass = runTests(examplesMditNoAttrs, false) && allPass
142
+ let allPass = runTests(examples, check, true)
143
+ allPass = runTests(examplesMditNoAttrs, check, false) && allPass
144
+ allPass = runTests(examplesMditBreaks, checkBreaks, false) && allPass
108
145
 
109
146
  if (allPass) console.log('Passed all tests.')