@peaceroad/markdown-it-strong-ja 0.1.4 → 0.2.0

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 CHANGED
@@ -1,6 +1,8 @@
1
1
  # p7d-markdown-it-strong-ja
2
2
 
3
- This is a plugin for markdown-it. It is an alternative to the standard `**` (strong) processing. It also processes strings that cannot be converted by the standard."
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
+
5
+ Since em processing was added from 0.2, the package name is strange.
4
6
 
5
7
  ## Use
6
8
 
@@ -11,10 +13,15 @@ const md = mdit().use(mdStrongJa)
11
13
 
12
14
  md.render('HTMLは**「HyperText Markup Language」**の略です。')
13
15
  // <p>HTMLは<strong>「HyperText Markup Language」</strong>の略です。</p>
16
+
17
+ md.render('HTMLは*「HyperText Markup Language」*の略です。')
18
+ // <p>HTMLは<em>「HyperText Markup Language」</em>の略です。</p>
14
19
  ```
15
20
 
16
21
  ## Example
17
22
 
23
+ The following examples is for strong. The process for em is roughly the same.
24
+
18
25
  ~~~
19
26
  [Markdown]
20
27
  HTMLは「**HyperText Markup Language**」の略です。
@@ -137,10 +144,10 @@ HTMLは**「HyperText Markup Language」**。
137
144
  [Markdown]
138
145
  a****b
139
146
  [HTML]
140
- <p>a<strong></strong>b</p>
147
+ <p>a****b</p>
141
148
 
142
149
  [Markdown]
143
- a****
150
+ a****
144
151
  [HTML]
145
- <p>a<strong></strong></p>
152
+ <p>a****</p>
146
153
  ~~~
package/index.js CHANGED
@@ -1,82 +1,92 @@
1
- const strongJa = (state, silent, md) => {
2
-
3
- let content, token
4
- let found = false
5
- const max = state.posMax
6
- const start = state.pos
7
-
8
- // console.log('input:: state.src.length: ' + state.src.length + ', state.posMax: ' + state.posMax + ', state.pos: ' + state.pos + ', start: ' + start)
9
-
10
- if (silent) return false
11
- if (start + 3 > max) return false
12
- if (state.src.charCodeAt(start) !== 0x2A || state.src.charCodeAt(start + 1) !== 0x2A) return false
13
-
14
-
15
- let end = start + 2
16
- while (end < max) {
17
- if (state.src.charCodeAt(end) === 0x2A && state.src.charCodeAt(end +1) === 0x2A) {
18
- found = true
19
- end++
20
- break
21
- }
22
- //state.md.inline.skipToken(state);
23
- end++
24
- }
25
- //console.log('found: ' + found + ', start: ' + start + ', end: '+ end)
26
-
27
- if (!found) {
28
- state.pos = start
29
- return false
30
- }
31
-
1
+ const processInlineTokens = (state, start, end, type, max) => {
2
+ let token
3
+ const content = type === 'strong' ? state.src.slice(start, end - 1) : state.src.slice(start, end)
32
4
 
33
- content = state.src.slice(start + 2, end - 1)
34
- //console.log('content: ' + content)
5
+ const childTokens = state.md.parseInline(content, state.env)
35
6
 
36
- token = state.push('strong_open', 'strong', 1)
37
- token.markup = '**'
38
-
39
- //**An error will occur if there are [] inside the brackets. I needed an argument for state.env.
40
- const childTokens = md.parseInline(content, state.env)
7
+ token = state.push(`${type}_open`, type, 1)
8
+ token.markup = type === 'strong' ? '**' : '*'
41
9
 
42
10
  if (childTokens[0] && childTokens[0].children) {
43
11
  childTokens[0].children.forEach(t => {
44
- token = state.push(t.type, t.tag, t.nesting)
45
- token.attrs = t.attrs
46
- token.map = t.map
47
- token.level = t.level
48
- token.children = t.children
49
- token.content = t.content
50
- token.markup = t.markup
51
- token.info = t.info
52
- token.meta = t.meta
53
- token.block = t.block
54
- token.hidden = t.hidden
12
+ token = state.push(t.type, t.tag, t.nesting)
13
+ token.attrs = t.attrs
14
+ token.map = t.map
15
+ token.level = t.level
16
+ token.children = t.children
17
+ token.content = t.content
18
+ token.markup = t.markup
19
+ token.info = t.info
20
+ token.meta = t.meta
21
+ token.block = t.block
22
+ token.hidden = t.hidden
55
23
  })
56
24
  }
57
25
 
58
- token = state.push('strong_close', 'strong', -1)
59
- token.markup = '**'
26
+ token = state.push(`${type}_close`, type, -1)
27
+ token.markup = type === 'strong' ? '**' : '*'
60
28
 
61
29
  state.pos = end + 1
62
- // When there is `** at the end of a paragraph(state.pos === max), if state.pos = end + 1 remains, the output is `</strong>undefined</p>`.
63
- //For now, I created md.core.ruler.push('remove_strong_ja_sp_chars') and it worked.
30
+
64
31
  if (state.pos === max) {
65
32
  state.pos = end
66
33
  token = state.push('text', '', 0)
67
34
  token.content = '★mdStrongJa★'
68
- return true
69
35
  }
