Skip to content

Commit c34b35d

Browse files
committed
feat(shiki): add shiki composable
1 parent 4e3380f commit c34b35d

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

components/content/prose-code.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@
1616
<Icon name="uil:copy" />
1717
</button>
1818
</div>
19-
<slot />
19+
<div v-html="output" />
2020
</div>
2121
</template>
2222

2323
<script setup lang="ts">
24+
import { useShiki } from "~/composables/use-shiki";
25+
import { type BundledLanguage } from "shiki";
26+
2427
interface ProseCodeProperties {
2528
code: string;
26-
language: string;
29+
language: BundledLanguage;
2730
filename?: string;
2831
highlights?: number[];
2932
meta?: string;
3033
}
3134
3235
const properties = defineProps<ProseCodeProperties>();
36+
const { code, language } = toRefs(properties);
37+
const { output } = await useShiki({ code, language });
3338
const copied = ref(false);
3439
const path = computed(() => {
35-
if (properties.language === "output") {
40+
if (properties.language as string === "output") {
3641
return "Output";
3742
}
3843
@@ -71,6 +76,9 @@ const copy = async () => {
7176
font-family: 'JetBrains Mono', Fira Code, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace;
7277
font-size: 14px;
7378
letter-spacing: 0.7px;
79+
display: flex;
80+
flex-direction: column;
81+
gap: 0.4em;
7482
}
7583
7684
// Result styling

composables/use-shiki.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createCssVariablesTheme } from "shiki";
2+
import { type BundledLanguage, getHighlighter } from "shiki/bundle/full";
3+
import type { Ref } from "vue";
4+
5+
interface ShikiParameters {
6+
code: Ref<string>;
7+
language: Ref<BundledLanguage>;
8+
}
9+
10+
const cssVariablesTheme = createCssVariablesTheme({
11+
name: "css-variables",
12+
variablePrefix: "--shiki-",
13+
variableDefaults: {},
14+
fontStyle: true
15+
});
16+
17+
export const useShiki = async ({ code, language }: ShikiParameters) => {
18+
const highlighter = await getHighlighter({
19+
langs: [ language.value ],
20+
themes: [ cssVariablesTheme ],
21+
});
22+
23+
const output = computed(() => {
24+
return highlighter.codeToHtml(code.value.trim(), {
25+
theme: cssVariablesTheme,
26+
lang: language.value,
27+
});
28+
});
29+
30+
return {
31+
output,
32+
};
33+
};

0 commit comments

Comments
 (0)