Skip to content

Commit d6506e2

Browse files
authored
Change dev hljs version (#99)
1 parent 929e2c3 commit d6506e2

14 files changed

+453
-16
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
node-version: ${{ matrix.node-version }}
2323
- run: npm ci
2424
- run: npm run build --if-present
25-
- run: npm test
25+
- run: npm run test:all
2626
- name: Upload coverage reports to Codecov
2727
if: matrix.node-version == '20.x' && github.repository == 'tsutsu3/markdown-it-named-code-blocks'
2828
uses: codecov/codecov-action@v4

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,38 @@ npm install markdown-it-named-code-blocks
4646
Use this same as a normal markdown-it plugin:
4747

4848
```js
49-
const md = require('markdown-it');
50-
const namedCodeBlocks = require('markdown-it-named-code-blocks');
49+
const md = require("markdown-it");
50+
const hljs = require("highlight.js");
51+
const namedCodeBlocks = require(".");
52+
53+
const parser = md({
54+
highlight: function (str, lang) {
55+
if (lang && hljs.getLanguage(lang)) {
56+
return (
57+
'<pre class="hljs"><code>' +
58+
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
59+
"</code></pre>"
60+
);
61+
}
62+
63+
return (
64+
'<pre class="hljs"><code>' + md.utils.escapeHtml(str) + "</code></pre>"
65+
);
66+
}
67+
}).use(namedCodeBlocks);
68+
69+
const str = '```js:hello.js\nconsole.log("Hello World!");\n```';
5170

52-
const parser = md().use(namedCodeBlocks);
71+
const result = parser.render(str);
72+
73+
console.log(result);
74+
```
5375

54-
const str = '```js:hello.js\nconsole.log("Hello World!);```'
76+
Output:
5577

56-
const result = parser.render(str);
78+
```html
79+
<pre class="hljs named-fence-block"><code><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;Hello World!&quot;</span>);
80+
</code><div class="named-fence-filename">hello.js</div></pre>
5781
```
5882

5983
Apply CSS like this:
@@ -86,9 +110,11 @@ If you want to enable inline CSS:
86110
const parser = md().use(namedCodeBlocks, {isEnableInlineCss: true});
87111
```
88112

113+
Output:
114+
89115
```html
90-
<pre class="hljs named-fence-block" style="position: relative; padding-top: 2em"><code>console.log(&quot;Hello World!&quot;)
91-
</code><div class="mincb-name" style="position: absolute; top: 0; left: 0; padding: 0 4px; font-weight: bold; color: #000000; background: #c0c0c0; opacity: .6;">hello.js</div></pre>
116+
<pre class="hljs named-fence-block" style="position: relative; padding-top: 2em;"><code><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;Hello World!&quot;</span>);
117+
</code><div class="named-fence-filename" style="position: absolute; top: 0; left: 0; padding: 0 4px; font-weight: bold; color: #000000; background: #c0c0c0; opacity: .6;">hello.js</div></pre>
92118
```
93119

94120
## 🎉 License

image/named_code_blocks.png

-13.7 KB
Loading

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"scripts": {
88
"lint": "eslint . --ext .ts",
99
"format": "prettier --write \"**/*.ts\"",
10+
"install-hljs10": "npm install highlight.js@^10.7.3 --no-save",
11+
"install-hljs11": "npm install highlight.js@^11.0.0 --no-save",
12+
"test-hljs10": "npm run install-hljs10 && jest --coverage",
13+
"test-hljs11": "npm run install-hljs11 && jest --coverage",
14+
"test:all": "npm run test-hljs10 && npm run test-hljs11",
1015
"test": "jest --coverage",
1116
"build": "tsc && npm run minify",
1217
"minify": "terser dist/index.js -o dist/index.min.js --compress --mangle",
@@ -39,7 +44,7 @@
3944
"@typescript-eslint/parser": "^8.13.0",
4045
"eslint": "^9.14.0",
4146
"eslint-config-prettier": "^9.1.0",
42-
"highlight.js": "^10.7.3",
47+
"highlight.js": "^11.10.0",
4348
"jest": "^29.7.0",
4449
"markdown-it": "^14.0.0",
4550
"markdown-it-testgen": "^0.1.6",
File renamed without changes.

test/fixture/codeblock_highlightjs_full_override.txt renamed to test/fixture/hljs10/codeblock_highlightjs_full_override.txt

File renamed without changes.
File renamed without changes.

test/fixture/hljs11/codeblock.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
none
2+
.
3+
```
4+
console.log("Hello World!");
5+
```
6+
.
7+
<pre><code>console.log(&quot;Hello World!&quot;);
8+
</code></pre>
9+
.
10+
11+
js
12+
.
13+
```js
14+
console.log("Hello World!");
15+
```
16+
.
17+
<pre><code class="language-js">console.log(&quot;Hello World!&quot;);
18+
</code></pre>
19+
.
20+
21+
js:
22+
.
23+
```js:
24+
console.log("Hello World!");
25+
```
26+
.
27+
<pre><code class="language-js">console.log(&quot;Hello World!&quot;);
28+
</code></pre>
29+
.
30+
31+
js:hello.js (named code blocks)
32+
.
33+
```js:hello.js
34+
console.log("Hello World!");
35+
```
36+
.
37+
<pre class="named-fence-block"><code class="language-js">console.log(&quot;Hello World!&quot;);
38+
</code><div class="named-fence-filename">hello.js</div></pre>
39+
.
40+
41+
:hello.js
42+
.
43+
```:hello.js
44+
console.log("Hello World!");
45+
```
46+
.
47+
<pre><code>console.log(&quot;Hello World!&quot;);
48+
</code></pre>
49+
.
50+
51+
hello.js
52+
.
53+
```hello.js
54+
console.log("Hello World!");
55+
```
56+
.
57+
<pre><code class="language-hello.js">console.log(&quot;Hello World!&quot;);
58+
</code></pre>
59+
.
60+
61+
js:hello.js:second.js
62+
.
63+
```js:hello.js:second.js
64+
console.log("Hello World!");
65+
```
66+
.
67+
<pre><code>console.log(&quot;Hello World!&quot;);
68+
</code></pre>
69+
.
70+
71+
hello.js:second.js (named code blocks, but language is incorrect)
72+
.
73+
```hello.js:second.js
74+
console.log("Hello World!");
75+
```
76+
.
77+
<pre class="named-fence-block"><code class="language-hello.js">console.log(&quot;Hello World!&quot;);
78+
</code><div class="named-fence-filename">second.js</div></pre>
79+
.
80+
81+
js langAttr
82+
.
83+
```js langAttr
84+
console.log("Hello World!");
85+
```
86+
.
87+
<pre><code class="language-js">console.log(&quot;Hello World!&quot;);
88+
</code></pre>
89+
.
90+
91+
js:hello.js langAttr
92+
.
93+
```js:hello.js langAttr
94+
console.log("Hello World!");
95+
```
96+
.
97+
<pre class="named-fence-block"><code class="language-js">console.log(&quot;Hello World!&quot;);
98+
</code><div class="named-fence-filename">hello.js</div></pre>
99+
.

0 commit comments

Comments
 (0)