36
+ }
70
37
 
71
- //console.log('output:: state.src.length: ' + state.src.length + ', state.posMax: ' + state.posMax + ', state.pos: ' + state.pos)
38
+ const strongJa = (state, silent, md) => {
39
+ let end, type
40
+ const max = state.posMax
41
+ const start = state.pos
42
+
43
+ if (silent) return false
44
+ if (start + 1 > max) return false
45
+ if (state.src.charCodeAt(start) !== 0x2A) return false
46
+
47
+ if (state.src.charCodeAt(start + 1) === 0x2A) {
48
+ end = start + 2
49
+ type = 'strong'
50
+ } else {
51
+ end = start + 1
52
+ type = 'em'
53
+ }
54
+
55
+ while (end < max) {
56
+ if (state.src.charCodeAt(end) === 0x2A) {
57
+ if (type === 'strong' && state.src.charCodeAt(end + 1) === 0x2A) {
58
+ end += 1
59
+ break
60
+ } else if (type === 'em' && (state.src.charCodeAt(end - 1) === 0x2A || state.src.charCodeAt(end + 1) === 0x2A)) {
61
+ end++
62
+ continue
63
+ } else if (type === 'em') {
64
+ break
65
+ }
66
+ }
67
+ end++
68
+ }
69
+ const noContent = type === 'strong' ? end === start + 3 :end === start + 1
70
+ if (end >= max || noContent) {
71
+ state.pos = start
72
+ return false
73
+ }
74
+ //console.log('start: ' + start + ', end: '+ end+ ', state.pos: ' + state.pos)
72
75
 
73
- return false
76
+ processInlineTokens(state, start + (type === 'strong' ? 2 : 1), end, type, max)
77
+ //console.log('output:: state.src.length: ' + state.src.length + ', state.posMax: ' + state.posMax + ', state.pos: ' + state.pos)
78
+ return true
74
79
  }
75
80
 
