Skip to content

Commit 7c0aa96

Browse files
authored
Revert "♻️ refactor: Language check" (#37)
This reverts commit 4ea9167.
1 parent a053bd7 commit 7c0aa96

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

src/components/modules/shared/CodeBlock.tsx

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import dynamic from 'next/dynamic'
33
import type { ReactNode } from 'react'
44
import { lazy, Suspense, useMemo, useState } from 'react'
55

6+
import { HighLighterPrismCdn } from '~/components/ui/code-highlighter'
67
import { ShikiHighLighterWrapper } from '~/components/ui/code-highlighter/shiki/ShikiWrapper'
78
import { parseShouldCollapsedFromAttrs } from '~/components/ui/code-highlighter/shiki/utils'
89
import { ExcalidrawLoading } from '~/components/ui/excalidraw/ExcalidrawLoading'
@@ -64,37 +65,39 @@ export const CodeBlockRender = (props: {
6465
const { lang } = props
6566
const nextProps = { ...props }
6667
nextProps.content = formatCode(props.content)
67-
const ShikiHighLighter =
68-
shikiImport ??
69-
lazy(() =>
70-
import('~/components/ui/code-highlighter/shiki/Shiki').then(
71-
(mod) => ({
72-
default: mod.ShikiHighLighter,
73-
}),
74-
),
68+
if (lang) {
69+
const ShikiHighLighter =
70+
shikiImport ??
71+
lazy(() =>
72+
import('~/components/ui/code-highlighter').then((mod) => ({
73+
default: mod.ShikiFallback,
74+
})),
75+
)
76+
if (isClientSide) {
77+
shikiImport = ShikiHighLighter
78+
}
79+
80+
const fallback = (
81+
<ShikiHighLighterWrapper
82+
{...nextProps}
83+
shouldCollapsed={parseShouldCollapsedFromAttrs(props.attrs || '')}
84+
>
85+
<pre className="bg-transparent px-5">
86+
<code className="!px-5 !text-base-content">
87+
{nextProps.content}
88+
</code>
89+
</pre>
90+
</ShikiHighLighterWrapper>
91+
)
92+
if (!isClientSide) return fallback
93+
return (
94+
<Suspense fallback={fallback}>
95+
<ShikiHighLighter {...nextProps} />
96+
</Suspense>
7597
)
76-
if (isClientSide) {
77-
shikiImport = ShikiHighLighter
7898
}
7999

80-
const fallback = (
81-
<ShikiHighLighterWrapper
82-
{...nextProps}
83-
shouldCollapsed={parseShouldCollapsedFromAttrs(props.attrs || '')}
84-
>
85-
<pre className="bg-transparent px-5">
86-
<code className="!px-5 !text-base-content">
87-
{nextProps.content}
88-
</code>
89-
</pre>
90-
</ShikiHighLighterWrapper>
91-
)
92-
if (!isClientSide) return fallback
93-
return (
94-
<Suspense fallback={fallback}>
95-
<ShikiHighLighter {...nextProps} />
96-
</Suspense>
97-
)
100+
return <HighLighterPrismCdn {...nextProps} />
98101
}
99102
}
100103
}, [props])

src/components/ui/code-highlighter/CodeHighlighter.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type React from 'react'
22
import type { FC } from 'react'
3-
import { useCallback, useEffect, useInsertionEffect, useRef } from 'react'
3+
import {
4+
use,
5+
useCallback,
6+
useEffect,
7+
useInsertionEffect,
8+
useMemo,
9+
useRef,
10+
} from 'react'
411

512
import { useIsPrintMode } from '~/atoms/css-media'
613
import { useIsDark } from '~/hooks/common/use-is-dark'
@@ -10,6 +17,8 @@ import { loadScript, loadStyleSheet } from '~/lib/load-script'
1017
import { toast } from '~/lib/toast'
1118

1219
import styles from './CodeHighlighter.module.css'
20+
import type { ShikiProps } from './shiki/Shiki'
21+
import { ShikiHighLighter } from './shiki/Shiki'
1322

1423
declare global {
1524
interface Window {
@@ -140,3 +149,22 @@ const useLoadHighlighter = (ref: React.RefObject<HTMLElement | null>) => {
140149
})
141150
}, [])
142151
}
152+
let bundledLanguagesKeysSet: Set<string> | null = null
153+
export const ShikiFallback: FC<ShikiProps> = (props) => {
154+
const { lang } = props
155+
const shikiSupported = use(
156+
useMemo(async () => {
157+
if (!lang) return false
158+
159+
if (!bundledLanguagesKeysSet) {
160+
const { bundledLanguages } = await import('shiki/langs')
161+
bundledLanguagesKeysSet = new Set(Object.keys(bundledLanguages))
162+
}
163+
164+
return bundledLanguagesKeysSet.has(lang)
165+
}, [lang]),
166+
)
167+
return (
168+
<ShikiHighLighter {...props} lang={shikiSupported ? props.lang : 'text'} />
169+
)
170+
}

src/components/ui/code-highlighter/shiki/Shiki.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const ShikiHighLighter: FC<ShikiProps> = (props) => {
4848

4949
const { bundledLanguages } = await import('shiki/langs')
5050

51-
if (!language || !(bundledLanguages as any)[language]) return
51+
if (!language) return
5252
const importFn = (bundledLanguages as any)[language]
5353
if (!importFn) return
5454
return loadShikiLanguage(language || '', importFn)

0 commit comments

Comments
 (0)