Skip to content

Commit 8aaf71d

Browse files
committed
prevent failing when language is not loaded
1 parent 79cbda8 commit 8aaf71d

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"boring-avatars": "^1.11.2",
3030
"ckeditor5": "^44.1.0",
3131
"clsx": "^2.1.1",
32-
"highlight.js": "^11.11.1",
3332
"html-react-parser": "^5.1.18",
3433
"js-base64": "^3.7.7",
3534
"js-cookie": "^3.0.5",

src/features/code-highlight/CodeHighlightProvider.tsx

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,65 @@
11
'use client';
22

33
import type { ReactNode } from 'react';
4-
import {
5-
CodeHighlightAdapterProvider,
6-
stripShikiCodeBlocks,
7-
} from '@mantine/code-highlight';
4+
import { CodeHighlightAdapterProvider, stripShikiCodeBlocks } from '@mantine/code-highlight';
85

9-
import {
10-
transformerNotationDiff,
11-
transformerNotationHighlight,
12-
} from '@shikijs/transformers';
6+
import {
7+
transformerNotationDiff,
8+
transformerNotationHighlight,
9+
transformerNotationFocus,
10+
transformerMetaHighlight,
11+
} from '@shikijs/transformers'
1312

1413
import { darkTheme, lightTheme } from './code-highlight-themes';
1514

15+
const preloadedLanguages = [
16+
'tsx', 'scss', 'html', 'bash',
17+
'json', 'javascript', 'typescript', 'css',
18+
'markdown', 'go', 'python', 'java', 'kotlin',
19+
'rust', 'php', 'ruby', 'swift', 'dart', 'scala',
20+
];
21+
1622
async function loadShiki() {
1723
const { createHighlighter } = await import('shiki');
18-
const shiki = await createHighlighter({
19-
langs: ['tsx', 'scss', 'html', 'bash', 'json'],
24+
const highlighter = await createHighlighter({
25+
langs: preloadedLanguages,
2026
themes: [],
2127
});
2228

23-
return shiki;
29+
return highlighter;
2430
}
2531

2632
const customShikiAdapter: any = {
2733
loadContext: loadShiki,
2834

2935
getHighlighter: (ctx: any) => {
3036
if (!ctx) {
31-
return ({ code }: { code: string }) => ({
32-
highlightedCode: code,
33-
isHighlighted: false,
34-
});
37+
return ({ code }: { code: string }) => ({highlightedCode: code, isHighlighted: false });
3538
}
3639

37-
return ({
38-
code,
39-
language,
40-
colorScheme,
41-
}: {
42-
code: string;
43-
language: string;
44-
colorScheme: 'light' | 'dark';
45-
}) => ({
46-
isHighlighted: true,
47-
highlightedCode: stripShikiCodeBlocks(
48-
ctx.codeToHtml(code, {
49-
lang: language,
50-
theme: (colorScheme === 'light' ? lightTheme : darkTheme) as any,
51-
transformers: [transformerNotationDiff(), transformerNotationHighlight()],
52-
})
53-
),
54-
});
40+
return ({ code, language, colorScheme }) => {
41+
42+
// if the language is not loaded don't highlight it
43+
if (!ctx.getLoadedLanguages().includes(language)) {
44+
return {highlightedCode: code, isHighlighted: false };
45+
}
46+
47+
return {
48+
isHighlighted: true,
49+
highlightedCode: stripShikiCodeBlocks(
50+
ctx.codeToHtml(code, {
51+
lang: language,
52+
theme: (colorScheme === 'light' ? lightTheme : darkTheme) as any,
53+
transformers: [
54+
transformerNotationDiff(),
55+
transformerNotationHighlight(),
56+
transformerNotationFocus(),
57+
transformerMetaHighlight(),
58+
],
59+
})
60+
),
61+
};
62+
};
5563
},
5664
};
5765

0 commit comments

Comments
 (0)