Skip to content

Commit 5149960

Browse files
committed
Revert "Improve codeblock highlight style"
1 parent 6c8f61c commit 5149960

File tree

6 files changed

+48
-38
lines changed

6 files changed

+48
-38
lines changed

.changeset/fast-pigs-itch.md

-5
This file was deleted.

packages/gitbook/src/components/DocumentView/CodeBlock/ClientCodeBlock.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useEffect, useMemo, useRef, useState } from 'react';
55

66
import { useInViewportListener } from '@/components/hooks/useInViewportListener';
77
import { useScrollListener } from '@/components/hooks/useScrollListener';
8-
import { useTheme } from 'next-themes';
98
import { useDebounceCallback } from 'usehooks-ts';
109
import type { BlockProps } from '../Block';
1110
import { CodeBlockRenderer } from './CodeBlockRenderer';
@@ -27,7 +26,6 @@ export function ClientCodeBlock(props: ClientBlockProps) {
2726
const [isInViewport, setIsInViewport] = useState(false);
2827
const plainLines = useMemo(() => plainHighlight(block, []), [block]);
2928
const [lines, setLines] = useState<null | HighlightLine[]>(null);
30-
const { resolvedTheme } = useTheme();
3129

3230
// Preload the highlighter when the block is mounted.
3331
useEffect(() => {
@@ -80,7 +78,7 @@ export function ClientCodeBlock(props: ClientBlockProps) {
8078

8179
if (typeof window !== 'undefined') {
8280
import('./highlight').then(({ highlight }) => {
83-
highlight(block, inlines, resolvedTheme).then((lines) => {
81+
highlight(block, inlines).then((lines) => {
8482
if (cancelled) {
8583
return;
8684
}
@@ -97,7 +95,7 @@ export function ClientCodeBlock(props: ClientBlockProps) {
9795

9896
// Otherwise if the block is not in viewport, we reset to the plain lines
9997
setLines(null);
100-
}, [isInViewport, block, inlines, resolvedTheme]);
98+
}, [isInViewport, block, inlines]);
10199

102100
return (
103101
<CodeBlockRenderer ref={blockRef} block={block} style={style} lines={lines ?? plainLines} />

packages/gitbook/src/components/DocumentView/CodeBlock/CodeBlock.tsx

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type { DocumentBlockCode } from '@gitbook/api';
33
import { getNodeFragmentByType } from '@/lib/document';
44
import { isV2 } from '@/lib/v2';
55

6-
import { MiddlewareHeaders } from '@v2/lib/middleware';
7-
import { headers } from 'next/headers';
86
import type { BlockProps } from '../Block';
97
import { Blocks } from '../Blocks';
108
import { ClientCodeBlock } from './ClientCodeBlock';
@@ -40,14 +38,9 @@ export async function CodeBlock(props: BlockProps<DocumentBlockCode>) {
4038

4139
if (isV2() && !isEstimatedOffscreen) {
4240
// In v2, we render the code block server-side
43-
const theme = getTheme();
44-
const lines = await highlight(block, richInlines, theme);
41+
const lines = await highlight(block, richInlines);
4542
return <CodeBlockRenderer block={block} style={style} lines={lines} />;
4643
}
4744

4845
return <ClientCodeBlock block={block} style={style} inlines={richInlines} />;
4946
}
50-
51-
function getTheme() {
52-
return headers().get(MiddlewareHeaders.Theme) || 'light';
53-
}

packages/gitbook/src/components/DocumentView/CodeBlock/CodeBlockRenderer.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { BlockProps } from '../Block';
99
import { CopyCodeButton } from './CopyCodeButton';
1010
import type { HighlightLine, HighlightToken } from './highlight';
1111

12+
import './theme.css';
1213
import './CodeBlockRenderer.css';
1314

1415
type CodeBlockRendererProps = Pick<BlockProps<DocumentBlockCode>, 'block' | 'style'> & {

packages/gitbook/src/components/DocumentView/CodeBlock/highlight.ts

+13-21
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import type {
33
DocumentBlockCodeLine,
44
DocumentInlineAnnotation,
55
} from '@gitbook/api';
6-
import { type ThemedToken, createSingletonShorthands, createdBundledHighlighter } from 'shiki/core';
6+
import {
7+
type ThemedToken,
8+
createCssVariablesTheme,
9+
createSingletonShorthands,
10+
createdBundledHighlighter,
11+
} from 'shiki/core';
712
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
813
import { type BundledLanguage, bundledLanguages } from 'shiki/langs';
9-
import githubDarkDefault from 'shiki/themes/github-dark-default.mjs';
10-
import minLight from 'shiki/themes/min-light.mjs';
14+
1115
import { plainHighlight } from './plain-highlight';
1216

1317
export type HighlightLine = {
@@ -29,15 +33,12 @@ export type RenderedInline = {
2933
body: React.ReactNode;
3034
};
3135

32-
const themes = {
33-
light: minLight,
34-
dark: githubDarkDefault,
35-
};
36+
const theme = createCssVariablesTheme();
3637

3738
const { getSingletonHighlighter } = createSingletonShorthands(
3839
createdBundledHighlighter<any, any>({
3940
langs: bundledLanguages,
40-
themes: themes,
41+
themes: {},
4142
engine: () => createJavaScriptRegexEngine({ forgiving: true, target: 'ES2018' }),
4243
})
4344
);
@@ -50,7 +51,7 @@ export async function preloadHighlight(block: DocumentBlockCode) {
5051
if (langName) {
5152
await getSingletonHighlighter({
5253
langs: [langName],
53-
themes: Object.values(themes),
54+
themes: [theme],
5455
});
5556
}
5657
}
@@ -60,11 +61,9 @@ export async function preloadHighlight(block: DocumentBlockCode) {
6061
*/
6162
export async function highlight(
6263
block: DocumentBlockCode,
63-
inlines: RenderedInline[],
64-
theme?: string
64+
inlines: RenderedInline[]
6565
): Promise<HighlightLine[]> {
6666
const langName = getBlockLang(block);
67-
6867
if (!langName) {
6968
// Language not found, fallback to plain highlighting
7069
return plainHighlight(block, inlines);
@@ -74,19 +73,12 @@ export async function highlight(
7473

7574
const highlighter = await getSingletonHighlighter({
7675
langs: [langName],
77-
themes: Object.values(themes),
76+
themes: [theme],
7877
});
7978

80-
const selectedTheme = (() => {
81-
if (theme === 'dark') {
82-
return themes.dark;
83-
}
84-
return themes.light;
85-
})();
86-
8779
const lines = highlighter.codeToTokensBase(code, {
8880
lang: langName,
89-
theme: selectedTheme,
81+
theme,
9082
tokenizeMaxLineLength: 400,
9183
});
9284

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
:root {
2+
--shiki-color-text: theme("colors.tint.11");
3+
--shiki-token-constant: #0a6355;
4+
--shiki-token-string: #8b6d32;
5+
--shiki-token-comment: theme("colors.teal.700/.64");
6+
--shiki-token-keyword: theme("colors.pomegranate.600");
7+
--shiki-token-parameter: #0a3069;
8+
--shiki-token-function: #8250df;
9+
--shiki-token-string-expression: #6a4906;
10+
--shiki-token-punctuation: theme("colors.pomegranate.700/.92");
11+
--shiki-token-link: theme("colors.tint.12");
12+
--shiki-token-inserted: #22863a;
13+
--shiki-token-deleted: #b31d28;
14+
--shiki-token-changed: #8250df;
15+
}
16+
17+
html.dark {
18+
--shiki-color-text: theme("colors.tint.11");
19+
--shiki-token-constant: #d19a66;
20+
--shiki-token-string: theme("colors.pomegranate.300");
21+
--shiki-token-comment: theme("colors.teal.300/.64");
22+
--shiki-token-keyword: theme("colors.pomegranate.400");
23+
--shiki-token-parameter: theme("colors.yellow.500");
24+
--shiki-token-function: #56b6c2;
25+
--shiki-token-string-expression: theme("colors.tint.11");
26+
--shiki-token-punctuation: #acc6ee;
27+
--shiki-token-link: theme("colors.pomegranate.400");
28+
--shiki-token-inserted: #85e89d;
29+
--shiki-token-deleted: #fdaeb7;
30+
--shiki-token-changed: #56b6c2;
31+
}

0 commit comments

Comments
 (0)