Skip to content

Commit

Permalink
refactor: optimize Shiki (#34)
Browse files Browse the repository at this point in the history
* ♻️ refactor: Language check

* ♻️ refactor: Remove Shiki fallback
  • Loading branch information
zhuozhiyongde authored Aug 28, 2024
1 parent b9bea2c commit 4ea9167
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 61 deletions.
59 changes: 28 additions & 31 deletions src/components/modules/shared/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import dynamic from 'next/dynamic'
import type { ReactNode } from 'react'
import { lazy, Suspense, useMemo, useState } from 'react'

import { HighLighterPrismCdn } from '~/components/ui/code-highlighter'
import { ShikiHighLighterWrapper } from '~/components/ui/code-highlighter/shiki/ShikiWrapper'
import { parseShouldCollapsedFromAttrs } from '~/components/ui/code-highlighter/shiki/utils'
import { ExcalidrawLoading } from '~/components/ui/excalidraw/ExcalidrawLoading'
Expand Down Expand Up @@ -65,39 +64,37 @@ export const CodeBlockRender = (props: {
const { lang } = props
const nextProps = { ...props }
nextProps.content = formatCode(props.content)
if (lang) {
const ShikiHighLighter =
shikiImport ??
lazy(() =>
import('~/components/ui/code-highlighter').then((mod) => ({
default: mod.ShikiFallback,
})),
)
if (isClientSide) {
shikiImport = ShikiHighLighter
}

const fallback = (
<ShikiHighLighterWrapper
{...nextProps}
shouldCollapsed={parseShouldCollapsedFromAttrs(props.attrs || '')}
>
<pre className="bg-transparent px-5">
<code className="!px-5 !text-base-content">
{nextProps.content}
</code>
</pre>
</ShikiHighLighterWrapper>
)
if (!isClientSide) return fallback
return (
<Suspense fallback={fallback}>
<ShikiHighLighter {...nextProps} />
</Suspense>
const ShikiHighLighter =
shikiImport ??
lazy(() =>
import('~/components/ui/code-highlighter/shiki/Shiki').then(
(mod) => ({
default: mod.ShikiHighLighter,
}),
),
)
if (isClientSide) {
shikiImport = ShikiHighLighter
}

return <HighLighterPrismCdn {...nextProps} />
const fallback = (
<ShikiHighLighterWrapper
{...nextProps}
shouldCollapsed={parseShouldCollapsedFromAttrs(props.attrs || '')}
>
<pre className="bg-transparent px-5">
<code className="!px-5 !text-base-content">
{nextProps.content}
</code>
</pre>
</ShikiHighLighterWrapper>
)
if (!isClientSide) return fallback
return (
<Suspense fallback={fallback}>
<ShikiHighLighter {...nextProps} />
</Suspense>
)
}
}
}, [props])
Expand Down
30 changes: 1 addition & 29 deletions src/components/ui/code-highlighter/CodeHighlighter.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import type React from 'react'
import type { FC } from 'react'
import {
use,
useCallback,
useEffect,
useInsertionEffect,
useMemo,
useRef,
} from 'react'
import { useCallback, useEffect, useInsertionEffect, useRef } from 'react'

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

import styles from './CodeHighlighter.module.css'
import type { ShikiProps } from './shiki/Shiki'
import { ShikiHighLighter } from './shiki/Shiki'

declare global {
interface Window {
Expand Down Expand Up @@ -149,22 +140,3 @@ const useLoadHighlighter = (ref: React.RefObject<HTMLElement | null>) => {
})
}, [])
}
let bundledLanguagesKeysSet: Set<string> | null = null
export const ShikiFallback: FC<ShikiProps> = (props) => {
const { lang } = props
const shikiSupported = use(
useMemo(async () => {
if (!lang) return false

if (!bundledLanguagesKeysSet) {
const { bundledLanguages } = await import('shiki/langs')
bundledLanguagesKeysSet = new Set(Object.keys(bundledLanguages))
}

return bundledLanguagesKeysSet.has(lang)
}, [lang]),
)
return (
<ShikiHighLighter {...props} lang={shikiSupported ? props.lang : 'text'} />
)
}
2 changes: 1 addition & 1 deletion src/components/ui/code-highlighter/shiki/Shiki.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const ShikiHighLighter: FC<ShikiProps> = (props) => {

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

if (!language) return
if (!language || !(bundledLanguages as any)[language]) return
const importFn = (bundledLanguages as any)[language]
if (!importFn) return
return loadShikiLanguage(language || '', importFn)
Expand Down

0 comments on commit 4ea9167

Please sign in to comment.