Skip to content

Commit 17b90df

Browse files
committed
feat(extensions): code
1 parent 5c6b798 commit 17b90df

File tree

6 files changed

+320
-1
lines changed

6 files changed

+320
-1
lines changed

extensions/code/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dependencies": {
3030
"@vue-motion/core": "workspace:*",
3131
"@vue-motion/lib": "workspace:*",
32+
"shiki": "^1.17.6",
3233
"vue": "^3.4.26"
3334
},
3435
"devDependencies": {

extensions/code/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type { CodeOptions } from './index.vue'
2+
export { default as Code } from './index.vue'

extensions/code/src/index.vue

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script setup lang="ts">
2+
import { defineWidget } from '@vue-motion/core'
3+
import { WidgetOptions, widget } from '@vue-motion/lib'
4+
import { CodeToHastOptions, codeToHtml } from 'shiki'
5+
import { ref, useSlots } from 'vue';
6+
7+
export interface CodeOptions extends WidgetOptions {
8+
shikiOptions?: CodeToHastOptions
9+
lang: string
10+
theme: string
11+
width: number
12+
height: number
13+
}
14+
15+
const props = defineProps<CodeOptions>()
16+
const options = defineWidget<CodeOptions>(props)
17+
18+
const slots = useSlots()
19+
const codes = slots.default ? slots.default().map(vnode => vnode.children).join('') : ''
20+
const content = ref(await codeToHtml(codes, {
21+
...options.shikiOptions,
22+
lang: options.lang,
23+
theme: options.theme,
24+
}))
25+
</script>
26+
27+
<template>
28+
<g v-bind="widget(options)">
29+
<foreignObject x="20" y="20" :width="options.width" :height="options.height">
30+
<!--
31+
In the context of SVG embeded into HTML, the XHTML namespace could
32+
be avoided, but it is mandatory in the context of an SVG document
33+
-->
34+
<div xmlns="http://www.w3.org/1999/xhtml" style="color: white" v-html="content"></div>
35+
</foreignObject>
36+
</g>
37+
</template>

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"test": "pnpm run --filter=* test",
2020
"lint": "eslint .",
2121
"lint:fix": "eslint --fix .",
22-
"pub:core": "pnpm run build:core && pnpm run --filter=./packages/* --sort pub"
22+
"pub:core": "pnpm run build:core && pnpm run --filter=./packages/* --sort pub",
23+
"preversion": "pnpm build:core && pnpm build:ext",
24+
"change": "changeset add",
25+
"release": "pnpm preversion && changeset version",
26+
"pub": "changeset publish"
2327
},
2428
"devDependencies": {
2529
"@antfu/eslint-config": "^2.26.0",

0 commit comments

Comments
 (0)