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.
Files changed (3) hide show
  1. package/index.cjs +29 -39
  2. package/package.json +1 -1
  3. 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
- * @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}</code>${rowsWrapper}</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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marked-prismjs-linenumber",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
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/) 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.
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
@@ -68,22 +61,7 @@ console.log(greeting);
68
61
  console.log(marked.parse(markdown));
69
62
  ```
70
63
 
71
- ### 2. Output
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