Skip to content

Commit 404202d

Browse files
committed
fix: fixing the code client
1 parent 265a8af commit 404202d

File tree

6 files changed

+70
-65
lines changed

6 files changed

+70
-65
lines changed

lib/Code/Code.Client.tsx

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,26 @@ import { Fragment, useLayoutEffect, useState } from 'react';
77
import { jsx, jsxs } from 'react/jsx-runtime';
88
import type { BundledLanguage } from 'shiki/bundle/web';
99
import { CodeBlockComponent } from './Code';
10+
import { transformerNotationHighlight } from '@shikijs/transformers';
1011

1112
const highlight = async (
1213
code: string,
1314
lang: BundledLanguage,
1415
className: string = '',
15-
theme: 'dark' | 'light' | 'auto' = 'auto',
1616
fileName?: string,
1717
) => {
1818
const codeToHast = await import('shiki/bundle/web').then((mod) => mod.codeToHast);
1919

20-
let themeOrThemes: any = {
20+
const hast = await codeToHast(code, {
21+
lang,
2122
themes: {
2223
dark: 'material-theme-ocean',
2324
light: 'min-light',
2425
},
25-
};
26-
27-
if (theme === 'dark') themeOrThemes = { theme: 'material-theme-ocean' };
28-
else if (theme === 'light') themeOrThemes = { theme: 'min-light' };
29-
30-
const out = await codeToHast(code, {
31-
lang,
32-
...themeOrThemes,
26+
transformers: [transformerNotationHighlight()],
3327
});
3428

35-
return toJsxRuntime(out, {
29+
return toJsxRuntime(hast, {
3630
Fragment,
3731
jsx,
3832
jsxs,
@@ -48,35 +42,41 @@ const highlight = async (
4842
}) as JSX.Element;
4943
};
5044

51-
export const PreBlockClient = ({
52-
children,
53-
className,
54-
fileName,
55-
lang = 'text',
56-
theme = 'auto',
57-
...rest
58-
}: {
59-
children: any;
60-
className?: string;
45+
const CodeBlockClient = (props: {
46+
children: string;
47+
className: string;
48+
lang: BundledLanguage;
6149
fileName?: string;
62-
lang?: BundledLanguage | 'text';
63-
theme?: 'dark' | 'light' | 'auto';
64-
[key: string]: any;
6550
}) => {
51+
console.log('CodeBlockClient', props);
52+
6653
const [nodes, setNodes] = useState(<></>);
6754

55+
let lang = 'text'; // default monospaced text
56+
57+
if (props.lang) {
58+
lang = props.lang.replace('lang-', '');
59+
}
60+
61+
if (props.className && props.className.startsWith('lang-')) {
62+
lang = props.className.replace('lang-', '');
63+
}
64+
6865
useLayoutEffect(() => {
69-
void highlight(children ?? '', lang as BundledLanguage, className, theme, fileName).then(
70-
setNodes,
71-
);
72-
}, [children, lang, className, theme, fileName]);
66+
void highlight(
67+
props.children ?? '',
68+
lang as BundledLanguage,
69+
props.className,
70+
props.fileName,
71+
).then(setNodes);
72+
}, [props.children, props.lang, props.className]);
73+
74+
return nodes;
75+
};
7376

74-
return (
75-
<pre
76-
className={clsx('code-block', className)}
77-
{...rest}
78-
>
79-
{nodes}
80-
</pre>
81-
);
77+
export const PreBlockClient = ({ children }: { children: any }) => {
78+
if ('type' in children && children['type'] === 'code') {
79+
return CodeBlockClient(children['props']);
80+
}
81+
return children;
8282
};

lib/Code/Code.Server.tsx

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ interface Props {
1313
lang: BundledLanguage;
1414
className: string;
1515
fileName?: string;
16-
highlight?: string;
1716
}
1817

1918
async function CodeBlock(props: Props) {
@@ -27,26 +26,6 @@ async function CodeBlock(props: Props) {
2726
lang = props.className.replace('lang-', '');
2827
}
2928

30-
const decorations: DecorationItem[] = [];
31-
32-
if (props.highlight) {
33-
const linesAndRanges = props.highlight.split(',');
34-
linesAndRanges.forEach((line) => {
35-
if (line.includes('-')) {
36-
const [start, end] = line.split('-');
37-
decorations.push({
38-
start: parseInt(start),
39-
end: parseInt(end),
40-
properties: {
41-
class: 'highlighted',
42-
},
43-
});
44-
} else {
45-
decorations.push({ start: parseInt(line), end: parseInt(line) });
46-
}
47-
});
48-
}
49-
5029
const hast = await codeToHast(props.children, {
5130
lang,
5231
themes: {

lib/Code/Copy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function CopyButton({ value, className, src, ...props }: CopyButtonProps)
4343
<span className="sr-only">Copy</span>
4444
{hasCopied ? <CheckIcon size={14} /> : <CopyIcon size={14} />}
4545
</button>
46-
<div className="group-hover:block hidden absolute mt-2 text-center px-2 py-0.5 bg-gray-100 dark:bg-gray-800 rounded-md shadow-sm border dark:border-gray-800">
46+
<div className="group-hover:block hidden absolute mt-2 text-center px-2 py-0.5 bg-gray-100 dark:bg-gray-800 rounded-md shadow-sm border dark:border-gray-800 whitespace-nowrap -translate-x-1/2 left-1/2">
4747
{hasCopied ? 'Copied!' : 'Copy'}
4848
</div>
4949
</div>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"dependencies": {
7777
"clsx": "^2.1.1",
7878
"lucide-react": "^0.469.0",
79+
"markdown-to-jsx": "^7.7.3",
7980
"next-mdx-remote": "^5.0.0",
8081
"react-medium-image-zoom": "^5.2.12",
8182
"react-router": "^7.1.1",

pnpm-lock.yaml

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

src/pages/CodeSamples.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import Markdown from 'markdown-to-jsx';
12
import { PreBlockClient } from '../../lib/Code/Code.Client';
23

3-
export const CodeSamples = () => {
4-
return (
5-
<PreBlockClient lang="jsx">
6-
{`
4+
const code = `
5+
6+
\`\`\`tsx fileName="src/pages/BlogListPage.tsx"
77
// Fetch data from the server
88
const getData = async () => {
99
const siteId = "*****";
@@ -34,8 +34,20 @@ const BlogListPage = async () => {
3434
);
3535
}
3636
37-
export default BlogListPage;
38-
`.trim()}
39-
</PreBlockClient>
37+
export default BlogListPage;
38+
\`\`\`
39+
`.trim();
40+
41+
export const CodeSamples = () => {
42+
return (
43+
<Markdown
44+
options={{
45+
overrides: {
46+
pre: PreBlockClient,
47+
},
48+
}}
49+
>
50+
{code}
51+
</Markdown>
4052
);
4153
};

0 commit comments

Comments
 (0)