marked-prismjs-linenumber 0.0.1

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.
Files changed (4) hide show
  1. package/index.cjs +39 -0
  2. package/index.mjs +4 -0
  3. package/package.json +22 -0
  4. package/readme.md +117 -0
package/index.cjs ADDED
@@ -0,0 +1,39 @@
1
+ const Prism = require('prismjs');
2
+ const loadLanguages = require('prismjs/components/');
3
+
4
+ function highlightWithLineNumbers(code, lang = 'plaintext') {
5
+ const grammar = Prism.languages[lang] || Prism.languages.plaintext;
6
+
7
+ let highlightedCode;
8
+ if (grammar) {
9
+ highlightedCode = Prism.highlight(code, grammar, lang);
10
+ } else {
11
+ highlightedCode = code
12
+ .replace(/&/g, '&')
13
+ .replace(/</g, '&lt;')
14
+ .replace(/>/g, '&gt;');
15
+ }
16
+
17
+ const lineCount = code.split('\n').length;
18
+ const rowsSpans = '<span></span>'.repeat(lineCount);
19
+ 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}${rowsWrapper}</code></pre>`;
21
+ }
22
+
23
+ const renderer = {
24
+ code({ text, lang }) {
25
+ const trimmedCode = text.trimEnd();
26
+ return highlightWithLineNumbers(trimmedCode, lang || 'plaintext');
27
+ }
28
+ };
29
+
30
+ function init(options = {}) {
31
+ if (Array.isArray(options.languages)) {
32
+ loadLanguages(options.languages);
33
+ }
34
+ }
35
+
36
+ module.exports = {
37
+ renderer,
38
+ init
39
+ };
package/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import mod from './index.cjs';
2
+ export const renderer = mod.renderer;
3
+ export const init = mod.init;
4
+ export default mod;
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "marked-prismjs-linenumber",
3
+ "version": "0.0.1",
4
+ "description": "Return HTML with line-number structure",
5
+ "type": "module",
6
+ "main": "./index.cjs",
7
+ "module": "./index.mjs",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./index.mjs",
11
+ "require": "./index.cjs",
12
+ "default": "./index.mjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "index.mjs",
17
+ "index.cjs"
18
+ ],
19
+ "keywords": ["marked", "prismjs", "linenumber"],
20
+ "author": "Y_z00",
21
+ "license": "MIT"
22
+ }
package/readme.md ADDED
@@ -0,0 +1,117 @@
1
+ # marked-prism-linenumber
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.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/marked-prism-linenumber.svg)](https://www.npmjs.com/package/marked-prism-linenumber)
6
+ [![license](https://img.shields.io/npm/l/marked-prism-linenumber.svg)](./LICENSE)
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install marked-prism-linenumber
12
+ ```
13
+
14
+ Or with pnpm / yarn:
15
+
16
+ ```bash
17
+ pnpm add marked-prism-linenumber
18
+ yarn add marked-prism-linenumber
19
+ ```
20
+
21
+ You also need `marked` and `prismjs`:
22
+
23
+ ```bash
24
+ npm install marked prismjs marked-prism-linenumber
25
+ ```
26
+
27
+ ## Required styles
28
+
29
+ ```html
30
+ <link
31
+ rel="stylesheet"
32
+ href="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism-funky.min.css"
33
+ />
34
+ <link
35
+ rel="stylesheet"
36
+ href="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.css"
37
+ />
38
+ ```
39
+
40
+ - `prism-funky.min.css` — the syntax theme. You can replace it with any other [Prism theme](https://prismjs.com/#themes).
41
+ - `line-numbers.min.css` — required for the line-number gutter, `pre.line-numbers` and `.line-numbers-rows`.
42
+
43
+ ## Usage
44
+
45
+ ### 1. Register the renderer
46
+
47
+ ```js
48
+ import { marked } from 'marked';
49
+ import { renderer, init } from 'marked-prism-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'] });
54
+
55
+ // Hook the renderer into marked.
56
+ marked.use({ renderer });
57
+
58
+ const markdown = `
59
+ # Example
60
+
61
+ \`\`\`js
62
+ const greeting = 'hello world';
63
+ console.log(greeting);
64
+ \`\`\`
65
+ `;
66
+
67
+ console.log(marked.parse(markdown));
68
+ ```
69
+
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
+ Each `<span></span>` inside `.line-numbers-rows` becomes one line number via the Prism line-numbers CSS.
86
+
87
+ ## API
88
+
89
+ ### `init(options?)`
90
+
91
+ Loads Prism language grammars before rendering. Call it once at startup.
92
+
93
+ | Option | Type | Default | Description |
94
+ | --- | --- | --- | --- |
95
+ | `languages` | `string[]` | — | Language names or aliases to load via `prismjs/components/index.js`. |
96
+
97
+ ```js
98
+ init({ languages: ['javascript', 'python', 'go'] });
99
+ ```
100
+
101
+ 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.
102
+
103
+ ### `renderer`
104
+
105
+ A `marked` renderer object exposing a single `code({ text, lang })` method. Pass it to `marked.use({ renderer })`.
106
+
107
+ ```js
108
+ import { marked } from 'marked';
109
+ import { renderer } from 'marked-prism-linenumber';
110
+
111
+ marked.use({ renderer });
112
+ ```
113
+
114
+ | Parameter | Type | Default | Description |
115
+ | --- | --- | --- | --- |
116
+ | `code` | `string` | — | The source code to highlight. |
117
+ | `lang` | `string` | `'plaintext'` | Prism language name or alias. |