Skip to content

Commit db3cc20

Browse files
committed
refactor: CodeBlock migration
1 parent fc5ab2d commit db3cc20

File tree

6 files changed

+89
-32
lines changed

6 files changed

+89
-32
lines changed

packages/notion-to-jsx/src/components/Renderer/components/Block/BlockRenderer.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ const BlockRenderer: React.FC<Props> = ({ block, onFocus, index }) => {
6767
</div>
6868
);
6969

70-
// case 'code':
71-
// return (
72-
// <div {...blockProps}>
73-
// <CodeBlock
74-
// code={block.code.rich_text[0].text.content}
75-
// language={block.code.language}
76-
// caption={block.code.caption?.[0]?.plain_text}
77-
// />
78-
// </div>
79-
// );
70+
case 'code':
71+
return (
72+
<div {...blockProps}>
73+
<CodeBlock
74+
code={block.code.rich_text[0].text.content}
75+
language={block.code.language}
76+
caption={block.code.caption?.[0]?.plain_text}
77+
/>
78+
</div>
79+
);
8080

8181
// case 'image':
8282
// return (

packages/notion-to-jsx/src/components/Renderer/components/Code/CodeBlock.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import React, { useMemo } from 'react';
2+
import { RichTextItem } from '../../../../types';
3+
import { codeBlock } from './styles.css';
24
import Prism, { Grammar } from 'prismjs';
5+
import { MemoizedRichText } from '../MemoizedComponents';
6+
37
import 'prismjs/themes/prism.css';
48
import 'prismjs/components/prism-typescript';
59
import 'prismjs/components/prism-javascript';
610
import 'prismjs/components/prism-jsx';
711
import 'prismjs/components/prism-tsx';
812

9-
import { MemoizedRichText } from '../MemoizedComponents';
10-
import { CodeBlockWrapper } from './styles';
11-
import { RichTextItem } from '../../../../types';
12-
import { Caption } from '../Media';
13-
14-
// Ensure Prism is available in the browser environment
1513
if (typeof window !== 'undefined') {
1614
window.Prism = Prism;
1715
}
@@ -31,11 +29,13 @@ const CodeBlock: React.FC<Props> = ({ code, language, caption }) => {
3129

3230
return (
3331
<>
34-
<CodeBlockWrapper dangerouslySetInnerHTML={{ __html: highlightedCode }} />
32+
<pre className={codeBlock}>
33+
<code dangerouslySetInnerHTML={{ __html: highlightedCode }} />
34+
</pre>
3535
{caption && (
36-
<Caption>
36+
<figcaption>
3737
<MemoizedRichText richTexts={caption} />
38-
</Caption>
38+
</figcaption>
3939
)}
4040
</>
4141
);
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { default as CodeBlock } from './CodeBlock';
2-
export * from './styles';
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { style, globalStyle } from '@vanilla-extract/css';
2+
import { vars } from '../../../../styles/theme.css';
3+
4+
export const codeBlock = style({
5+
backgroundColor: vars.colors.code.background,
6+
color: vars.colors.code.text,
7+
padding: vars.spacing.md,
8+
borderRadius: vars.borderRadius.md,
9+
overflow: 'auto',
10+
fontFamily: vars.typography.fontFamily.code,
11+
fontSize: '0.85em',
12+
lineHeight: vars.typography.lineHeight.relaxed,
13+
margin: `${vars.spacing.sm} 0`,
14+
});
15+
16+
globalStyle(`${codeBlock} code`, {
17+
fontFamily: 'inherit',
18+
});
19+
20+
globalStyle(
21+
`${codeBlock} .token.comment, ${codeBlock} .token.prolog, ${codeBlock} .token.doctype, ${codeBlock} .token.cdata`,
22+
{
23+
color: '#666666',
24+
fontStyle: 'italic',
25+
}
26+
);
27+
28+
globalStyle(`${codeBlock} .token.punctuation`, {
29+
color: '#999999',
30+
});
31+
32+
globalStyle(
33+
`${codeBlock} .token.property, ${codeBlock} .token.tag, ${codeBlock} .token.boolean, ${codeBlock} .token.number, ${codeBlock} .token.constant, ${codeBlock} .token.symbol`,
34+
{
35+
color: '#905',
36+
}
37+
);
38+
39+
globalStyle(
40+
`${codeBlock} .token.selector, ${codeBlock} .token.attr-name, ${codeBlock} .token.string, ${codeBlock} .token.char, ${codeBlock} .token.builtin`,
41+
{
42+
color: '#690',
43+
}
44+
);
45+
46+
globalStyle(
47+
`${codeBlock} .token.operator, ${codeBlock} .token.entity, ${codeBlock} .token.url, ${codeBlock} .language-css .token.string, ${codeBlock} .style .token.string`,
48+
{
49+
color: '#9a6e3a',
50+
}
51+
);
52+
53+
globalStyle(
54+
`${codeBlock} .token.atrule, ${codeBlock} .token.attr-value, ${codeBlock} .token.keyword`,
55+
{
56+
color: '#07a',
57+
}
58+
);
59+
60+
globalStyle(`${codeBlock} .token.function, ${codeBlock} .token.class-name`, {
61+
color: '#dd4a68',
62+
});
63+
64+
globalStyle(
65+
`${codeBlock} .token.regex, ${codeBlock} .token.important, ${codeBlock} .token.variable`,
66+
{
67+
color: '#e90',
68+
}
69+
);

packages/notion-to-jsx/src/components/Renderer/components/Code/styles.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/notion-to-jsx/tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export default defineConfig({
88
splitting: false,
99
sourcemap: true,
1010
clean: true,
11-
external: ['react', 'react-dom', 'styled-components'],
11+
external: ['react', 'react-dom', 'styled-components', 'prismjs'],
1212
esbuildPlugins: [vanillaExtractPlugin()],
1313
});

0 commit comments

Comments
 (0)