Syntax Highlighting in Vue Markdown with Shiki
Highlight fenced code blocks in light and dark themes with Shiki in Vue and Vite.
App.vue
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { content } from './content'
import { Comark } from '@comark/vue'
import highlight from '@comark/vue/plugins/highlight'
// Import themes and languages directly
import githubLight from '@shikijs/themes/github-light'
import githubDark from '@shikijs/themes/github-dark'
import python from '@shikijs/langs/python'
import rust from '@shikijs/langs/rust'
import go from '@shikijs/langs/go'
import sql from '@shikijs/langs/sql'
import css from '@shikijs/langs/css'
// Theme toggle
const isDark = ref(false)
onMounted(() => {
isDark.value = document.body.classList.contains('dark')
})
function toggleTheme() {
isDark.value = !isDark.value
document.body.classList.toggle('dark', isDark.value)
}
</script>
<template>
<div>
<button
class="theme-toggle"
@click="toggleTheme"
>
{{ isDark ? '☀️ Light' : '🌙 Dark' }}
</button>
<Suspense>
<Comark
:plugins="[
highlight({
themes: {
light: githubLight,
dark: githubDark,
},
languages: [python, rust, go, sql, css],
}),
]"
>
{{ content }}
</Comark>
</Suspense>
</div>
</template>
<style>
.shiki .line {
display: block;
}
.shiki .line:empty {
height: 1lh;
}
.shiki .line.highlight {
background-color: rgba(255, 255, 0, 0.1);
display: block;
margin: 0 -1rem;
padding: 0 1rem;
}
.dark .shiki span {
color: var(--shiki-dark) !important;
}
</style>
This example highlights fenced code blocks in Vue and Vite with @comark/vue/plugins/shiki, direct theme and grammar imports, and a light/dark theme toggle.
Features
- Dual themes: emits light and dark token colors via
--shiki-darkCSS variables and toggles between them ondocument.body. - Direct theme and language imports: loads individual modules from
@shikijs/themesand@shikijs/langsso bundlers tree-shake unused grammars. - Line highlighting: marks lines specified in fence attributes (such as
{2-3}) with a.highlightclass. - Optional
<pre>styles: sets inline background and foreground styles whenpreStylesis enabled.
Usage
Install Shiki and its theme and language packages:
npm install shiki @shikijs/themes @shikijs/langsImport the
shikiplugin along with the themes and languages you need:import shiki from '@comark/vue/plugins/shiki' import githubLight from '@shikijs/themes/github-light' import githubDark from '@shikijs/themes/github-dark' import javascript from '@shikijs/langs/javascript' import typescript from '@shikijs/langs/typescript' import python from '@shikijs/langs/python'Pass the configured plugin to
<Markdown>:<template> <Suspense> <Markdown :plugins="[ shiki({ themes: { light: githubLight, dark: githubDark, }, languages: [javascript, typescript, python], }), ]" > {{ content }} </Markdown> </Suspense> </template>Add fenced code blocks in Markdown:
```javascript console.log('Hello, World!') ``` ```typescript const greeting: string = 'Hello, TypeScript!' ``` ```python print('Hello, Python!') ```
Why import themes and languages directly
- Smaller client bundle: Vite bundles only the grammars and themes your app imports.
- Build-time validation: missing theme or language module names fail during compilation instead of at runtime.
- TypeScript autocomplete: imported modules carry Shiki's
ThemeRegistrationandLanguageRegistrationtypes.
Configuration options
import type { BundledLanguage, BundledTheme } from 'shiki'
interface ShikiOptions {
// Themes imported from @shikijs/themes
themes?: Record<string, BundledTheme>
// Languages imported from @shikijs/langs
languages?: BundledLanguage[]
// Add inline background and foreground styles to <pre> elements
preStyles?: boolean
}