vitepress-plugin-sfc 2.0.0-beta.1 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,156 +1,51 @@
1
- # Vitepress Plugin Component
1
+ # VitePress Plugin SFC
2
2
 
3
- [简体中文](README-zh-CN.md)
3
+ [Documentation](https://vitepress-plugin-sfc-playgrou.vercel.app/)
4
4
 
5
- A VitePress plugin to display Vue components in your documentation with an automatically generated two-tab interface: one for the component's preview and another to show its source code.
5
+ Allows you to write Vue SFC within Markdown files and render them in VitePress.
6
6
 
7
- ## Features
7
+ ## Install
8
8
 
9
- - [x] **Component Preview** - Directly view the rendered output of your component within the documentation.
10
- - [x] **Syntax Highlighting** - Compatible with VitePress's syntax highlighting.
11
- - [x] **Code Copying** - One-click copy for the component's source code.
12
- - [x] **Dark Mode Support** - Automatically adapts to VitePress's light and dark themes.
13
- - [x] **Lightweight** - No complex dependencies, only `2kb` / gzip:`1kb`.
14
- - [x] **Internationalization Support** - Default support for Chinese and English, configurable for other languages.
15
-
16
- ## Installation
17
-
18
- ```bash
19
- npm install vitepress-plugin-sfc --save-dev
9
+ ```shell
10
+ npm install -D vitepress-plugin-sfc
20
11
  ```
21
12
 
22
13
  ## Configuration
23
14
 
24
- ### Add Markdown Plugin
25
-
26
- Add the plugin to your VitePress configuration file (`.vitepress/config.js` or `.vitepress/config.ts`):
15
+ Configure it in `.vitepress/config.ts`:
27
16
 
28
17
  ```ts
29
- import { defineConfig } from 'vitepress'
30
- import { componentViewMarkdownPlugin } from 'vitepress-plugin-sfc'
18
+ import { defineConfig } from "vitepress";
19
+ import { sfcPlugin, sfcRegistryVitePlugin } from "vitepress-plugin-sfc";
31
20
 
32
21
  export default defineConfig({
33
- // ... other configurations
34
-
22
+ // ...other configurations
35
23
  markdown: {
36
24
  config: (md) => {
37
- md.use(componentViewMarkdownPlugin)
38
- }
25
+ md.use(sfcPlugin);
26
+ },
39
27
  },
40
- })
41
- ```
42
-
43
- ### Configure Client
44
-
45
- VitePress resolves themes from `.vitepress/theme/index.js` or `.vitepress/theme/index.ts`, and we configure the client here as well.
46
-
47
- ```ts
48
- // theme/index.ts
49
- import DefaultTheme from "vitepress/theme";
50
- import { enhanceAppWithComponentView } from 'vitepress-plugin-sfc/client'
51
-
52
- export default {
53
- extends: DefaultTheme, // Use VitePress's default theme
54
- enhanceApp({ app }) {
55
- enhanceAppWithComponentView(app)
56
- }
57
- }
58
- ```
59
-
60
- The `enhanceAppWithComponentView` function automatically registers the preview component on the passed root component.
61
-
62
- ### Configure Internationalization
63
-
64
- The plugin gets language configuration from the `locales.lang` option in VitePress and supports `zh` and `en` languages by default. You can also manually configure other languages or even override the default languages.
65
-
66
- Configure internationalization in the client configuration:
67
-
68
- ```ts
69
- export default {
70
- // Other configurations
71
- enhanceApp({ app }) {
72
- enhanceAppWithComponentView(app, {
73
- // Configure corresponding languages through localeMappings option
74
- localeMappings: {
75
- fr: {
76
- previewLabel: "foo",
77
- codeLabel: "bar",
78
- },
79
- },
80
- });
28
+ vite: {
29
+ plugins: [sfcRegistryVitePlugin()],
81
30
  },
82
- };
83
- ```
84
-
85
- ## Example
86
-
87
- This is an example of button SFC:
88
-
89
- ```vue
90
- <script setup lang="ts">
91
- import { ref } from 'vue';
92
-
93
- const count = ref(0)
94
- </script>
95
-
96
- <template>
97
- <button class="my-button" @click="count++">
98
- Clicked {{ count }} times
99
- </button>
100
- </template>
101
-
102
- <style>
103
- .my-button {
104
- background-color: #3eaf7c;
105
- color: white;
106
- border: none;
107
- padding: 8px 16px;
108
- border-radius: 4px;
109
- cursor: pointer;
110
- }
111
- </style>
31
+ });
112
32
  ```
113
33
 
114
- VitePress supports [importing Vue components in markdown files](https://vitepress.dev/guide/using-vue#using-components), we import the Button component in the same way:
115
-
116
- ```bash
117
- <script setup>
118
- import Button from './Button.vue'
119
- </script>
120
- ```
121
-
122
- Then use the specified syntax:
123
-
124
- ```markdown
125
- ::: component-view
126
- <Button />
127
- :::
128
- ```
34
+ ## Usage
129
35
 
130
- Here is the complete example code in a markdown file:
36
+ Wrap **valid** Vue SFC content with `:::sfc`:
131
37
 
132
38
  ```markdown
