@peaceroad/markdown-it-renderer-fence 0.1.1 → 0.1.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/README.md +30 -4
- package/index.js +53 -16
- package/package.json +4 -4
- package/test/example-lines-emphasis.txt +98 -0
- package/test/examples-highlightjs.txt +10 -0
- package/test/test.js +5 -3
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ const md = mdit({
|
|
|
22
22
|
return highlightjs.highlight(str, { language: lang }).value
|
|
23
23
|
} catch (__) {}
|
|
24
24
|
}
|
|
25
|
-
return
|
|
25
|
+
return str
|
|
26
26
|
}).use(mditAttrs).use(mditRendererFence)
|
|
27
27
|
|
|
28
28
|
|
|
@@ -68,12 +68,12 @@ $ pwd
|
|
|
68
68
|
~~~
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
## Add span elements to display line number
|
|
71
|
+
## Add span elements to display line number.
|
|
72
72
|
|
|
73
73
|
Add `start` or `data-pre-start` attribute by adding attributes used markdown-it-attrs.
|
|
74
74
|
|
|
75
75
|
~~~md
|
|
76
|
-
```js {
|
|
76
|
+
```js {start="1"}
|
|
77
77
|
import mdit from 'markdonw-it'
|
|
78
78
|
const md = mdit()
|
|
79
79
|
md.render('Nyaan')
|
|
@@ -81,10 +81,36 @@ md.render('Nyaan')
|
|
|
81
81
|
~~~
|
|
82
82
|
|
|
83
83
|
~~~html
|
|
84
|
-
<pre><code class="language-js
|
|
84
|
+
<pre><code class="language-js" data-pre-start="1" style="counter-set:pre-line-number 1;"><span class="pre-line"><span class="hljs-keyword">import</span> mdit <span class="hljs-keyword">from</span> <span class="hljs-string">'markdonw-it'</span></span>
|
|
85
85
|
<span class="pre-line"><span class="hljs-keyword">const</span> md = <span class="hljs-title function_">mdit</span>()</span>
|
|
86
86
|
<span class="pre-line">md.<span class="hljs-title function_">render</span>(<span class="hljs-string">'Nyaan'</span>)</span>
|
|
87
87
|
</code></pre>
|
|
88
88
|
~~~
|
|
89
89
|
|
|
90
90
|
CSS example: <https://codepen.io/peaceroad/pen/qBGpYGK>
|
|
91
|
+
|
|
92
|
+
## Add span elements to add background color to the row ranges.
|
|
93
|
+
|
|
94
|
+
Add `em-lines` or `emphasize-lines` attribute by by adding attributes used markdown-it-attrs.
|
|
95
|
+
|
|
96
|
+
~~~md
|
|
97
|
+
``` {em-lines="2,4-5"}
|
|
98
|
+
1
|
|
99
|
+
2
|
|
100
|
+
3
|
|
101
|
+
4
|
|
102
|
+
5
|
|
103
|
+
6
|
|
104
|
+
```
|
|
105
|
+
~~~
|
|
106
|
+
|
|
107
|
+
~~~html
|
|
108
|
+
[HTML]
|
|
109
|
+
<pre><code>1
|
|
110
|
+
<span class="pre-lines-emphasis">2
|
|
111
|
+
</span>3
|
|
112
|
+
<span class="pre-lines-emphasis">4
|
|
113
|
+
5
|
|
114
|
+
</span>6
|
|
115
|
+
</code></pre>
|
|
116
|
+
~~~
|
package/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
//import highlightjs from 'highlight.js'
|
|
2
|
+
|
|
1
3
|
const fenceStartTag = (tagName, sAttr) => {
|
|
2
4
|
let orderedAttrs = [...sAttr.id, ...sAttr.clas, ...sAttr.data, ...sAttr.style, ...sAttr.other]
|
|
3
5
|
//console.log(orderedAttrs)
|
|
@@ -8,20 +10,48 @@ const fenceStartTag = (tagName, sAttr) => {
|
|
|
8
10
|
return tag + '>'
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
const
|
|
13
|
+
const parseEmphasizeLines = (attrValue) => {
|
|
14
|
+
const lines = []
|
|
15
|
+
let s, e
|
|
16
|
+
attrValue.split(',').forEach(range => {
|
|
17
|
+
if (range.includes('-')) {
|
|
18
|
+
[s, e] = range.split('-').map(n => parseInt(n.trim(), 10));
|
|
19
|
+
lines.push([s, e]);
|
|
20
|
+
} else {
|
|
21
|
+
s = parseInt(range.trim(), 10)
|
|
22
|
+
lines.push([s, s])
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return lines;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const splitFenceBlockToLines = (content, emphasizeLines, opt, hasPreLineStart) => {
|
|
12
29
|
const br = content.match(/\r?\n/)
|
|
13
|
-
const lines = content.split(
|
|
30
|
+
const lines = content.split(/\r?\n/)
|
|
14
31
|
lines.map((line, n) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
32
|
+
if (opt.setLineNumber && hasPreLineStart) {
|
|
33
|
+
const lastElementTag = line.match(/<(\w+)( +[^>]*?)>[^>]*?(<\/\1>)?[^>]*?$/)
|
|
34
|
+
if (lastElementTag && !lastElementTag[3]) {
|
|
35
|
+
line += '</span>'
|
|
36
|
+
if (n < lines.length - 2) {
|
|
37
|
+
lines[n + 1] = `<${lastElementTag[1]}${lastElementTag[2]}>` + lines[n + 1]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (n < lines.length - 1) {
|
|
41
|
+
lines[n] = '<span class="pre-line">' + line + '</span>'
|
|
20
42
|
}
|
|
21
43
|
}
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
|
|
45
|
+
if (opt.setEmphasizeLines && emphasizeLines.length > 0) {
|
|
46
|
+
if (emphasizeLines[0][0] === n + 1) {
|
|
47
|
+
lines[n] = `<span class="pre-lines-emphasis">${lines[n]}`
|
|
48
|
+
}
|
|
49
|
+
if (emphasizeLines[0][1] === n) {
|
|
50
|
+
lines[n] = '</span>' + lines[n]
|
|
51
|
+
emphasizeLines.shift()
|
|
52
|
+
}
|
|
24
53
|
}
|
|
54
|
+
|
|
25
55
|
})
|
|
26
56
|
return lines.join(br)
|
|
27
57
|
}
|
|
@@ -32,7 +62,7 @@ const setInfoAttr = (infoAttr) => {
|
|
|
32
62
|
for (let attrSet of attrSets) {
|
|
33
63
|
const str = attrSet.match(/^(?:([.#])(.+)|(.+?)(?:=("')?(.*?)\1?)?)$/)
|
|
34
64
|
if (str) {
|
|
35
|
-
console.log(str)
|
|
65
|
+
//console.log(str)
|
|
36
66
|
if (str[1] === '.') str[1] = 'class'
|
|
37
67
|
if (str[1] === '#') str[1] = 'id'
|
|
38
68
|
if (str[3]) {
|
|
@@ -49,25 +79,25 @@ const getFenceHtml = (tokens, idx, env, slf, md, options) => {
|
|
|
49
79
|
const opt = {
|
|
50
80
|
setHighlight: true,
|
|
51
81
|
setLineNumber: true,
|
|
82
|
+
setEmphasizeLines: true,
|
|
52
83
|
langPrefix: 'language-',
|
|
53
84
|
highlight: null,
|
|
54
85
|
}
|
|
55
86
|
if (options) Object.assign(opt, options)
|
|
56
87
|
|
|
57
88
|
const token = tokens[idx]
|
|
58
|
-
//console.log(token)
|
|
59
89
|
let content = token.content
|
|
60
90
|
const infoAttr = token.info.trim().match(/{(.*)}$/)
|
|
61
91
|
if(infoAttr) {
|
|
62
92
|
token.attrs = token.attrs ? [...token.attrs, ...setInfoAttr(infoAttr[1])] : setInfoAttr(infoAttr[1])
|
|
63
93
|
}
|
|
64
|
-
//console.log(token.attrs)
|
|
65
94
|
|
|
66
95
|
let lang = token.info.trim().replace(/ *({.*)?$/, '')
|
|
67
96
|
const langClass = lang && token.info !== 'samp' ? opt.langPrefix + lang : ''
|
|
68
97
|
let sAttr = {id: [], clas: [], data: [], style: [], other: []}
|
|
69
98
|
let hasPreLineStart = false
|
|
70
99
|
let preLineStart = -1
|
|
100
|
+
let emphasizeLines = []
|
|
71
101
|
if (token.attrs) {
|
|
72
102
|
//console.log('start: ' +token.attrs)
|
|
73
103
|
for (let attr of token.attrs) {
|
|
@@ -85,6 +115,8 @@ const getFenceHtml = (tokens, idx, env, slf, md, options) => {
|
|
|
85
115
|
attr[0] = 'data-pre-start'
|
|
86
116
|
}
|
|
87
117
|
sAttr.data.push(attr)
|
|
118
|
+
} else if (/^em(?:phasize)?-lines$/.test(attr[0])) {
|
|
119
|
+
emphasizeLines = parseEmphasizeLines(attr[1])
|
|
88
120
|
} else if (attr[0] === 'style') {
|
|
89
121
|
sAttr.style.push(attr)
|
|
90
122
|
} else {
|
|
@@ -101,18 +133,22 @@ const getFenceHtml = (tokens, idx, env, slf, md, options) => {
|
|
|
101
133
|
}
|
|
102
134
|
}
|
|
103
135
|
//console.log(JSON.stringify(sAttr))
|
|
104
|
-
|
|
105
136
|
if (opt.setHighlight && md.options.highlight) {
|
|
106
137
|
if (lang && lang !== 'samp' ) {
|
|
107
|
-
content = md.options.highlight(
|
|
138
|
+
content = md.options.highlight(content, lang)
|
|
139
|
+
/*
|
|
140
|
+
try {
|
|
141
|
+
content = highlightjs.highlight(token.content, {language: lang}).value
|
|
142
|
+
} catch (__) {}
|
|
143
|
+
*/
|
|
108
144
|
} else {
|
|
109
145
|
content = md.utils.escapeHtml(token.content)
|
|
110
146
|
}
|
|
111
147
|
} else {
|
|
112
148
|
content = md.utils.escapeHtml(token.content)
|
|
113
149
|
}
|
|
114
|
-
if (opt.setLineNumber
|
|
115
|
-
content = splitFenceBlockToLines(
|
|
150
|
+
if (opt.setLineNumber || opt.setEmphasizeLines) {
|
|
151
|
+
content = splitFenceBlockToLines(content, emphasizeLines, opt, hasPreLineStart)
|
|
116
152
|
}
|
|
117
153
|
|
|
118
154
|
let fenceHtml = '<pre>'
|
|
@@ -122,6 +158,7 @@ const getFenceHtml = (tokens, idx, env, slf, md, options) => {
|
|
|
122
158
|
} else {
|
|
123
159
|
fenceHtml += fenceStartTag('code', sAttr)
|
|
124
160
|
}
|
|
161
|
+
|
|
125
162
|
fenceHtml += content
|
|
126
163
|
if (isSamp) {
|
|
127
164
|
fenceHtml += '</samp>'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peaceroad/markdown-it-renderer-fence",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "In code blocks, the `<samp>` tag is used with the language keywords `samp`, `shell`, and `console`, and provides an option to display line numbers.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"repository": "https://github.com/peaceroad/p7d-markdown-it-renderer-fence",
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@peaceroad/markdown-it-figure-with-p-caption": "^0.
|
|
15
|
-
"highlight.js": "^11.9.0",
|
|
14
|
+
"@peaceroad/markdown-it-figure-with-p-caption": "^0.10.1",
|
|
16
15
|
"markdown-it": "^14.1.0",
|
|
17
|
-
"markdown-it-attrs": "^4.
|
|
16
|
+
"markdown-it-attrs": "^4.2.0",
|
|
17
|
+
"highlight.js": "^11.10.0"
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
[Markdown]
|
|
2
|
+
``` {em-lines="3-5"}
|
|
3
|
+
1
|
|
4
|
+
2
|
|
5
|
+
3
|
|
6
|
+
4
|
|
7
|
+
5
|
|
8
|
+
6
|
|
9
|
+
```
|
|
10
|
+
[HTML]
|
|
11
|
+
<pre><code>1
|
|
12
|
+
2
|
|
13
|
+
<span class="pre-lines-emphasis">3
|
|
14
|
+
4
|
|
15
|
+
5
|
|
16
|
+
</span>6
|
|
17
|
+
</code></pre>
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
[Markdown]
|
|
21
|
+
``` {em-lines="1"}
|
|
22
|
+
1
|
|
23
|
+
2
|
|
24
|
+
3
|
|
25
|
+
4
|
|
26
|
+
5
|
|
27
|
+
6
|
|
28
|
+
```
|
|
29
|
+
[HTML]
|
|
30
|
+
<pre><code><span class="pre-lines-emphasis">1
|
|
31
|
+
</span>2
|
|
32
|
+
3
|
|
33
|
+
4
|
|
34
|
+
5
|
|
35
|
+
6
|
|
36
|
+
</code></pre>
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
[Markdown]
|
|
40
|
+
``` {em-lines="1-3,5-6"}
|
|
41
|
+
1
|
|
42
|
+
2
|
|
43
|
+
3
|
|
44
|
+
4
|
|
45
|
+
5
|
|
46
|
+
6
|
|
47
|
+
```
|
|
48
|
+
[HTML]
|
|
49
|
+
<pre><code><span class="pre-lines-emphasis">1
|
|
50
|
+
2
|
|
51
|
+
3
|
|
52
|
+
</span>4
|
|
53
|
+
<span class="pre-lines-emphasis">5
|
|
54
|
+
6
|
|
55
|
+
</span></code></pre>
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
[Markdown]
|
|
59
|
+
``` {em-lines="3-5" start="1"}
|
|
60
|
+
1
|
|
61
|
+
2
|
|
62
|
+
3
|
|
63
|
+
4
|
|
64
|
+
5
|
|
65
|
+
6
|
|
66
|
+
```
|
|
67
|
+
[HTML]
|
|
68
|
+
<pre><code data-pre-start="1" style="counter-set:pre-line-number 1;"><span class="pre-line">1</span>
|
|
69
|
+
<span class="pre-line">2</span>
|
|
70
|
+
<span class="pre-lines-emphasis"><span class="pre-line">3</span>
|
|
71
|
+
<span class="pre-line">4</span>
|
|
72
|
+
<span class="pre-line">5</span>
|
|
73
|
+
</span><span class="pre-line">6</span>
|
|
74
|
+
</code></pre>
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
[Markdown]
|
|
78
|
+
``` {em-lines="2,4-5"}
|
|
79
|
+
1
|
|
80
|
+
2
|
|
81
|
+
3
|
|
82
|
+
4
|
|
83
|
+
5
|
|
84
|
+
6
|
|
85
|
+
```
|
|
86
|
+
[HTML]
|
|
87
|
+
<pre><code>1
|
|
88
|
+
<span class="pre-lines-emphasis">2
|
|
89
|
+
</span>3
|
|
90
|
+
<span class="pre-lines-emphasis">4
|
|
91
|
+
5
|
|
92
|
+
</span>6
|
|
93
|
+
</code></pre>
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
@@ -163,3 +163,13 @@ console.log('A test.')
|
|
|
163
163
|
[HTML]
|
|
164
164
|
<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A test.'</span>)
|
|
165
165
|
</code></pre>
|
|
166
|
+
|
|
167
|
+
[Markdown]
|
|
168
|
+
```conf
|
|
169
|
+
[Service]
|
|
170
|
+
Environment="OLLAMA_HOST=0.0.0.0"
|
|
171
|
+
```
|
|
172
|
+
[HTML]
|
|
173
|
+
<pre><code class="language-conf">[Service]
|
|
174
|
+
Environment="OLLAMA_HOST=0.0.0.0"
|
|
175
|
+
</code></pre>
|
package/test/test.js
CHANGED
|
@@ -22,10 +22,10 @@ const mdHighlightJs = mdit({
|
|
|
22
22
|
return highlightjs.highlight(str, { language: lang }).value
|
|
23
23
|
} catch (__) {}
|
|
24
24
|
}
|
|
25
|
-
return
|
|
25
|
+
return str
|
|
26
26
|
}
|
|
27
27
|
}).use(mditRendererFence, opt).use(mditAttrs)
|
|
28
|
-
|
|
28
|
+
const mdLinesEmphasis = mdit({ html: true }).use(mditRendererFence).use(mditAttrs)
|
|
29
29
|
|
|
30
30
|
let __dirname = path.dirname(new URL(import.meta.url).pathname)
|
|
31
31
|
const isWindows = (process.platform === 'win32')
|
|
@@ -36,6 +36,7 @@ if (isWindows) {
|
|
|
36
36
|
const testData = {
|
|
37
37
|
noOption: __dirname + path.sep + 'examples.txt',
|
|
38
38
|
highlightjs: __dirname + path.sep + 'examples-highlightjs.txt',
|
|
39
|
+
linesEmphasis: __dirname + path.sep + 'example-lines-emphasis.txt',
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
const getTestData = (pat) => {
|
|
@@ -87,7 +88,7 @@ const runTest = (process, pat, pass, testId) => {
|
|
|
87
88
|
|
|
88
89
|
while(n <= end) {
|
|
89
90
|
if (!ms[n]
|
|
90
|
-
|
|
91
|
+
//|| n != 2
|
|
91
92
|
) {
|
|
92
93
|
n++
|
|
93
94
|
continue
|
|
@@ -114,5 +115,6 @@ const runTest = (process, pat, pass, testId) => {
|
|
|
114
115
|
let pass = true
|
|
115
116
|
pass = runTest(md, testData.noOption, pass)
|
|
116
117
|
pass = runTest(mdHighlightJs, testData.highlightjs, pass)
|
|
118
|
+
pass = runTest(mdLinesEmphasis, testData.linesEmphasis, pass)
|
|
117
119
|
|
|
118
120
|
if (pass) console.log('Passed all test.')
|