1
+ import { createHighlighterCore } from 'shiki/core' ;
2
+ import { loadWasm } from 'shiki/engine/oniguruma' ;
1
3
import type { CheerioAPI } from 'cheerio' ;
2
- import hljs from 'highlight.js' ;
3
4
4
- const DEFAULT_LANGUAGE_SUBSET = [ 'typescript' , 'html' , 'css' , 'scss' , 'json' ] ;
5
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6
+ // @ts -ignore
7
+ await loadWasm ( import ( 'shiki/dist/onig.wasm' ) ) ;
8
+
9
+ const highlighter = await createHighlighterCore ( {
10
+ themes : [
11
+ import ( 'shiki/themes/github-dark.mjs' ) , // dark mode
12
+ import ( 'shiki/themes/github-light.mjs' ) , // light mode
13
+ ] ,
14
+ langs : [
15
+ import ( 'shiki/langs/shell.mjs' ) ,
16
+ import ( 'shiki/langs/bash.mjs' ) ,
17
+ import ( 'shiki/langs/json.mjs' ) ,
18
+ import ( 'shiki/langs/typescript.mjs' ) ,
19
+ import ( 'shiki/langs/angular-ts.mjs' ) ,
20
+ import ( 'shiki/langs/angular-html.mjs' ) ,
21
+ ] ,
22
+ } ) ;
23
+
24
+ const themes = highlighter . getLoadedThemes ( ) ;
5
25
6
26
type RewriteAdapter = ( $ : CheerioAPI ) => void ;
7
27
@@ -18,7 +38,7 @@ export const rewriteHTML = (...adapters: RewriteAdapter[]) => {
18
38
* @param $
19
39
*/
20
40
export const wpCodeRewriter : RewriteAdapter = ( $ ) => {
21
- $ ( 'pre' ) . each ( ( index , element ) => {
41
+ $ ( 'pre' ) . each ( ( _ , element ) => {
22
42
const code = $ ( element ) . text ( ) ;
23
43
24
44
// Check if the content is already wrapped in a <code> block
@@ -29,46 +49,41 @@ export const wpCodeRewriter: RewriteAdapter = ($) => {
29
49
// Also add `hljs` class to make it apply hljs styling schema
30
50
if ( ! hasCodeBlock ) {
31
51
$ ( element ) . html ( `<code class="hljs">${ code } </code>` ) ;
32
- } else {
33
- $ ( element ) . children ( 'code' ) . addClass ( 'hljs' ) ;
34
52
}
35
53
36
- // Detect the language and apply syntax highlighting
37
- const highlightedCode = hljs . highlightAuto (
38
- code ,
39
- DEFAULT_LANGUAGE_SUBSET ,
40
- ) . value ;
54
+ const classAttr = $ ( element ) . find ( 'code' ) . attr ( ) [ 'class' ] ;
55
+ const classes = classAttr . split ( ' ' ) ;
56
+ const codeLanguageClass = classes . find ( ( cl ) =>
57
+ / ^ l a n g u a g e - [ \w - ] + $ / . test ( cl ) ,
58
+ ) ;
41
59
42
- // Replace the content of the <code> block with the highlighted code
43
- $ ( element ) . children ( 'code' ) . html ( highlightedCode ) ;
44
- } ) ;
45
- } ;
60
+ let language : string ;
46
61
47
- /**
48
- * Rewrites code blocks generated by `crayon` plugin and applies HLJS styling
49
- * @param $
50
- */
51
- export const crayonCodeRewriter : RewriteAdapter = ( $ ) => {
52
- $ ( '.crayon-syntax' ) . each ( ( _ , element ) => {
53
- const $element = $ ( element ) ;
54
- let code = '' ;
62
+ if ( codeLanguageClass ) {
63
+ language = codeLanguageClass . replace ( 'language-' , '' ) ;
64
+ } else {
65
+ language = 'angular-ts' ;
66
+ }
55
67
56
- // Extract code from Crayon lines
57
- $element . find ( '.crayon-line' ) . each ( ( _ , line ) => {
58
- code += $ ( line ) . text ( ) + '\n' ;
59
- } ) ;
68
+ if ( language === 'typescript' || language === 'ts' ) {
69
+ language = 'angular-ts' ;
70
+ }
60
71
61
- // Detect the language and apply syntax highlighting
62
- const highlightedCode = hljs . highlightAuto (
63
- code ,
64
- DEFAULT_LANGUAGE_SUBSET ,
65
- ) . value ;
72
+ if ( language === 'html' ) {
73
+ language = 'angular-html' ;
74
+ }
66
75
67
- // Create a new <pre><code> element with the highlighted code
68
- const preCodeBlock = `<pre><code class="hljs">${ highlightedCode } </code></pre>` ;
76
+ const highlightedCode = highlighter . codeToHtml ( code , {
77
+ theme : highlighter . getLoadedThemes ( ) [ 0 ] ,
78
+ themes : {
79
+ dark : themes [ 0 ] ,
80
+ light : themes [ 1 ] ,
81
+ } ,
82
+ lang : language ,
83
+ } ) ;
69
84
70
- // Replace the entire crayon-syntax element with the new preCodeBlock
71
- $element . replaceWith ( preCodeBlock ) ;
85
+ // Replace the content of the <code> block with the highlighted code
86
+ $ ( element ) . replaceWith ( highlightedCode ) ;
72
87
} ) ;
73
88
} ;
74
89
0 commit comments