marked-prismjs-linenumber 0.0.6 → 0.0.8
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 +14 -5
- package/index.d.ts +19 -0
- package/index.mjs +4 -4
- package/package.json +14 -4
- package/readme.md +18 -25
package/index.cjs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
const Prism = require('prismjs');
|
|
2
2
|
const loadLanguages = require('prismjs/components/');
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} code
|
|
6
|
+
* @param {string} [lang]
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
4
9
|
function highlightWithLineNumbers(code, lang = 'plaintext') {
|
|
5
10
|
const grammar = Prism.languages[lang] || Prism.languages.plaintext;
|
|
6
11
|
|
|
@@ -17,23 +22,27 @@ function highlightWithLineNumbers(code, lang = 'plaintext') {
|
|
|
17
22
|
const lineCount = code.split('\n').length;
|
|
18
23
|
const rowsSpans = '<span></span>'.repeat(lineCount);
|
|
19
24
|
const rowsWrapper = `<span aria-hidden="true" class="line-numbers-rows">${rowsSpans}</span>`;
|
|
20
|
-
return `<pre class="line-numbers language-${lang}"><code class="language-${lang}">${highlightedCode}
|
|
25
|
+
return `<pre class="line-numbers language-${lang}"><code class="language-${lang}">${highlightedCode}</code>${rowsWrapper}</pre>`;
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
const renderer = {
|
|
29
|
+
/**
|
|
30
|
+
* @param {{ text: string; lang?: string }} args
|
|
31
|
+
* @returns {string}
|
|
32
|
+
*/
|
|
24
33
|
code({ text, lang }) {
|
|
25
34
|
const trimmedCode = text.trimEnd();
|
|
26
35
|
return highlightWithLineNumbers(trimmedCode, lang || 'plaintext');
|
|
27
36
|
}
|
|
28
37
|
};
|
|
29
38
|
|
|
39
|
+
/**
|
|
40
|
+
* @param {{ languages?: string[] }} [options]
|
|
41
|
+
*/
|
|
30
42
|
function init(options = {}) {
|
|
31
43
|
if (Array.isArray(options.languages)) {
|
|
32
44
|
loadLanguages(options.languages);
|
|
33
45
|
}
|
|
34
46
|
}
|
|
35
47
|
|
|
36
|
-
module.exports = {
|
|
37
|
-
renderer,
|
|
38
|
-
init
|
|
39
|
-
};
|
|
48
|
+
module.exports = { renderer, init };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface PrismJsLineNumberOptions {
|
|
2
|
+
languages?: string[];
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface MarkedCodeArgs {
|
|
6
|
+
text: string;
|
|
7
|
+
lang?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface MarkedExtension {
|
|
11
|
+
renderer: {
|
|
12
|
+
code(args: MarkedCodeArgs): string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function linenumber(options?: PrismJsLineNumberOptions): MarkedExtension;
|
|
17
|
+
|
|
18
|
+
export { linenumber };
|
|
19
|
+
export default linenumber;
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
import linenumber from './index.cjs';
|
|
2
|
+
|
|
3
|
+
export default linenumber;
|
|
4
|
+
export { linenumber };
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "marked-prismjs-linenumber",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Return HTML with prismjs line-number structure",
|
|
5
|
-
"type": "module",
|
|
6
5
|
"main": "./index.cjs",
|
|
7
6
|
"module": "./index.mjs",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
10
11
|
"import": "./index.mjs",
|
|
11
12
|
"require": "./index.cjs",
|
|
12
13
|
"default": "./index.mjs"
|
|
@@ -14,13 +15,22 @@
|
|
|
14
15
|
},
|
|
15
16
|
"files": [
|
|
16
17
|
"index.mjs",
|
|
17
|
-
"index.cjs"
|
|
18
|
+
"index.cjs",
|
|
19
|
+
"index.d.ts"
|
|
18
20
|
],
|
|
19
21
|
"repository": {
|
|
20
22
|
"type": "git",
|
|
21
23
|
"url": "https://github.com/Yz0012/y_z00blog/tree/main/marked-extensions/marked-prismjs-linenumber"
|
|
22
24
|
},
|
|
23
|
-
"
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"marked": ">=4",
|
|
27
|
+
"prismjs": "^1.29.0"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"marked",
|
|
31
|
+
"prismjs",
|
|
32
|
+
"linenumber"
|
|
33
|
+
],
|
|
24
34
|
"author": "Y_z00",
|
|
25
35
|
"license": "MIT"
|
|
26
36
|
}
|
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# marked-prismjs-linenumber
|
|
2
2
|
|
|
3
|
-
> A [marked](https://marked.js.org/)
|
|
3
|
+
> A [marked](https://marked.js.org/) extension that highlights Markdown code blocks with [Prism.js](https://prismjs.com/) and renders line numbers — just like the `line-numbers` plugin, but produced straight from the Markdown parser.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/marked-prismjs-linenumber)
|
|
6
6
|
[](./LICENSE)
|
|
@@ -42,18 +42,19 @@ npm install marked prismjs marked-prismjs-linenumber
|
|
|
42
42
|
|
|
43
43
|
## Usage
|
|
44
44
|
|
|
45
|
-
### 1. Register the
|
|
45
|
+
### 1. Register the extension
|
|
46
46
|
|
|
47
47
|
```js
|
|
48
48
|
import { marked } from 'marked';
|
|
49
|
-
import
|
|
49
|
+
import linenumber from 'marked-prismjs-linenumber';
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
marked.use(
|
|
52
|
+
linenumber({
|
|
53
|
+
// Optional: preload Prism grammars you need.
|
|
54
|
+
// Anything not loaded falls back to escaped plain text.
|
|
55
|
+
languages: ['javascript', 'typescript', 'css', 'bash', 'json'],
|
|
56
|
+
})
|
|
57
|
+
);
|
|
57
58
|
|
|
58
59
|
const markdown = `
|
|
59
60
|
# Example
|
|
@@ -82,38 +83,30 @@ The code block above is rendered as:
|
|
|
82
83
|
</pre>
|
|
83
84
|
```
|
|
84
85
|
|
|
85
|
-
[Effect display](https://yz0012.github.io/y_z00blog/Post/%E6%9D%82%E9%A1%B9/%E8%A1%8C%E5%8F%B7%E7%A4%BA%E4%BE%8B.html)
|
|
86
|
-
|
|
87
86
|
Each `<span></span>` inside `.line-numbers-rows` becomes one line number via the Prism line-numbers CSS.
|
|
88
87
|
|
|
89
88
|
## API
|
|
90
89
|
|
|
91
|
-
### `
|
|
90
|
+
### `linenumber(options?)`
|
|
92
91
|
|
|
93
|
-
|
|
92
|
+
Creates a `marked` extension. Pass it directly to `marked.use(...)`.
|
|
94
93
|
|
|
95
94
|
| Option | Type | Default | Description |
|
|
96
95
|
| --- | --- | --- | --- |
|
|
97
96
|
| `languages` | `string[]` | — | Language names or aliases to load via `prismjs/components/index.js`. |
|
|
98
97
|
|
|
99
98
|
```js
|
|
100
|
-
|
|
99
|
+
import { marked } from 'marked';
|
|
100
|
+
import linenumber from 'marked-prismjs-linenumber';
|
|
101
|
+
|
|
102
|
+
marked.use(linenumber({ languages: ['javascript', 'python', 'go'] }));
|
|
101
103
|
```
|
|
102
104
|
|
|
103
105
|
If a language is not loaded, or an unknown `lang` is used in a fenced code block, the renderer escapes the raw code and outputs it as plain text.
|
|
104
106
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
A `marked` renderer object exposing a single `code({ text, lang })` method. Pass it to `marked.use({ renderer })`.
|
|
108
|
-
|
|
109
|
-
```js
|
|
110
|
-
import { marked } from 'marked';
|
|
111
|
-
import { renderer } from 'marked-prismjs-linenumber';
|
|
112
|
-
|
|
113
|
-
marked.use({ renderer });
|
|
114
|
-
```
|
|
107
|
+
The extension overrides the `code({ text, lang })` renderer:
|
|
115
108
|
|
|
116
109
|
| Parameter | Type | Default | Description |
|
|
117
110
|
| --- | --- | --- | --- |
|
|
118
|
-
| `
|
|
111
|
+
| `text` | `string` | — | The source code to highlight. |
|
|
119
112
|
| `lang` | `string` | `'plaintext'` | Prism language name or alias. |
|