marked-prismjs-linenumber 0.0.8 → 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/index.cjs +29 -39
- package/package.json +1 -1
- package/readme.md +2 -24
package/index.cjs
CHANGED
|
@@ -1,48 +1,38 @@
|
|
|
1
1
|
const Prism = require('prismjs');
|
|
2
2
|
const loadLanguages = require('prismjs/components/');
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @returns {string}
|
|
8
|
-
*/
|
|
9
|
-
function highlightWithLineNumbers(code, lang = 'plaintext') {
|
|
10
|
-
const grammar = Prism.languages[lang] || Prism.languages.plaintext;
|
|
11
|
-
|
|
12
|
-
let highlightedCode;
|
|
13
|
-
if (grammar) {
|
|
14
|
-
highlightedCode = Prism.highlight(code, grammar, lang);
|
|
15
|
-
} else {
|
|
16
|
-
highlightedCode = code
|
|
17
|
-
.replace(/&/g, '&')
|
|
18
|
-
.replace(/</g, '<')
|
|
19
|
-
.replace(/>/g, '>');
|
|
4
|
+
function linenumber(options = {}) {
|
|
5
|
+
if (Array.isArray(options.languages)) {
|
|
6
|
+
loadLanguages(options.languages);
|
|
20
7
|
}
|
|
21
8
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
9
|
+
return {
|
|
10
|
+
renderer: {
|
|
11
|
+
code({ text, lang }) {
|
|
12
|
+
const language = lang || 'plaintext';
|
|
13
|
+
const grammar = Prism.languages[language] || Prism.languages.plaintext;
|
|
14
|
+
const code = text.trimEnd();
|
|
27
15
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
};
|
|
16
|
+
let highlightedCode;
|
|
17
|
+
if (grammar) {
|
|
18
|
+
highlightedCode = Prism.highlight(code, grammar, language);
|
|
19
|
+
} else {
|
|
20
|
+
highlightedCode = code
|
|
21
|
+
.replace(/&/g, '&')
|
|
22
|
+
.replace(/</g, '<')
|
|
23
|
+
.replace(/>/g, '>');
|
|
24
|
+
}
|
|
38
25
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
26
|
+
const lineCount = code.split('\n').length;
|
|
27
|
+
const rowsSpans = '<span></span>'.repeat(lineCount);
|
|
28
|
+
const rowsWrapper = `<span aria-hidden="true" class="line-numbers-rows">${rowsSpans}</span>`;
|
|
29
|
+
|
|
30
|
+
return `<pre class="line-numbers language-${language}"><code class="language-${language}">${highlightedCode}</code>${rowsWrapper}</pre>`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
46
34
|
}
|
|
47
35
|
|
|
48
|
-
module.exports =
|
|
36
|
+
module.exports = linenumber;
|
|
37
|
+
module.exports.default = linenumber;
|
|
38
|
+
module.exports.linenumber = linenumber;
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# marked-prismjs-linenumber
|
|
2
2
|
|
|
3
|
-
> A [marked](https://marked.js.org/) extension that highlights Markdown code blocks with [Prism.js](https://prismjs.com/) and renders line numbers
|
|
3
|
+
> A [marked](https://marked.js.org/) extension that highlights Markdown code blocks with [Prism.js](https://prismjs.com/) and renders line numbers
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/marked-prismjs-linenumber)
|
|
6
6
|
[](./LICENSE)
|
|
@@ -11,13 +11,6 @@
|
|
|
11
11
|
npm install marked-prismjs-linenumber
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
Or with pnpm / yarn:
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
pnpm add marked-prismjs-linenumber
|
|
18
|
-
yarn add marked-prismjs-linenumber
|
|
19
|
-
```
|
|
20
|
-
|
|
21
14
|
You also need `marked` and `prismjs`:
|
|
22
15
|
|
|
23
16
|
```bash
|
|
@@ -68,22 +61,7 @@ console.log(greeting);
|
|
|
68
61
|
console.log(marked.parse(markdown));
|
|
69
62
|
```
|
|
70
63
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
The code block above is rendered as:
|
|
74
|
-
|
|
75
|
-
```html
|
|
76
|
-
<pre class="line-numbers language-js">
|
|
77
|
-
<code class="language-js">
|
|
78
|
-
<span class="token keyword">const</span> greeting <span class="token operator">=</span> ...
|
|
79
|
-
<span aria-hidden="true" class="line-numbers-rows">
|
|
80
|
-
<span></span><span></span>
|
|
81
|
-
</span>
|
|
82
|
-
</code>
|
|
83
|
-
</pre>
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
Each `<span></span>` inside `.line-numbers-rows` becomes one line number via the Prism line-numbers CSS.
|
|
64
|
+
[Effect display](https://yz0012.github.io/y_z00blog/Post/%E7%A4%BA%E4%BE%8B/%E8%A1%8C%E5%8F%B7%E7%A4%BA%E4%BE%8B.html)
|
|
87
65
|
|
|
88
66
|
## API
|
|
89
67
|
|