76
81
  const mdStrongJa = (md) => {
77
82
  md.inline.ruler.before('emphasis', 'strong_ja', (state, silent) => {
78
83
  strongJa(state, silent, md)
79
84
  })
85
+ /*
86
+ md.inline.ruler.before('emphasis', 'em_ja', (state, silent) => {
87
+ emJa(state, silent, md)
88
+ })*/
89
+
80
90
 
81
91
  md.core.ruler.push('remove_strong_ja_sp_chars', (state) => {
82
92
  let i = 0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peaceroad/markdown-it-strong-ja",
3
- "description": "This is a plugin for markdown-it. It is an alternative to the standard `**` (strong) processing. It also processes strings that cannot be converted by the standard.",
4
- "version": "0.1.4",
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.2.0",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -0,0 +1,176 @@
1
+ [Markdown]
2
+ HTMLは「*HyperText Markup Language*」の略です。
3
+ [HTML]
4
+ <p>HTMLは「<em>HyperText Markup Language</em>」の略です。</p>
5
+
6
+
7
+ [Markdown]
8
+ HTMLは*「HyperText Markup Language」*の略です。
9
+ [HTML]
10
+ <p>HTMLは<em>「HyperText Markup Language」</em>の略です。</p>
11
+
12
+
13
+ [Markdown]
14
+ HTMLは*「HyperText **Markup** Language」*の略です。
15
+ [HTML]
16
+ <p>HTMLは<em>「HyperText <strong>Markup</strong> Language」</em>の略です。</p>
17
+
18
+
19
+ [Markdown]
20
+ HTMLは*「HyperText **Markup** `Language`」*の略です。
21
+ [HTML]
22
+ <p>HTMLは<em>「HyperText <strong>Markup</strong> <code>Language</code>」</em>の略です。</p>
23
+
24
+
25
+ [Markdown]
26
+ HTMLは*「HyperText Mark
27
+
28
+ up Language」*の略です。
29
+ [HTML]
30
+ <p>HTMLは*「HyperText Mark</p>
31
+ <p>up Language」*の略です。</p>
32
+
33
+
34
+ [Markdown]
35
+ HTMLは\*「HyperText Markup Language」*の略です。
36
+ [HTML]
37
+ <p>HTMLは*「HyperText Markup Language」*の略です。</p>
38
+
39
+
40
+ [Markdown]
41
+ HTMLは\\*「HyperText Markup Language」*の略です。
42
+ [HTML]
43
+ <p>HTMLは\<em>「HyperText Markup Language」</em>の略です。</p>
44
+
45
+
46
+ [Markdown]
47
+ HTMLは\\\*「HyperText Markup Language」*の略です。
48
+ [HTML]
49
+ <p>HTMLは\*「HyperText Markup Language」*の略です。</p>
50
+
51
+
52
+ [Markdown]
53
+ HTMLは`*`は*「HyperText Markup Language」*の略です。
54
+ [HTML]
55
+ <p>HTMLは<code>*</code>は<em>「HyperText Markup Language」</em>の略です。</p>
56
+
57
+ [Markdown]
58
+ HTMLは`*`は*「HyperText* <b>Markup</b> Language」の略です。
59
+ [HTML:false]
60
+ <p>HTMLは<code>*</code>は<em>「HyperText</em> &lt;b&gt;Markup&lt;/b&gt; Language」の略です。</p>
61
+ [HTML:true]
62
+ <p>HTMLは<code>*</code>は<em>「HyperText</em> <b>Markup</b> Language」の略です。</p>
63
+
64
+
65
+ [Markdown]
66
+ HTMLは`*`は*「HyperText <b>Markup</b> Language」*の略です。
67
+ [HTML:false]
68
+ <p>HTMLは<code>*</code>は<em>「HyperText &lt;b&gt;Markup&lt;/b&gt; Language」</em>の略です。</p>
69
+ [HTML:true]
70
+ <p>HTMLは<code>*</code>は<em>「HyperText <b>Markup</b> Language」</em>の略です。</p>
71
+
72
+
73
+ [Markdown]
74
+ ```
75
+ HTMLは`*`は*「HyperText Markup Language」*の略です。
76
+ ```
77
+ [HTML:false]
78
+ <pre><code>HTMLは`*`は*「HyperText Markup Language」*の略です。
79
+ </code></pre>
80
+ [HTML:true]
81
+ <pre><code>HTMLは`*`は*「HyperText Markup Language」*の略です。
82
+ </code></pre>
83
+
84
+
85
+ [Markdown]
86
+ HTMLは*「HyperText <b>Markup</b> Language」*
87
+ [HTML:false]
88
+ <p>HTMLは<em>「HyperText &lt;b&gt;Markup&lt;/b&gt; Language」</em></p>
89
+ [HTML:true]
90
+ <p>HTMLは<em>「HyperText <b>Markup</b> Language」</em></p>
91
+
92
+
93
+ [Markdown]
94
+ HTMLは「*HyperText Markup Language*」
95
+ [HTML]
96
+ <p>HTMLは「<em>HyperText Markup Language</em>」</p>
97
+
98
+ [Markdown]
99
+ HTMLは*「HyperText Markup Language」*。
100
+ [HTML]
101
+ <p>HTMLは<em>「HyperText Markup Language」</em>。</p>
102
+
103
+ [Markdown]
104
+ HTMLは*「HyperText Markup Language」*
105
+ [HTML]
106
+ <p>HTMLは<em>「HyperText Markup Language」</em></p>
107
+
108
+
109
+ [Markdown]
110
+ HTMLは*「HyperText Markup Language」*。
111
+ [HTML]
112
+ <p>HTMLは<em>「HyperText Markup Language」</em>。</p>
113
+
114
+ [Markdown]
115
+ **
116
+ [HTML]
117
+ <p>**</p>
118
+
119
+ [Markdown]
120
+ ***
121
+ [HTML]
122
+ <hr>
123
+
124
+ [Markdown]
125
+ a**b
126
+ [HTML]
127
+ <p>a**b</p>
128
+
129
+ [Markdown]
130
+ a**
131
+ [HTML]
132
+ <p>a**</p>
133
+
134
+ [Markdown]
135
+ HTMLは*「HyperText <b>Markup</b> Language」*
136
+
137
+ HTMLは*「HyperText <b>Markup</b> Language」*
138
+ [HTML:false]
139
+ <p>HTMLは<em>「HyperText &lt;b&gt;Markup&lt;/b&gt; Language」</em></p>
140
+ <p>HTMLは<em>「HyperText &lt;b&gt;Markup&lt;/b&gt; Language」</em></p>
141
+ [HTML:true]
142
+ <p>HTMLは<em>「HyperText <b>Markup</b> Language」</em></p>
143
+ <p>HTMLは<em>「HyperText <b>Markup</b> Language」</em></p>
144
+
145
+ [Markdown]
146
+ HTMLは*「Hyper[Text] Markup Language」*
147
+ [HTML]
148
+ <p>HTMLは<em>「Hyper[Text] Markup Language」</em></p>
149
+
150
+ [Markdown]
151
+ HTMLは*「Hyper[Text] <b>Markup</b> Language」*
152
+ [HTML:false]
153
+ <p>HTMLは<em>「Hyper[Text] &lt;b&gt;Markup&lt;/b&gt; Language」</em></p>
154
+ [HTML:true]
155
+ <p>HTMLは<em>「Hyper[Text] <b>Markup</b> Language」</em></p>
156
+
157
+
158
+ [Markdown]
159
+ HTMLは*「Hyper[Text] Markup Language」*
160
+ [HTML]
161
+ <p>HTMLは<em>「Hyper[Text] Markup Language」</em></p>
162
+
163
+ [Markdown]
164
+ HTMLは*「Hyper[Text] <b>Markup</b> Language」*
165
+ [HTML:false]
166
+ <p>HTMLは<em>「Hyper[Text] &lt;b&gt;Markup&lt;/b&gt; Language」</em></p>
167
+ [HTML:true]
168
+ <p>HTMLは<em>「Hyper[Text] <b>Markup</b> Language」</em></p>
169
+
170
+
171
+ [Markdown]
172
+ - HTMLは*「Hyper[Text] Markup Language」*
173
+ [HTML]
174
+ <ul>
175
+ <li>HTMLは<em>「Hyper[Text] Markup Language」</em></li>
176
+ </ul>
@@ -119,12 +119,12 @@ HTMLは**「HyperText Markup Language」**。
119
119
  [Markdown]
120
120
  a****b
121
121
  [HTML]
122
- <p>a<strong></strong>b</p>
122
+ <p>a****b</p>
123
123
 
124
124
  [Markdown]
125
- a****
125
+ a****
126
126
  [HTML]
127
- <p>a<strong></strong></p>
127
+ <p>a****</p>
128
128
 
129
129
 
130
130
  [Markdown]
package/test/test.js CHANGED
@@ -41,6 +41,7 @@ const check = (ms, example) => {
41
41
 
42
42
  const examples = {
43
43
  strong: __dirname + '/example-strong.txt',
44
+ em: __dirname + '/example-em.txt',
44
45
  }
45
46
 
46
47
  for (let example in examples) {