@peaceroad/markdown-it-strong-ja 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 k_taka
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # p7d-markdown-it-strong-ja
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."
4
+
5
+ ## Use
6
+
7
+ ```js
8
+ import mdit from 'markdown-it'
9
+ import mdStrongJa from '@peaceroad/markdown-it-strong-ja'
10
+ const md = mdit().use(mdStrongJa)
11
+
12
+ md.renderer('HTMLは**「HyperText Markup Language」**の略です。')
13
+ // <p>HTMLは<strong>「HyperText Markup Language」</strong>の略です。</p>
14
+ ```
15
+
16
+ ## Example
17
+
18
+ ~~~
19
+ [Markdown]
20
+ HTMLは「**HyperText Markup Language**」の略です。
21
+ [HTML]
22
+ <p>HTMLは「<strong>HyperText Markup Language</strong>」の略です。</p>
23
+
24
+
25
+ [Markdown]
26
+ HTMLは**「HyperText Markup Language」**の略です。
27
+ [HTML]
28
+ <p>HTMLは<strong>「HyperText Markup Language」</strong>の略です。</p>
29
+
30
+
31
+ [Markdown]
32
+ HTMLは**「HyperText *Markup* Language」**の略です。
33
+ [HTML]
34
+ <p>HTMLは<strong>「HyperText <em>Markup</em> Language」</strong>の略です。</p>
35
+
36
+
37
+ [Markdown]
38
+ HTMLは**「HyperText *Markup* `Language`」**の略です。
39
+ [HTML]
40
+ <p>HTMLは<strong>「HyperText <em>Markup</em> <code>Language</code>」</strong>の略です。</p>
41
+
42
+
43
+ [Markdown]
44
+ HTMLは**「HyperText Mark
45
+
46
+ up Language」**の略です。
47
+ [HTML]
48
+ <p>HTMLは**「HyperText Mark</p>
49
+ <p>up Language」**の略です。</p>
50
+
51
+
52
+ [Markdown]
53
+ HTMLは\**「HyperText Markup Language」**の略です。
54
+ [HTML]
55
+ <p>HTMLは**「HyperText Markup Language」**の略です。</p>
56
+
57
+
58
+ [Markdown]
59
+ HTMLは\\**「HyperText Markup Language」**の略です。
60
+ [HTML]
61
+ <p>HTMLは\<strong>「HyperText Markup Language」</strong>の略です。</p>
62
+
63
+
64
+ [Markdown]
65
+ HTMLは\\\**「HyperText Markup Language」**の略です。
66
+ [HTML]
67
+ <p>HTMLは\**「HyperText Markup Language」**の略です。</p>
68
+
69
+
70
+ [Markdown]
71
+ HTMLは`**`は**「HyperText Markup Language」**の略です。
72
+ [HTML]
73
+ <p>HTMLは<code>**</code>は<strong>「HyperText Markup Language」</strong>の略です。</p>
74
+
75
+ [Markdown]
76
+ HTMLは`**`は**「HyperText** <b>Markup</b> Language」の略です。
77
+ [HTML:false]
78
+ <p>HTMLは<code>**</code>は<strong>「HyperText</strong> &lt;b&gt;Markup&lt;/b&gt; Language」の略です。</p>
79
+ [HTML:true]
80
+ <p>HTMLは<code>**</code>は<strong>「HyperText</strong> <b>Markup</b> Language」の略です。</p>
81
+
82
+
83
+ [Markdown]
84
+ HTMLは`**`は**「HyperText <b>Markup</b> Language」**の略です。
85
+ [HTML:false]
86
+ <p>HTMLは<code>**</code>は<strong>「HyperText &lt;b&gt;Markup&lt;/b&gt; Language」</strong>の略です。</p>
87
+ [HTML:true]
88
+ <p>HTMLは<code>**</code>は<strong>「HyperText <b>Markup</b> Language」</strong>の略です。</p>
89
+
90
+
91
+ [Markdown]
92
+ ```
93
+ HTMLは`**`は**「HyperText Markup Language」**の略です。
94
+ ```
95
+ [HTML:false]
96
+ <pre><code>HTMLは`**`は**「HyperText Markup Language」**の略です。
97
+ </code></pre>
98
+ [HTML:true]
99
+ <pre><code>HTMLは`**`は**「HyperText Markup Language」**の略です。
100
+ </code></pre>
101
+ ~~~
package/index.js ADDED
@@ -0,0 +1,77 @@
1
+ import mdit from 'markdown-it'
2
+
3
+ const strongJa = (state, silent, mdOptions) => {
4
+ let found, content,token
5
+ const max = state.posMax
6
+ const start = state.pos
7
+
8
+ if (state.src.charCodeAt(start) !== 0x2A || state.src.charCodeAt(start + 1) !== 0x2A) return false
9
+ if (silent) return false
10
+ if (start + 3 >= max) return false
11
+ state.pos = start + 2
12
+
13
+ while (state.pos < max) {
14
+ if (state.src.charCodeAt(state.pos) === 0x2A && state.src.charCodeAt(state.pos - 1) === 0x2A) {
15
+ found = true
16
+ break
17
+ }
18
+ state.md.inline.skipToken(state);
19
+ }
20
+ //console.log('found: ' + found)
21
+
22
+ if (!found || start + 2 === state.pos) {
23
+ state.pos = start
24
+ return false
25
+ }
26
+
27
+ content = state.src.slice(start + 2, state.pos - 1)
28
+
29
+ if(!mdOptions.html) {
30
+ content = content.replace(/</g, '&lt;').replace(/>/g, '&gt;')
31
+ }
32
+
33
+
34
+ state.posMax = state.pos
35
+ state.pos = start + 2
36
+ //console.log('state.posMax: ' + state.posMax + ', state.pos: ' + state.pos)
37
+
38
+ token = state.push('strong_open', 'strong', 1)
39
+ token.markup = '**'
40
+
41
+
42
+ const md = new mdit({html: true})
43
+ const childTokens = md.parseInline(content)
44
+
45
+ token = childTokens[0].children.map(t => {
46
+ const aToken = state.push(t.type, t.tag, t.nesting)
47
+ aToken.attrs = t.attrs
48
+ aToken.map = t.map
49
+ aToken.level = t.level
50
+ aToken.children = t.children
51
+ aToken.content = t.content
52
+ aToken.markup = t.markup
53
+ aToken.info = t.info
54
+ aToken.meta = t.meta
55
+ aToken.block = t.block
56
+ aToken.hidden = t.hidden
57
+ return aToken
58
+ })
59
+
60
+ // console.log(token)
61
+
62
+ token = state.push('strong_close', 'strong', -1)
63
+ token.markup = '**'
64
+
65
+ state.pos = state.posMax + 1
66
+ state.posMax = max
67
+
68
+ return
69
+ }
70
+
71
+ const mdStrongJa = (md) => {
72
+ md.inline.ruler.before('emphasis', 'strong_ja', (state, silent) => {
73
+ strongJa(state, silent, md.options)
74
+ })
75
+ }
76
+
77
+ export default mdStrongJa
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
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.0",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "node test/test.js"
9
+ },
10
+ "author": "peaceroad <peaceroad@gmail.com>",
11
+ "license": "MIT",
12
+ "dependencies": {
13
+ "markdown-it": "^14.1.0"
14
+ }
15
+ }
@@ -0,0 +1,83 @@
1
+ [Markdown]
2
+ HTMLは「**HyperText Markup Language**」の略です。
3
+ [HTML]
4
+ <p>HTMLは「<strong>HyperText Markup Language</strong>」の略です。</p>
5
+
6
+
7
+ [Markdown]
8
+ HTMLは**「HyperText Markup Language」**の略です。
9
+ [HTML]
10
+ <p>HTMLは<strong>「HyperText Markup Language」</strong>の略です。</p>
11
+
12
+
13
+ [Markdown]
14
+ HTMLは**「HyperText *Markup* Language」**の略です。
15
+ [HTML]
16
+ <p>HTMLは<strong>「HyperText <em>Markup</em> Language」</strong>の略です。</p>
17
+
18
+
19
+ [Markdown]
20
+ HTMLは**「HyperText *Markup* `Language`」**の略です。
21
+ [HTML]
22
+ <p>HTMLは<strong>「HyperText <em>Markup</em> <code>Language</code>」</strong>の略です。</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は\<strong>「HyperText Markup Language」</strong>の略です。</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>は<strong>「HyperText Markup Language」</strong>の略です。</p>
56
+
57
+ [Markdown]
58
+ HTMLは`**`は**「HyperText** <b>Markup</b> Language」の略です。
59
+ [HTML:false]
60
+ <p>HTMLは<code>**</code>は<strong>「HyperText</strong> &lt;b&gt;Markup&lt;/b&gt; Language」の略です。</p>
61
+ [HTML:true]
62
+ <p>HTMLは<code>**</code>は<strong>「HyperText</strong> <b>Markup</b> Language」の略です。</p>
63
+
64
+
65
+ [Markdown]
66
+ HTMLは`**`は**「HyperText <b>Markup</b> Language」**の略です。
67
+ [HTML:false]
68
+ <p>HTMLは<code>**</code>は<strong>「HyperText &lt;b&gt;Markup&lt;/b&gt; Language」</strong>の略です。</p>
69
+ [HTML:true]
70
+ <p>HTMLは<code>**</code>は<strong>「HyperText <b>Markup</b> Language」</strong>の略です。</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
+
package/test/test.js ADDED
@@ -0,0 +1,70 @@
1
+ import assert from 'assert'
2
+ import fs from 'fs'
3
+ import path from 'path'
4
+ import url from 'url'
5
+
6
+ import mdit from 'markdown-it'
7
+ import mdStrongJa from '../index.js'
8
+
9
+ const __dirname = path.dirname(url.fileURLToPath(import.meta.url)).replace(/\\/g, '/')
10
+
11
+ const check = (ms, example) => {
12
+ const md = mdit().use(mdStrongJa)
13
+ const mdWithHtml = mdit({html: true}).use(mdStrongJa)
14
+ let n = 1
15
+ while (n < ms.length) {
16
+ //if (n !== 7) { n++; continue }
17
+ const m = ms[n].markdown
18
+
19
+ console.log('Test [' + n + ', HTML: false] >>>')
20
+ const h = md.render(m)
21
+ try {
22
+ assert.strictEqual(h, ms[n].html)
23
+ } catch(e) {
24
+ console.log('Input: ' + ms[n].markdown + '\nConvert: ' + h + 'Correct: ' + ms[n].html)
25
+ }
26
+
27
+ if (ms[n].htmlWithHtmlTrue) {
28
+ console.log('Test [' + n + ', HTML: true] >>>')
29
+ const hh = mdWithHtml.render(m)
30
+ try {
31
+ assert.strictEqual(hh, ms[n].htmlWithHtmlTrue)
32
+ } catch(e) {
33
+ console.log('Input: ' + ms[n].markdown + '\nConvert: ' + hh + 'Correct: ' + ms[n].htmlWithHtmlTrue)
34
+ }
35
+ }
36
+
37
+ n++
38
+ }
39
+ }
40
+
41
+ const examples = {
42
+ strong: __dirname + '/example-strong.txt',
43
+ }
44
+
45
+ for (let example in examples) {
46
+ const exampleCont = fs.readFileSync(examples[example], 'utf-8').trim()
47
+ let ms = [];
48
+ let ms0 = exampleCont.split(/\n*\[Markdown\]\n/)
49
+ let n = 1
50
+ while (n < ms0.length) {
51
+ let mhs = ms0[n].split(/\n+\[HTML[^\]]*?\]\n/)
52
+ let i = 1
53
+ while (i < 3) {
54
+ if (mhs[i] === undefined) {
55
+ mhs[i] = ''
56
+ } else {
57
+ mhs[i] = mhs[i].replace(/$/,'\n')
58
+ }
59
+ i++
60
+ }
61
+ ms[n] = {
62
+ markdown: mhs[0],
63
+ html: mhs[1],
64
+ htmlWithHtmlTrue: mhs[2],
65
+ }
66
+ n++
67
+ }
68
+ console.log('Check ' + example + " process. =======================")
69
+ check(ms, example)
70
+ }