Skip to content

Commit

Permalink
fix: shiki code block cls and lcp
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 18, 2024
1 parent 83771f2 commit d9dfd41
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
23 changes: 13 additions & 10 deletions src/components/modules/shared/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,25 @@ export const CodeBlockRender = (props: {
if (isClientSide) {
shikiImport = ShikiHighLighter
}

const fallback = (
<ShikiHighLighterWrapper {...nextProps}>
<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={
<ShikiHighLighterWrapper {...nextProps}>
<pre className="bg-transparent px-5">
<code className="!px-5">{nextProps.content}</code>
</pre>
</ShikiHighLighterWrapper>
}
>
<Suspense fallback={fallback}>
<ShikiHighLighter {...nextProps} />
</Suspense>
)
}

return <HighLighterPrismCdn {...props} />
return <HighLighterPrismCdn {...nextProps} />
}
}
}, [props])
Expand Down
13 changes: 8 additions & 5 deletions src/components/ui/code-highlighter/index.demo.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Suspense } from 'react'
import type { DocumentComponent } from 'storybook/typings'

import { ShikiHighLighter as Shiki } from './shiki/Shiki'

export const ShikiHighLighter: DocumentComponent = () => {
return (
<Shiki
content={`import {
<Suspense>
<Shiki
content={`import {
useCallback,
useEffect,
useLayoutEffect,
Expand Down Expand Up @@ -194,8 +196,9 @@ export const ShikiHighLighter: DocumentComponent = () => {
)
}
`}
lang="tsx"
attrs='filename="ShikiHighLighter.tsx" {3,4}'
/>
lang="tsx"
attrs='filename="ShikiHighLighter.tsx" {3,4}'
/>
</Suspense>
)
}
3 changes: 3 additions & 0 deletions src/components/ui/code-highlighter/shiki/Shiki.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.code-card {
@apply relative my-6 overflow-hidden rounded-md;

font-size: 14px;

:global {
pre {
@apply !m-0 !p-0;
Expand Down
8 changes: 4 additions & 4 deletions src/components/ui/code-highlighter/shiki/Shiki.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from 'react'
import { use, useEffect, useMemo, useState } from 'react'
import type { FC } from 'react'

import { isServerSide } from '~/lib/env'
Expand All @@ -12,7 +12,7 @@ interface Props {
attrs?: string
}

const codeHighlighter = await (async () => {
const codeHighlighterPromise = (async () => {
if (isServerSide) return
const [{ getHighlighterCore }, getWasm, { codeHighlighter }] =
await Promise.all([
Expand Down Expand Up @@ -54,15 +54,15 @@ const codeHighlighter = await (async () => {

export const ShikiHighLighter: FC<Props> = (props) => {
const { lang: language, content: value, attrs } = props

const codeHighlighter = use(codeHighlighterPromise)
const highlightedHtml = useMemo(() => {
return codeHighlighter?.({
attrs: attrs || '',
// code: `${value.split('\n')[0].repeat(10)} // [!code highlight]\n${value}`,
code: value,
lang: language ? language.toLowerCase() : '',
})
}, [attrs, language, value])
}, [attrs, codeHighlighter, language, value])

const [renderedHtml, setRenderedHtml] = useState(highlightedHtml)
const [codeBlockRef, setCodeBlockRef] = useState<HTMLDivElement | null>(null)
Expand Down

0 comments on commit d9dfd41

Please sign in to comment.