39
+ :::sfc
133
40
  <script setup>
134
- import Button from './Button.vue'
41
+ import { ref } from "vue"
42
+ const msg = ref("");
135
43
  </script>
136
44
 
137
- # This will render a button with click count and a tab bar showing **Preview** and **Source Code**
138
-
139
- ::: component-view
140
- <Button />
45
+ <template>
46
+ <input v-model="msg" placeholder="Please enter text" />
47
+ </template>
141
48
  :::
142
-
143
- ##
144
49
  ```
145
50
 
146
- You can view the component's preview and source code in the tabs above.
147
-
148
- You can check out the [live demo here](https://vitepress-plugin-sfc-playgrou.vercel.app/).
149
-
150
- ## Notes
151
-
152
- - Component paths should be relative to the project root.
153
- - Current version supports simple Vue components; complex components might require additional configuration. - Components must be valid Single File Components (SFCs).
154
- ## License
155
-
156
51
  [MIT](LICENSE)
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
+ import { MarkdownEnv, Plugin } from "vitepress";
1
2
  import LinkifyIt from "linkify-it";
2
- import { Plugin } from "vitepress";
3
3
 
4
4
  //#region \0rolldown/runtime.js
5
5
  declare namespace index_d_exports$1 {
@@ -1065,16 +1065,20 @@ interface SFC {
1065
1065
  content: string;
1066
1066
  hash: string;
1067
1067
  importer: string;
1068
+ markupParams: MarkupParams;
1068
1069
  }
1069
- interface PluginOptions$1 {
1070
- render?: boolean;
1070
+ interface MarkdownEnvExt extends MarkdownEnv {
1071
+ sfcs: SFC[];
1072
+ __matched_sfc_set__: Set<string>;
1071
1073
  }
1072
- declare const sfcPlugin: PluginWithOptions<PluginOptions$1>;
1074
+ interface MarkupParams {
1075
+ showSource: boolean;
1076
+ filepath?: string;
1077
+ component?: string;
1078
+ }
1079
+ declare const sfcPlugin: PluginSimple;
1073
1080
  //#endregion
1074
1081
  //#region src/node/vite.d.ts
1075
- interface PluginOptions {
1076
- md: MarkdownIt;
1077
- }
1078
- declare function sfcRegistryVitePlugin(options: PluginOptions): Plugin;
1082
+ declare function sfcRegistryVitePlugin(): Promise<Plugin>;
1079
1083
  //#endregion
1080
- export { SCRIPT_RE, SFC, SFC_IMPORTER_RE, sfcPlugin, sfcRegistryVitePlugin };
1084
+ export { MarkdownEnvExt, MarkupParams, SCRIPT_RE, SFC, SFC_IMPORTER_RE, sfcPlugin, sfcRegistryVitePlugin };
package/dist/index.mjs CHANGED
@@ -1,5 +1,9 @@
1
- import{Buffer as e}from"node:buffer";import t from"node:crypto";function n(e,t,n){function r(e){return e.trim().split(` `,2)[0]===t}function i(e,n,r,i,a){return e[n].nesting===1&&e[n].attrJoin(`class`,t),a.renderToken(e,n,r,i,a)}n||={};let a=n.marker||`:`,o=a.charCodeAt(0),s=a.length,c=n.validate||r,l=n.render||i;function u(e,n,r,i){let l,u=!1,d=e.bMarks[n]+e.tShift[n],f=e.eMarks[n];if(o!==e.src.charCodeAt(d))return!1;for(l=d+1;l<=f&&a[(l-d)%s]===e.src[l];l++);let p=Math.floor((l-d)/s);if(p<3)return!1;l-=(l-d)%s;let m=e.src.slice(d,l),h=e.src.slice(l,f);if(!c(h,m))return!1;if(i)return!0;let g=n;for(;g++,!(g>=r||(d=e.bMarks[g]+e.tShift[g],f=e.eMarks[g],d<f&&e.sCount[g]<e.blkIndent));)if(o===e.src.charCodeAt(d)&&!(e.sCount[g]-e.blkIndent>=4)){for(l=d+1;l<=f&&a[(l-d)%s]===e.src[l];l++);if(!(Math.floor((l-d)/s)<p)&&(l-=(l-d)%s,l=e.skipSpaces(l),!(l<f))){u=!0;break}}let _=e.parentType,v=e.lineMax;e.parentType=`container`,e.lineMax=g;let y=e.push(`container_`+t+`_open`,`div`,1);y.markup=m,y.block=!0,y.info=h,y.map=[n,g],e.md.block.tokenize(e,n+1,g);let b=e.push(`container_`+t+`_close`,`div`,-1);return b.markup=e.src.slice(d,l),b.block=!0,e.parentType=_,e.lineMax=v,e.line=g+ +!!u,!0}e.block.ruler.before(`fence`,`container_`+t,u,{alt:[`paragraph`,`reference`,`blockquote`,`list`]}),e.renderer.rules[`container_`+t+`_open`]=l,e.renderer.rules[`container_`+t+`_close`]=l}const r=/<script\b[^>]*>([\s\S]*?)<\/script>/,i=/^virtual:VP_SFC_/;function a(e){return t.createHash(`md5`).update(e).digest(`hex`)}function o(t,o){let{render:s=!0}=o||{};t.use(n,`sfc`,{validate:e=>RegExp(`^sfc`).test(e.trim()),render:(n,r)=>{if(!s)return;let i=n[r];if(i.nesting===1){let{content:n,name:r}=i.meta?.sfc||{},a=t.options.highlight?.(n,`vue`,``)??``,o=`<template #v3><${r} /></template>`;return`<ComponentView highlightedSrc="${e.from(a).toString(`base64`)}">\n${o}`}else return`</ComponentView>`}}),t.core.ruler.after(`normalize`,`inject_script`,e=>{let t=`
2
- <script setup><\/script>
3
- `;if(!new RegExp(r,`gi`).test(e.src)){let n=e.src.replace(/---([\s\S]*?)---/gi,e=>e+t);e.src=n===e.src?n+t:n}}),t.core.ruler.after(`block`,`sfc_extract_and_import`,e=>{let t=e.src.split(/\r?\n/),n=[],o,s,c=(e,n)=>{let{type:r,nesting:o,map:s}=e;if(r!==`container_sfc_open`||o!==1||!s)return;let[c,l]=s,u=t.slice(c+1,l).join(`
4
- `),d=a(u),f=`VP_SFC_${d}`,p=`virtual:${f}.vue`;if(!i.test(p))throw Error(`[vitepress-plugin-sfc] Plugin-self Error: Unmatched importer syntax`);let m={content:u,name:f,hash:d,importer:p};return n?.(m),m};for(let t of e.tokens){let{type:e,content:i,map:a}=t;!o&&e===`html_block`&&new RegExp(r,`i`).test(i)&&(o=t);let l=c(t,e=>{t.meta={...t.meta??{},sfc:e},s=t});if(l&&n.push(l),s&&s!==t){let[e]=a||[-1],[n,r]=s.map;e>n&&e<r?t.content=``:s=void 0}}e.env.sfcs=n;let l=n.map(({name:e,importer:t})=>`import ${e} from '${t}'`).join(`
5
- `);l&&o&&(o.content=o.content.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/i,(e,t)=>e.replace(t,`\n${l}\n`+t)))})}const s=o;function c(e){let{md:t}=e||{},n=new Map;return{name:`vite-plugin-sfc-registry`,enforce:`pre`,resolveId:{order:`pre`,filter:{id:new RegExp(i,`gi`)},handler(e){if(n.has(e))return e}},load:{order:`pre`,filter:{id:new RegExp(i,`gi`)},handler(e){if(n.has(e))return n.get(e)}},transform(e,r){if(/.md$/gi.test(r)){let r={};t.use(s,{render:!1}).render(e,r);let i=r.sfcs||[];for(let{importer:e,content:t}of i)n.set(e,t)}}}}export{r as SCRIPT_RE,i as SFC_IMPORTER_RE,s as sfcPlugin,c as sfcRegistryVitePlugin};
1
+ import e from"node:crypto";import{createMarkdownRenderer as t}from"vitepress";function n(e,t,n){function r(e){return e.trim().split(` `,2)[0]===t}function i(e,n,r,i,a){return e[n].nesting===1&&e[n].attrJoin(`class`,t),a.renderToken(e,n,r,i,a)}n||={};let a=n.marker||`:`,o=a.charCodeAt(0),s=a.length,c=n.validate||r,l=n.render||i;function u(e,n,r,i){let l,u=!1,d=e.bMarks[n]+e.tShift[n],f=e.eMarks[n];if(o!==e.src.charCodeAt(d))return!1;for(l=d+1;l<=f&&a[(l-d)%s]===e.src[l];l++);let p=Math.floor((l-d)/s);if(p<3)return!1;l-=(l-d)%s;let m=e.src.slice(d,l),h=e.src.slice(l,f);if(!c(h,m))return!1;if(i)return!0;let g=n;for(;g++,!(g>=r||(d=e.bMarks[g]+e.tShift[g],f=e.eMarks[g],d<f&&e.sCount[g]<e.blkIndent));)if(o===e.src.charCodeAt(d)&&!(e.sCount[g]-e.blkIndent>=4)){for(l=d+1;l<=f&&a[(l-d)%s]===e.src[l];l++);if(!(Math.floor((l-d)/s)<p)&&(l-=(l-d)%s,l=e.skipSpaces(l),!(l<f))){u=!0;break}}let _=e.parentType,v=e.lineMax;e.parentType=`container`,e.lineMax=g;let y=e.push(`container_`+t+`_open`,`div`,1);y.markup=m,y.block=!0,y.info=h,y.map=[n,g],e.md.block.tokenize(e,n+1,g);let b=e.push(`container_`+t+`_close`,`div`,-1);return b.markup=e.src.slice(d,l),b.block=!0,e.parentType=_,e.lineMax=v,e.line=g+ +!!u,!0}e.block.ruler.before(`fence`,`container_`+t,u,{alt:[`paragraph`,`reference`,`blockquote`,`list`]}),e.renderer.rules[`container_`+t+`_open`]=l,e.renderer.rules[`container_`+t+`_close`]=l}const r=/<script\b[^>]*>([\s\S]*?)<\/script>/,i=/^virtual:VP_SFC_/;function a(){return{content:`<script setup>
2
+ <\/script>`,tagOpen:`<script setup>`,type:`script`,contentStripped:`
3
+ `,tagClose:`<\/script>`}}function o(t){return e.createHash(`sha256`).update(t).digest(`hex`)}function s(e){if(!e)return{};let t=e.split(`,`);return Object.fromEntries(t.map(e=>{let[,t,n]=e.match(/([^=]+)=(.*)/)||[];return e===`showSource`||t===`showSource`&&n===`true`?[`showSource`,!0]:[t,n]}))}function c(e){e.use(n,`sfc`,{validate:e=>RegExp(`^sfc`).test(e.trim()),render:(e,t,n,r)=>{let{__matched_sfc_set__:i,sfcBlocks:o}=r;o.scripts.length||o.scripts.push(a());let[s]=o.scripts??[],c=e[t];if(c.nesting===1){let{hash:e,name:t,content:r,markupParams:a,importer:o}=c.meta.sfc;if(!i.has(e)){let{tagOpen:n,tagClose:r,contentStripped:a}=s,c=a+`// [vitepress-plugin-sfc] injected vue virtual imports
4
+ // REGION START
5
+ import ${t} from '${o}'
6
+ // REGION END
7
+ `;s.contentStripped=c,s.content=n+c+r,i.add(e)}return a.showSource?`<ComponentView>\n<template #v3><${t} /></template>\n<template #highlight>${n.highlight(r,`vue`,``)}</template>\n</ComponentView>\n`:`<${t} />`}else return``}}),e.core.ruler.after(`block`,`extract_sfc`,e=>{let t=e.src.split(`
8
+ `),n=e.env.sfcs||[],r;function a(e){let{type:n,nesting:r,map:a,info:c}=e;if(n!==`container_sfc_open`||r!==1||!a)return;let[l,u]=a,d=t.slice(l+1,u).join(`
9
+ `),f=o(d),p=`VP_SFC_${f}`,m=`virtual:${p}.vue`;if(!i.test(m))throw Error(`[vitepress-plugin-sfc] Plugin-self Error: Unmatched importer syntax`);let[,h]=(c??``).split(` `);return{content:d,name:p,hash:f,importer:m,markupParams:s(h)}}for(let t of e.tokens){let{map:e}=t,i=a(t);if(i){r=t,t.meta={...t.meta,sfc:i};let e=n.some(({name:e})=>i?.name===e);i&&!e&&n.push(i)}if(r&&r!==t){let[n]=e||[-1],[i,a]=r.map;n>i&&n<a?t.content=``:r=void 0}}let c=e.env;c.sfcs=[...n],c.__matched_sfc_set__=new Set})}const l=c;async function u(){let e=await t(``,{html:!0});e.use(l);let n=new Map;return{name:`vite-plugin-sfc-registry`,enforce:`pre`,resolveId:{filter:{id:new RegExp(i,`gi`)},handler(e){if(n.has(e))return e}},load:{filter:{id:new RegExp(i,`gi`)},handler(e){if(n.has(e))return n.get(e)}},transform(t,r){if(/.md$/gi.test(r)){let r={};e.render(t,r);let i=r.sfcs||[];for(let{importer:e,content:t}of i)n.set(e,t)}}}}export{r as SCRIPT_RE,i as SFC_IMPORTER_RE,l as sfcPlugin,u as sfcRegistryVitePlugin};
package/package.json CHANGED
@@ -1,10 +1,30 @@
1
1
  {
2
2
  "name": "vitepress-plugin-sfc",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "VitePress plugin that renders inline Vue SFC with tabs showing preview and source code",
5
+ "keywords": [
6
+ "component",
7
+ "plugin",
8
+ "tabs",
9
+ "vitepress",
10
+ "vitepress plugin",
11
+ "vitepress-plugin",
12
+ "vue"
13
+ ],
14
+ "license": "MIT",
15
+ "author": "hotdogc1017@gmail.com",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/heroui-vue/vitepress-plugin-sfc"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src/client"
23
+ ],
5
24
  "type": "module",
6
25
  "main": "dist/index.js",
7
26
  "module": "dist/index.js",
27
+ "types": "./dist/index.d.mts",
8
28
  "exports": {
9
29
  ".": {
10
30
  "types": "./dist/index.d.mts",
@@ -12,50 +32,34 @@
12
32
  },
13
33
  "./client": "./src/client/index.ts"
14
34
  },
15
- "files": [
16
- "dist",
17
- "src/client"
18
- ],
19
- "types": "./dist/index.d.mts",
20
- "scripts": {
21
- "dev": "tsdown -w",
22
- "build": "tsdown --minify",
23
- "bump": "bumpp --sign",
24
- "pub": "npm run build && npm publish --registry=https://registry.npmjs.org/",
25
- "dev:docs": "pnpm -C playground dev"
26
- },
27
- "keywords": [
28
- "vitepress",
29
- "vitepress-plugin",
30
- "vitepress plugin",
31
- "plugin",
32
- "vue",
33
- "component",
34
- "tabs"
35
- ],
36
35
  "publishConfig": {
37
36
  "access": "public",
38
37
  "registry": "https://registry.npmjs.org/"
39
38
  },
40
- "author": "hotdogc1017@gmail.com",
41
- "license": "MIT",
42
- "repository": {
43
- "type": "git",
44
- "url": "https://github.com/heroui-vue/vitepress-plugin-sfc"
45
- },
46
39
  "devDependencies": {
47
- "@babel/parser": "8.0.0-rc.5",
48
- "@babel/traverse": "8.0.0-rc.5",
49
- "@rolldown/pluginutils": "^1.0.1",
50
40
  "@types/markdown-it": "^14.2.0",
51
41
  "@types/markdown-it-container": "^2.0.10",
52
42
  "@types/node": "^22.17.0",
53
43
  "bumpp": "^10.1.0",
54
44
  "markdown-it-container": "^4.0.0",
45
+ "oxfmt": "^0.73.0",
46
+ "oxlint": "^1.88.0",
55
47
  "tsdown": "0.21.10",
56
48
  "typescript": "^5.8.3",
57
49
  "unplugin-vue": "^6.2.0",
58
50
  "vitepress": "next",
51
+ "vitest": "^5.0.4",
59
52
  "vue": "^3.5.0"
53
+ },
54
+ "scripts": {
55
+ "dev": "tsdown -w",
56
+ "build": "tsdown --minify",
57
+ "bump": "bumpp --sign",
58
+ "pub": "npm run build && npm publish --registry=https://registry.npmjs.org/",
59
+ "dev:docs": "pnpm -C playground dev",
60
+ "fmt": "oxfmt",
61
+ "fmt:check": "oxfmt --check",
62
+ "lint": "oxlint",
63
+ "t": "vitest run tests/output.test.ts"
60
64
  }
61
- }
65
+ }
@@ -6,176 +6,160 @@ import { useLocaleMappings } from "./useLocaleMappings";
6
6
  import type { LocaleMappings } from "./useLocaleMappings";
7
7
 
8
8
  const localMappings: LocaleMappings = {
9
- en: {
10
- previewLabel: "Preview",
11
- codeLabel: "Source Code",
12
- },
13
- zh: {
14
- previewLabel: "预览",
15
- codeLabel: "源代码",
16
- },
9
+ en: {
10
+ previewLabel: "Preview",
11
+ codeLabel: "Source Code",
12
+ },
13
+ zh: {
14
+ previewLabel: "预览",
15
+ codeLabel: "源代码",
16
+ },
17
17
  };
18
18
 
19
19
  const props = defineProps<{
20
- src: string;
21
- highlightedSrc?: string;
22
- previewLabel?: string;
23
- codeLabel?: string;
20
+ src: string;
21
+ highlightedSrc?: string;
22
+ previewLabel?: string;
23
+ codeLabel?: string;
24
24
  }>();
25
25
 
26
26
  const { lang } = useData();
27
27
  const { get } = useLocaleMappings();
28
28
 
29
29
  const labels = computed(() => {
30
- const mappings = { ...localMappings, ...(get() ?? {}) };
31
- // Default to English mappings if no locale-specific mappings are found
32
- const { previewLabel: preview, codeLabel: code } =
33
- mappings[lang.value] ?? localMappings.en;
34
- return [
35
- {
36
- label: preview,
37
- value: "preview",
38
- },
39
- { label: code, value: "code" },
40
- ] as const;
30
+ const mappings = { ...localMappings, ...get() };
31
+ // Default to English mappings if no locale-specific mappings are found
32
+ const { previewLabel: preview, codeLabel: code } = mappings[lang.value] ?? localMappings.en;
33
+ return [
34
+ {
35
+ label: preview,
36
+ value: "preview",
37
+ },
38
+ { label: code, value: "code" },
39
+ ] as const;
41
40
  });
42
41
 
43
42
  const decodedSrc = ref("");
44
43
  const highlightedSrc = ref("");
45
44
  const activedTab = ref<"preview" | "code">("preview");
46
- const copied = ref(false);
47
45
  const tabGroupId = useId();
48
46
 
49
47
  watch(
50
- [() => props.src, () => props.highlightedSrc],
51
- async ([src, highlight = ""]) => {
52
- try {
53
- decodedSrc.value = atob(src);
54
- } catch (e) {
55
- decodedSrc.value = src;
56
- }
57
-
58
- try {
59
- highlightedSrc.value = atob(highlight);
60
- } catch (e) {
61
- highlightedSrc.value = highlight;
62
- }
63
-
64
- try {
65
- atob(src);
66
- } catch (e) {}
67
- },
68
- {
69
- immediate: true,
70
- },
48
+ [() => props.src, () => props.highlightedSrc],
49
+ async ([src, highlight = ""]) => {
50
+ try {
51
+ decodedSrc.value = atob(src);
52
+ } catch (e) {
53
+ decodedSrc.value = src;
54
+ }
55
+
56
+ try {
57
+ highlightedSrc.value = atob(highlight);
58
+ } catch (e) {
59
+ highlightedSrc.value = highlight;
60
+ }
61
+
62
+ try {
63
+ atob(src);
64
+ } catch (e) {}
65
+ },
66
+ {
67
+ immediate: true,
68
+ },
71
69
  );
72
-
73
- async function copyCode() {
74
- await navigator.clipboard.writeText(decodedSrc.value);
75
- copied.value = true;
76
- setTimeout(() => {
77
- copied.value = false;
78
- }, 2000);
79
- }
80
70
  </script>
81
71
 
82
72
  <template>
83
- <div class="vp-component-tabs">
84
- <header>
85
- <template v-for="{ label, value } in labels" :key="value">
86
- <input
87
- :id="`tab-${tabGroupId}-${value}`"
88
- type="radio"
89
- :name="`vp-component-tabs-${tabGroupId}`"
90
- style="position: fixed; opacity: 0; pointer-events: none"
91
- :data-checked="activedTab === value"
92
- />
93
- <label
94
- :for="`tab-${tabGroupId}-${value}`"
95
- @click="activedTab = value"
96
- >{{ label }}</label
97
- >
98
- </template>
99
- </header>
100
-
101
- <main v-show="activedTab === 'preview'">
102
- <!-- <slot /> -->
103
- <slot name="v3" />
104
- </main>
105
-
106
- <div
107
- v-show="activedTab === 'code'"
108
- :class="`language-vue${activedTab === 'code' ? ' active' : ''}`"
109
- style="margin: 0px"
110
- >
111
- <button title="" class="copy"></button>
112
- <span class="lang">Vue</span>
113
- <span v-html="highlightedSrc" />
114
- </div>
73
+ <div class="vp-component-tabs">
74
+ <header>
75
+ <template v-for="{ label, value } in labels" :key="value">
76
+ <input
77
+ :id="`tab-${tabGroupId}-${value}`"
78
+ type="radio"
79
+ :name="`vp-component-tabs-${tabGroupId}`"
80
+ style="position: fixed; opacity: 0; pointer-events: none"
81
+ :data-checked="activedTab === value" />
82
+ <label :for="`tab-${tabGroupId}-${value}`" @click="activedTab = value">{{ label }}</label>
83
+ </template>
84
+ </header>
85
+
86
+ <main v-show="activedTab === 'preview'">
87
+ <!-- <slot /> -->
88
+ <slot name="v3" />
89
+ </main>
90
+
91
+ <div
92
+ v-show="activedTab === 'code'"
93
+ :class="`language-vue${activedTab === 'code' ? ' active' : ''}`"
94
+ style="margin: 0px">
95
+ <button title="copy source code" class="copy"></button>
96
+ <span class="lang">Vue</span>
97
+ <slot name="highlight" />
115
98
  </div>
99
+ </div>
116
100
  </template>
117
101
 
118
102
  <style>
119
103
  .vp-component-tabs {
120
- margin: 16px 0;
121
- display: grid;
122
- grid-template-columns: 1fr;
123
- grid-template-rows: min-content 1fr;
124
- row-gap: 16px;
104
+ margin: 16px 0;
105
+ display: grid;
106
+ grid-template-columns: 1fr;
107
+ grid-template-rows: min-content 1fr;
108
+ row-gap: 16px;
125
109
  }
126
110
 
127
111
  .vp-component-tabs > header {
128
- display: flex;
129
- position: relative;
130
- border-radius: 8px;
131
- overflow-x: auto;
132
- overflow-y: hidden;
112
+ display: flex;
113
+ position: relative;
114
+ border-radius: 8px;
115
+ overflow-x: auto;
116
+ overflow-y: hidden;
133
117
  }
134
118
 
135
119
  .vp-component-tabs > header input + label {
136
- box-sizing: border-box;
137
- display: inline-block;
138
- position: relative;
139
- padding: 8px 12px;
140
- font-size: 14px;
141
- font-weight: 500;
142
- color: var(--vp-code-tab-text-color);
143
- white-space: nowrap;
144
- cursor: pointer;
145
- transition:
146
- color 0.25s,
147
- background-color 0.25s;
120
+ box-sizing: border-box;
121
+ display: inline-block;
122
+ position: relative;
123
+ padding: 8px 12px;
124
+ font-size: 14px;
125
+ font-weight: 500;
126
+ color: var(--vp-code-tab-text-color);
127
+ white-space: nowrap;
128
+ cursor: pointer;
129
+ transition:
130
+ color 0.25s,
131
+ background-color 0.25s;
148
132
  }
149
133
 
150
134
  .vp-component-tabs > header input + label::after {
151
- content: "";
152
- position: absolute;
153
- height: 4px;
154
- border-radius: 2px;
155
- left: 8px;
156
- right: 8px;
157
- bottom: -1px;
158
- transition: background-color 0.25s;
135
+ content: "";
136
+ position: absolute;
137
+ height: 4px;
138
+ border-radius: 2px;
139
+ left: 8px;
140
+ right: 8px;
141
+ bottom: -1px;
142
+ transition: background-color 0.25s;
159
143
  }
160
144
  .vp-component-tabs > header input[data-checked="true"] + label::after {
161
- background-color: var(--vp-code-tab-active-bar-color);
145
+ background-color: var(--vp-code-tab-active-bar-color);
162
146
  }
163
147
  .vp-component-tabs > header input[data-checked="true"] + label,
164
148
  .vp-component-tabs > header label:hover {
165
- color: var(--vp-code-tab-active-text-color);
149
+ color: var(--vp-code-tab-active-text-color);
166
150
  }
167
151
 
168
152
  .vp-component-tabs > main {
169
- padding: 20px;
170
- background-color: var(--vp-nav-bg-color);
171
- border-radius: 8px;
172
- outline: 1px solid var(--vp-c-divider);
153
+ padding: 20px;
154
+ background-color: var(--vp-nav-bg-color);
155
+ border-radius: 8px;
156
+ outline: 1px solid var(--vp-c-divider);
173
157
  }
174
158
 
175
159
  .vp-component-tabs > section {
176
- background-color: var(--vp-code-block-bg);
177
- border-radius: 8px;
178
- padding: 24px;
179
- overflow: auto;
160
+ background-color: var(--vp-code-block-bg);
161
+ border-radius: 8px;
162
+ padding: 24px;
163
+ overflow: auto;
180
164
  }
181
165
  </style>
@@ -1,14 +1,9 @@
1
1
  import { provide, inject, getCurrentInstance } from "vue";
2
2
  import type { App } from "vue";
3
3
 
4
- export type LocaleMappings = Record<
5
- string,
6
- { previewLabel: string; codeLabel: string }
7
- >;
4
+ export type LocaleMappings = Record<string, { previewLabel: string; codeLabel: string }>;
8
5
 
9
- const localeMappingSymbol = Symbol(
10
- "__vitepress_plugin_component_localeMapping",
11
- );
6
+ const localeMappingSymbol = Symbol("__vitepress_plugin_component_localeMapping");
12
7
 
13
8
  /**
14
9
  * Initialize locale mappings for the application.