Skip to content

Commit

Permalink
feat(docs): highlight code block
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangmo8 committed Oct 25, 2024
1 parent d143272 commit c9b5dc1
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 30 deletions.
3 changes: 0 additions & 3 deletions apps/www/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import autoprefixer from 'autoprefixer'
import tailwind from 'tailwindcss'
import Icons from 'unplugin-icons/vite'
import { defineConfig } from 'vitepress'
import { cssVariables } from './theme/config/shiki'

import { siteConfig } from './theme/config/site'
import CodeWrapperPlugin from './theme/plugins/codewrapper'
Expand All @@ -31,7 +30,6 @@ export default defineConfig({
['meta', { name: 'og:site_name', content: siteConfig.name }],
['meta', { name: 'og:image', content: siteConfig.ogImage }],
['meta', { name: 'twitter:image', content: siteConfig.ogImage }],

],

sitemap: {
Expand All @@ -58,7 +56,6 @@ export default defineConfig({

srcDir: path.resolve(__dirname, '../src'),
markdown: {
theme: cssVariables,
codeTransformers: [
transformerMetaWordHighlight(),
],
Expand Down
8 changes: 2 additions & 6 deletions apps/www/.vitepress/theme/components/BlockPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import { useConfigStore } from '@/stores/config'
import { CircleHelp, Info, Monitor, Smartphone, Tablet } from 'lucide-vue-next'
import MagicString from 'magic-string'
import { codeToHtml } from 'shiki'
import { reactive, ref, watch } from 'vue'
import { compileScript, parse, walk } from 'vue/compiler-sfc'
import { cssVariables } from '../config/shiki'
import { highlight } from '../config/shiki'
import BlockCopyButton from './BlockCopyButton.vue'
import Spinner from './Spinner.vue'
import StyleSwitcher from './StyleSwitcher.vue'
Expand Down Expand Up @@ -79,10 +78,7 @@ watch([style, codeConfig], async () => {
})
}
codeHtml.value = await codeToHtml(rawString.value, {
lang: 'vue',
theme: cssVariables,
})
codeHtml.value = highlight(rawString.value, 'vue')
}
catch (err) {
console.error(err)
Expand Down
8 changes: 2 additions & 6 deletions apps/www/.vitepress/theme/components/ComponentPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { cn } from '@/lib/utils'
import { useConfigStore } from '@/stores/config'
import { useClipboard } from '@vueuse/core'
import MagicString from 'magic-string'
import { codeToHtml } from 'shiki'
import { computed, ref, watch } from 'vue'
import { cssVariables } from '../config/shiki'
import { highlight } from '../config/shiki'
import CodeSandbox from './CodeSandbox.vue'
import ComponentLoader from './ComponentLoader.vue'
import Stackblitz from './Stackblitz.vue'
Expand Down Expand Up @@ -37,10 +36,7 @@ function transformImportPath(code: string) {
watch([style, codeConfig], async () => {
try {
rawString.value = await import(`../../../src/lib/registry/${style.value}/example/${props.name}.vue?raw`).then(res => res.default.trim())
codeHtml.value = await codeToHtml(transformedRawString.value, {
lang: 'vue',
theme: cssVariables,
})
codeHtml.value = highlight(transformedRawString.value, 'vue')
}
catch (err) {
console.error(err)
Expand Down
43 changes: 39 additions & 4 deletions apps/www/.vitepress/theme/config/shiki.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
import { createCssVariablesTheme } from 'shiki'
import type { HighlighterCore } from 'shiki/core'
import type { ThemeOptions } from 'vitepress'
import { computedAsync } from '@vueuse/core'
import { createHighlighterCore } from 'shiki/core'
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript'

export const cssVariables = createCssVariablesTheme({
variablePrefix: '--shiki-',
variableDefaults: {},
export const shikiThemes: ThemeOptions = {
light: 'vitesse-light',
dark: 'vitesse-dark',
}

export const highlighter = computedAsync<HighlighterCore>(async (onCancel) => {
const shiki = await createHighlighterCore({
engine: createJavaScriptRegexEngine(),
themes: [
() => import('shiki/themes/vitesse-dark.mjs'),
() => import('shiki/themes/vitesse-light.mjs'),
],
langs: [
() => import('shiki/langs/javascript.mjs'),
() => import('shiki/langs/vue.mjs'),
],
})

onCancel(() => shiki?.dispose())
return shiki
})

export function highlight(code: string, lang: string) {
if (!highlighter.value)
return code

return highlighter.value.codeToHtml(code, {
lang,
defaultColor: false,
themes: {
dark: 'vitesse-dark',
light: 'vitesse-light',
},
})
}
17 changes: 13 additions & 4 deletions apps/www/.vitepress/theme/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:root {
--font-geist-sans: "geist-sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";

--background: 0 0% 100%;
--foreground: 240 10% 3.9%;
--card: 0 0% 100%;
Expand Down Expand Up @@ -121,6 +121,15 @@
scrollbar-color: hsl(215.4 16.3% 56.9% / 0.3);
}

html.dark .shiki,
html.dark .shiki span {
color: var(--shiki-dark);
}
html:not(.dark) .shiki,
html:not(.dark) .shiki span {
color: var(--shiki-light);
}

.hide-scrollbar::-webkit-scrollbar {
display: none;
}
Expand All @@ -147,7 +156,7 @@
@apply absolute w-9 h-9 bg-muted rounded-full font-mono font-medium text-center text-base inline-flex items-center justify-center -indent-px border-4 border-background;
@apply -ml-[50px] -mt-1;
content: counter(step);
}
}
}

@media (max-width: 640px) {
Expand All @@ -157,7 +166,7 @@
}

div[class^="language-"] {
@apply mb-4 mt-6 max-h-[650px] overflow-x-auto md:rounded-lg border !bg-secondary-foreground dark:!bg-secondary
@apply mb-4 mt-6 max-h-[650px] overflow-x-auto md:rounded-lg border
}
pre {
@apply py-4;
Expand All @@ -177,7 +186,7 @@ pre code {

pre code .line {
@apply px-4 min-h-4 !py-0.5 w-full inline-block leading-[--vp-code-line-height];
}
}

.line-number {
@apply !text-[.75rem] !inline-block text-muted-foreground leading-[--vp-code-line-height];
Expand Down
2 changes: 1 addition & 1 deletion apps/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"markdown-it": "^14.1.0",
"pathe": "^1.1.2",
"rimraf": "^6.0.1",
"shiki": "^1.17.7",
"shiki": "^1.22.1",
"tailwind-merge": "^2.5.2",
"tailwindcss": "^3.4.12",
"tsx": "^4.19.1",
Expand Down
61 changes: 55 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c9b5dc1

Please sign in to comment.