Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions packages/elements/src/code-block/CodeBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { BundledLanguage } from 'shiki'
import type { HTMLAttributes } from 'vue'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { reactiveOmit } from '@vueuse/core'
import { reactiveOmit, useDebounceFn } from '@vueuse/core'
import { computed, onBeforeUnmount, provide, ref, watch } from 'vue'
import { CodeBlockKey } from './context'
import { highlightCode } from './utils'
Expand Down Expand Up @@ -31,23 +31,27 @@ provide(CodeBlockKey, {
let requestId = 0
let isUnmounted = false

watch(
() => [props.code, props.language, props.showLineNumbers] as const,
async ([code, language, showLineNumbers]) => {
requestId += 1
const currentId = requestId
const updateHighlight = useDebounceFn(async (code: string, language: BundledLanguage, showLineNumbers: boolean) => {
requestId += 1
const currentId = requestId

try {
const [light, dark] = await highlightCode(code, language, showLineNumbers)
try {
const [light, dark] = await highlightCode(code, language, showLineNumbers)

if (currentId === requestId && !isUnmounted) {
html.value = light
darkHtml.value = dark
}
}
catch (error) {
console.error('[CodeBlock] highlight failed', error)
if (currentId === requestId && !isUnmounted) {
html.value = light
darkHtml.value = dark
}
}
catch (error) {
console.error('[CodeBlock] highlight failed', error)
}
}, 100)

watch(
() => [props.code, props.language, props.showLineNumbers] as const,
([code, language, showLineNumbers]) => {
updateHighlight(code, language, showLineNumbers)
},
{ immediate: true },
)
Expand Down