Skip to content

Commit 5425c56

Browse files
committed
feat: Code tokenize add
1 parent 85f04ea commit 5425c56

File tree

2 files changed

+36
-62
lines changed

2 files changed

+36
-62
lines changed

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import React, { useMemo } from 'react';
22
import { codeBlock } from './styles.css';
3-
import Prism, { Grammar } from 'prismjs';
3+
import Prism, { Grammar, Token } from 'prismjs';
44
import { MemoizedRichText } from '../MemoizedComponents';
55
import { RichTextItem } from '../RichText/RichTexts';
66

7+
import 'prismjs/components/prism-typescript';
8+
import 'prismjs/components/prism-jsx';
9+
import 'prismjs/components/prism-tsx';
10+
711
if (typeof window !== 'undefined') {
812
window.Prism = Prism;
913
}
@@ -14,17 +18,42 @@ export interface Props {
1418
caption?: RichTextItem[];
1519
}
1620

21+
const renderToken = (token: string | Token, i: number): React.ReactNode => {
22+
if (typeof token === 'string') {
23+
return <span key={i}>{token}</span>;
24+
}
25+
26+
const content = token.content;
27+
let tokenContent: React.ReactNode;
28+
29+
if (Array.isArray(content)) {
30+
tokenContent = content.map((subToken, j) => renderToken(subToken, j));
31+
} else if (typeof content === 'object' && content !== null) {
32+
tokenContent = renderToken(content as Token, 0);
33+
} else {
34+
tokenContent = content;
35+
}
36+
37+
return (
38+
<span key={i} className={`token ${token.type}`}>
39+
{tokenContent}
40+
</span>
41+
);
42+
};
43+
1744
const CodeBlock: React.FC<Props> = ({ code, language, caption }) => {
18-
const highlightedCode = useMemo(() => {
45+
const tokens = useMemo(() => {
1946
const prismLanguage =
2047
Prism.languages[language] || Prism.languages.plaintext;
21-
return Prism.highlight(code, prismLanguage as Grammar, language);
48+
return Prism.tokenize(code, prismLanguage as Grammar);
2249
}, [code, language]);
2350

2451
return (
2552
<>
26-
<pre className={codeBlock}>
27-
<code dangerouslySetInnerHTML={{ __html: highlightedCode }} />
53+
<pre className={`${codeBlock} language-${language}`}>
54+
<code className={`language-${language}`}>
55+
{tokens.map((token, i) => renderToken(token, i))}
56+
</code>
2857
</pre>
2958
{caption && (
3059
<figcaption>
Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { style, globalStyle } from '@vanilla-extract/css';
1+
import { style } from '@vanilla-extract/css';
22
import { vars } from '../../../../styles/theme.css';
33

44
export const codeBlock = style({
@@ -8,62 +8,7 @@ export const codeBlock = style({
88
borderRadius: vars.borderRadius.md,
99
overflow: 'auto',
1010
fontFamily: vars.typography.fontFamily.code,
11-
fontSize: '0.85em',
11+
fontSize: '0.85em !important',
1212
lineHeight: vars.typography.lineHeight.relaxed,
1313
margin: `${vars.spacing.sm} 0`,
1414
});
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-
);

0 commit comments

Comments
 (0)