marked-prismjs-linenumber 0.0.7 → 0.0.9

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 CHANGED
@@ -1,48 +1,38 @@
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
- */
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, '&lt;')
19
- .replace(/>/g, '&gt;');
4
+ function linenumber(options = {}) {
5
+ if (Array.isArray(options.languages)) {
6
+ loadLanguages(options.languages);
20
7
  }
21
8
 
22
- const lineCount = code.split('\n').length;
23
- const rowsSpans = '<span></span>'.repeat(lineCount);
24
- const rowsWrapper = `<span aria-hidden="true" class="line-numbers-rows">${rowsSpans}</span>`;
25
- return `<pre class="line-numbers language-${lang}"><code class="language-${lang}">${highlightedCode}${rowsWrapper}</code></pre>`;
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
- const renderer = {
29
- /**
30
- * @param {{ text: string; lang?: string }} args
31
- * @returns {string}
32
- */
33
- code({ text, lang }) {
34
- const trimmedCode = text.trimEnd();
35
- return highlightWithLineNumbers(trimmedCode, lang || 'plaintext');
36
- }
37
- };
16
+ let highlightedCode;
17
+ if (grammar) {
18
+ highlightedCode = Prism.highlight(code, grammar, language);
19
+ } else {
20
+ highlightedCode = code
21
+ .replace(/&/g, '&amp;')
22
+ .replace(/</g, '&lt;')
23
+ .replace(/>/g, '&gt;');
24
+ }
38
25
 
39
- /**
40
- * @param {{ languages?: string[] }} [options]
41
- */
42
- function init(options = {}) {
43
- if (Array.isArray(options.languages)) {
44
- loadLanguages(options.languages);
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 = { renderer, init };
36
+ module.exports = linenumber;
37
+ module.exports.default = linenumber;
38
+ module.exports.linenumber = linenumber;
package/index.d.ts CHANGED
@@ -7,15 +7,13 @@ export interface MarkedCodeArgs {
7
7
  lang?: string;
8
8
  }
9
9
 
10
- export const renderer: {
11
- code(args: MarkedCodeArgs): string;
12
- };
13
-
14
- export function init(options?: PrismJsLineNumberOptions): void;
10
+ export interface MarkedExtension {
11
+ renderer: {
12
+ code(args: MarkedCodeArgs): string;
13
+ };
14
+ }
15
15
 
16
- declare const _default: {
17
- renderer: typeof renderer;
18
- init: typeof init;
19
- };
16
+ declare function linenumber(options?: PrismJsLineNumberOptions): MarkedExtension;
20
17
 
21
- export default _default;
18
+ export { linenumber };
19
+ export default linenumber;
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import mod from './index.cjs';
2
- export const renderer = mod.renderer;
3
- export const init = mod.init;
4
- export default mod;
1
+ import linenumber from './index.cjs';
2
+
3
+ export default linenumber;
4
+ export { linenumber };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marked-prismjs-linenumber",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Return HTML with prismjs line-number structure",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.mjs",
package/readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # marked-prismjs-linenumber
2
2
 
3
- > A [marked](https://marked.js.org/) renderer 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.
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
  [![npm version](https://img.shields.io/npm/v/marked-prismjs-linenumber.svg)](https://www.npmjs.com/package/marked-prismjs-linenumber)
6
6
  [![license](https://img.shields.io/npm/l/marked-prismjs-linenumber.svg)](./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
@@ -42,18 +35,19 @@ npm install marked prismjs marked-prismjs-linenumber
42
35
 
43
36
  ## Usage
44
37
 
45
- ### 1. Register the renderer
38
+ ### 1. Register the extension
46
39
 
47
40
  ```js
48
41
  import { marked } from 'marked';
49
- import { renderer, init } from 'marked-prismjs-linenumber';
50
-
51
- // Optional: preload Prism grammars you need.
52
- // Anything not loaded falls back to escaped plain text.
53
- init({ languages: ['javascript', 'typescript', 'css', 'bash', 'json'] });
42
+ import linenumber from 'marked-prismjs-linenumber';
54
43
 
55
- // Hook the renderer into marked.
56
- marked.use({ renderer });
44
+ marked.use(
45
+ linenumber({
46
+ // Optional: preload Prism grammars you need.
47
+ // Anything not loaded falls back to escaped plain text.
48
+ languages: ['javascript', 'typescript', 'css', 'bash', 'json'],
49
+ })
50
+ );
57
51
 
58
52
  const markdown = `
59
53
  # Example
@@ -67,53 +61,28 @@ console.log(greeting);
67
61
  console.log(marked.parse(markdown));
68
62
  ```
69
63
 
70
- ### 2. Output
71
-
72
- The code block above is rendered as:
73
-
74
- ```html
75
- <pre class="line-numbers language-js">
76
- <code class="language-js">
77
- <span class="token keyword">const</span> greeting <span class="token operator">=</span> ...
78
- <span aria-hidden="true" class="line-numbers-rows">
79
- <span></span><span></span>
80
- </span>
81
- </code>
82
- </pre>
83
- ```
84
-
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
- Each `<span></span>` inside `.line-numbers-rows` becomes one line number via the Prism line-numbers CSS.
88
-
89
64
  ## API
90
65
 
91
- ### `init(options?)`
66
+ ### `linenumber(options?)`
92
67
 
93
- Loads Prism language grammars before rendering. Call it once at startup.
68
+ Creates a `marked` extension. Pass it directly to `marked.use(...)`.
94
69
 
95
70
  | Option | Type | Default | Description |
96
71
  | --- | --- | --- | --- |
97
72
  | `languages` | `string[]` | — | Language names or aliases to load via `prismjs/components/index.js`. |
98
73
 
99
74
  ```js
100
- init({ languages: ['javascript', 'python', 'go'] });
75
+ import { marked } from 'marked';
76
+ import linenumber from 'marked-prismjs-linenumber';
77
+
78
+ marked.use(linenumber({ languages: ['javascript', 'python', 'go'] }));
101
79
  ```
102
80
 
103
81
  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
82
 
105
- ### `renderer`
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
- ```
83
+ The extension overrides the `code({ text, lang })` renderer:
115
84
 
116
85
  | Parameter | Type | Default | Description |
117
86
  | --- | --- | --- | --- |
118
- | `code` | `string` | — | The source code to highlight. |
87
+ | `text` | `string` | — | The source code to highlight. |
119
88
  | `lang` | `string` | `'plaintext'` | Prism language name or alias. |