vitepress-plugin-caniuse 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 pengzhanbo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # vitepress-plugin-caniuse
2
+
3
+ Embed [caniuse.com](https://caniuse.com/) data in your VitePress site.
4
+
5
+ 在 VitePress 中嵌入 [caniuse.com](https://caniuse.com/) 数据
6
+
7
+ ## Usage
8
+
9
+ ### With Vitepress-tuck
10
+
11
+ **Installation:**
12
+
13
+ ```bash
14
+ # npm
15
+ npm install -D vitepress-tuck vitepress-plugin-caniuse
16
+ # pnpm
17
+ pnpm add -D vitepress-tuck vitepress-plugin-caniuse
18
+ # yarn
19
+ yarn add -D vitepress-tuck vitepress-plugin-caniuse
20
+ ```
21
+
22
+ **Configuration:**
23
+
24
+ ```ts
25
+ // .vitepress/config.ts
26
+ import caniuse from 'vitepress-plugin-caniuse'
27
+ import { defineConfig } from 'vitepress-tuck'
28
+
29
+ export default defineConfig({
30
+ plugins: [caniuse()],
31
+ })
32
+ ```
33
+
34
+ ```ts
35
+ // .vitepress/theme/index.ts
36
+ import type { Theme } from 'vitepress'
37
+ import enhanceApp from 'virtual:enhance-app'
38
+ import DefaultTheme from 'vitepress/theme'
39
+
40
+ export default {
41
+ extends: DefaultTheme,
42
+ enhanceApp(ctx) {
43
+ enhanceApp(ctx)
44
+ },
45
+ } satisfies Theme
46
+ ```
47
+
48
+ ### With Vitepress
49
+
50
+ **Installation:**
51
+
52
+ ```bash
53
+ # npm
54
+ npm install -D vitepress-plugin-caniuse
55
+ # pnpm
56
+ pnpm add -D vitepress-plugin-caniuse
57
+ # yarn
58
+ yarn add -D vitepress-plugin-caniuse
59
+ ```
60
+
61
+ **Configuration:**
62
+
63
+ ```ts
64
+ // .vitepress/config.ts
65
+ import { defineConfig } from 'vitepress'
66
+ import { caniuseMarkdownPlugin } from 'vitepress-plugin-caniuse'
67
+
68
+ export default defineConfig({
69
+ markdown: {
70
+ config: (md) => {
71
+ md.use(caniuseMarkdownPlugin)
72
+ },
73
+ },
74
+ vite: {
75
+ ssr: {
76
+ noExternal: ['vitepress-plugin-caniuse'],
77
+ },
78
+ },
79
+ })
80
+ ```
81
+
82
+ ```ts
83
+ // .vitepress/theme/index.ts
84
+ import type { Theme } from 'vitepress'
85
+ import { enhanceAppWithCaniuse } from 'vitepress-plugin-caniuse/client'
86
+ import DefaultTheme from 'vitepress/theme'
87
+
88
+ export default {
89
+ extends: DefaultTheme,
90
+ enhanceApp(ctx) {
91
+ enhanceAppWithCaniuse(ctx)
92
+ },
93
+ } satisfies Theme
94
+ ```
95
+
96
+ ## Syntax
97
+
98
+ ```md
99
+ @[caniuse](feature_name)
100
+ ```
101
+
102
+ ## Example
103
+
104
+ ```md
105
+ @[caniuse](css-grid)
106
+ ```
107
+
108
+ You can view all available `feature_name` from <https://caniuse-embed.vercel.app> or <https://caniuse.com/>
@@ -0,0 +1,19 @@
1
+ import { EnhanceAppContext } from "vitepress/client";
2
+
3
+ //#region src/client/VPCaniuse.vue.d.ts
4
+ type __VLS_Props = {
5
+ feature: string;
6
+ past?: number;
7
+ future?: number;
8
+ meta?: string;
9
+ baseline?: boolean;
10
+ };
11
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
12
+ declare const _default: typeof __VLS_export;
13
+ //#endregion
14
+ //#region src/client/index.d.ts
15
+ declare function enhanceAppWithCaniuse({
16
+ app
17
+ }: EnhanceAppContext): void;
18
+ //#endregion
19
+ export { _default as VPCaniuse, enhanceAppWithCaniuse };
@@ -0,0 +1,53 @@
1
+ import "../style.css";
2
+ import { computed, createElementBlock, createElementVNode, defineComponent, normalizeClass, normalizeStyle, openBlock, ref } from "vue";
3
+ import { useEventListener } from "@vueuse/core";
4
+ import { useData } from "vitepress/client";
5
+ //#region src/client/VPCaniuse.vue
6
+ const _hoisted_1 = ["src", "title"];
7
+ const url = "https://caniuse.pengzhanbo.cn/";
8
+ const _sfc_main = /* @__PURE__ */ defineComponent({
9
+ __name: "VPCaniuse",
10
+ props: {
11
+ feature: {},
12
+ past: { default: 2 },
13
+ future: { default: 1 },
14
+ meta: { default: "" },
15
+ baseline: {
16
+ type: Boolean,
17
+ default: false
18
+ }
19
+ },
20
+ setup(__props) {
21
+ const { isDark } = useData();
22
+ const height = ref(__props.baseline ? "150px" : "350px");
23
+ const source = computed(() => {
24
+ return `${url}${__props.feature}${__props.baseline ? "/baseline#" : `#past=${__props.past}&future=${__props.future}&`}meta=${__props.meta}&theme=${isDark.value ? "dark" : "light"}`;
25
+ });
26
+ useEventListener("message", (event) => {
27
+ const { type, payload } = parseData(event.data);
28
+ if (type === "ciu-embed" && payload && payload.feature === __props.feature && payload.meta === __props.meta) height.value = `${Math.ceil(payload.height)}px`;
29
+ });
30
+ function parseData(data) {
31
+ if (typeof data === "string") try {
32
+ return JSON.parse(data);
33
+ } catch {
34
+ return { type: "" };
35
+ }
36
+ return data;
37
+ }
38
+ return (_ctx, _cache) => {
39
+ return openBlock(), createElementBlock("div", { class: normalizeClass(["vp-caniuse ciu_embed", { baseline: __props.baseline }]) }, [createElementVNode("iframe", {
40
+ src: source.value,
41
+ style: normalizeStyle({ height: height.value }),
42
+ title: `Can I use ${__props.feature}`
43
+ }, null, 12, _hoisted_1)], 2);
44
+ };
45
+ }
46
+ });
47
+ //#endregion
48
+ //#region src/client/index.ts
49
+ function enhanceAppWithCaniuse({ app }) {
50
+ app.component("VPCaniuse", _sfc_main);
51
+ }
52
+ //#endregion
53
+ export { _sfc_main as VPCaniuse, enhanceAppWithCaniuse };
@@ -0,0 +1,19 @@
1
+ import { EnhanceAppContext } from "vitepress/client";
2
+
3
+ //#region src/client/VPCaniuse.vue.d.ts
4
+ type __VLS_Props = {
5
+ feature: string;
6
+ past?: number;
7
+ future?: number;
8
+ meta?: string;
9
+ baseline?: boolean;
10
+ };
11
+ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
12
+ declare const _default: typeof __VLS_export;
13
+ //#endregion
14
+ //#region src/client/index.d.ts
15
+ declare function enhanceAppWithCaniuse({
16
+ app
17
+ }: EnhanceAppContext): void;
18
+ //#endregion
19
+ export { _default as VPCaniuse, enhanceAppWithCaniuse };
@@ -0,0 +1,55 @@
1
+ import { computed, defineComponent, mergeProps, ref, useSSRContext } from "vue";
2
+ import { ssrRenderAttr, ssrRenderAttrs, ssrRenderStyle } from "vue/server-renderer";
3
+ import { useEventListener } from "@vueuse/core";
4
+ import { useData } from "vitepress/client";
5
+ //#region src/client/VPCaniuse.vue
6
+ const url = "https://caniuse.pengzhanbo.cn/";
7
+ const _sfc_main = /* @__PURE__ */ defineComponent({
8
+ __name: "VPCaniuse",
9
+ __ssrInlineRender: true,
10
+ props: {
11
+ feature: {},
12
+ past: { default: 2 },
13
+ future: { default: 1 },
14
+ meta: { default: "" },
15
+ baseline: {
16
+ type: Boolean,
17
+ default: false
18
+ }
19
+ },
20
+ setup(__props) {
21
+ const { isDark } = useData();
22
+ const height = ref(__props.baseline ? "150px" : "350px");
23
+ const source = computed(() => {
24
+ return `${url}${__props.feature}${__props.baseline ? "/baseline#" : `#past=${__props.past}&future=${__props.future}&`}meta=${__props.meta}&theme=${isDark.value ? "dark" : "light"}`;
25
+ });
26
+ useEventListener("message", (event) => {
27
+ const { type, payload } = parseData(event.data);
28
+ if (type === "ciu-embed" && payload && payload.feature === __props.feature && payload.meta === __props.meta) height.value = `${Math.ceil(payload.height)}px`;
29
+ });
30
+ function parseData(data) {
31
+ if (typeof data === "string") try {
32
+ return JSON.parse(data);
33
+ } catch {
34
+ return { type: "" };
35
+ }
36
+ return data;
37
+ }
38
+ return (_ctx, _push, _parent, _attrs) => {
39
+ _push(`<div${ssrRenderAttrs(mergeProps({ class: ["vp-caniuse ciu_embed", { baseline: __props.baseline }] }, _attrs))}><iframe${ssrRenderAttr("src", source.value)} style="${ssrRenderStyle({ height: height.value })}"${ssrRenderAttr("title", `Can I use ${__props.feature}`)}></iframe></div>`);
40
+ };
41
+ }
42
+ });
43
+ const _sfc_setup = _sfc_main.setup;
44
+ _sfc_main.setup = (props, ctx) => {
45
+ const ssrContext = useSSRContext();
46
+ (ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("src/client/VPCaniuse.vue");
47
+ return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
48
+ };
49
+ //#endregion
50
+ //#region src/client/index.ts
51
+ function enhanceAppWithCaniuse({ app }) {
52
+ app.component("VPCaniuse", _sfc_main);
53
+ }
54
+ //#endregion
55
+ export { _sfc_main as VPCaniuse, enhanceAppWithCaniuse };
@@ -0,0 +1,19 @@
1
+ .vp-caniuse {
2
+ margin: 16px -24px;
3
+ }
4
+
5
+ .vp-caniuse.baseline {
6
+ overflow: hidden;
7
+ border-radius: 8px;
8
+ }
9
+
10
+ .vp-caniuse iframe {
11
+ width: 100%;
12
+ border: none;
13
+ }
14
+
15
+ @media (min-width: 768px) {
16
+ .vp-caniuse {
17
+ margin: 16px 0;
18
+ }
19
+ }
@@ -0,0 +1,35 @@
1
+ import { PluginSimple } from "markdown-it";
2
+
3
+ //#region src/node/caniusePlugin.d.ts
4
+ /**
5
+ * @example
6
+ * `.vitepress/config.ts`
7
+ * ```ts
8
+ * import { definePlugin } from 'vitepress'
9
+ * import { caniuseMarkdownPlugin } from 'vitepress-plugin-caniuse'
10
+ * export default defineConfig({
11
+ * markdown: {
12
+ * config: (md) => {
13
+ * md.use(caniuseMarkdownPlugin)
14
+ * },
15
+ * },
16
+ * })
17
+ * ```
18
+ */
19
+ declare const caniuseMarkdownPlugin: PluginSimple;
20
+ //#endregion
21
+ //#region src/node/index.d.ts
22
+ /**
23
+ * @example
24
+ * `.vitepress/config.ts`
25
+ * ```ts
26
+ * import { definePlugin } from 'vitepress-tuck'
27
+ * import caniuse from 'vitepress-plugin-caniuse'
28
+ * export default defineConfig({
29
+ * plugins: [caniuse()],
30
+ * })
31
+ * ```
32
+ */
33
+ declare const _default: (option?: unknown) => import("vitepress-tuck").VitepressPlugin;
34
+ //#endregion
35
+ export { caniuseMarkdownPlugin, _default as default };
@@ -0,0 +1,81 @@
1
+ import { definePlugin } from "vitepress-tuck";
2
+ import { createEmbedRuleBlock, stringifyAttrs } from "vitepress-plugin-toolkit";
3
+ //#region src/node/caniusePlugin.ts
4
+ /**
5
+ * @example
6
+ * `.vitepress/config.ts`
7
+ * ```ts
8
+ * import { definePlugin } from 'vitepress'
9
+ * import { caniuseMarkdownPlugin } from 'vitepress-plugin-caniuse'
10
+ * export default defineConfig({
11
+ * markdown: {
12
+ * config: (md) => {
13
+ * md.use(caniuseMarkdownPlugin)
14
+ * },
15
+ * },
16
+ * })
17
+ * ```
18
+ */
19
+ const caniuseMarkdownPlugin = (md) => {
20
+ createEmbedRuleBlock(md, {
21
+ type: "caniuse",
22
+ syntaxPattern: /^@\[caniuse\s*(embed|baseline)?(?:\{([0-9,\-]*)\})?\]\(([^)]*)\)/,
23
+ meta: ([, mode, versions = "", feature = ""]) => ({
24
+ feature,
25
+ mode: mode || "embed",
26
+ versions
27
+ }),
28
+ content: resolveCanIUse
29
+ });
30
+ };
31
+ const UNDERLINE_RE = /_+/g;
32
+ let uuid = 0;
33
+ function resolveCanIUse({ feature, mode, versions }) {
34
+ if (!feature) return "";
35
+ feature = feature.replace(UNDERLINE_RE, "_");
36
+ const { past, future } = resolveVersions(versions);
37
+ const meta = `${feature}-${uuid++}`;
38
+ return `<VPCaniuse${stringifyAttrs({
39
+ feature,
40
+ meta,
41
+ past,
42
+ future,
43
+ baseline: mode === "baseline"
44
+ })} />`;
45
+ }
46
+ function resolveVersions(versions) {
47
+ if (!versions) return {
48
+ past: 5,
49
+ future: 3
50
+ };
51
+ const list = versions.split(",").map((v) => Number(v.trim())).filter((v) => !Number.isNaN(v) && v >= -5 && v <= 3);
52
+ list.push(0);
53
+ const uniq = [...new Set(list)].sort((a, b) => b - a);
54
+ return {
55
+ future: uniq[0],
56
+ past: Math.abs(uniq[uniq.length - 1])
57
+ };
58
+ }
59
+ //#endregion
60
+ //#region src/node/index.ts
61
+ /**
62
+ * @example
63
+ * `.vitepress/config.ts`
64
+ * ```ts
65
+ * import { definePlugin } from 'vitepress-tuck'
66
+ * import caniuse from 'vitepress-plugin-caniuse'
67
+ * export default defineConfig({
68
+ * plugins: [caniuse()],
69
+ * })
70
+ * ```
71
+ */
72
+ var node_default = definePlugin(() => ({
73
+ name: "vitepress-plugin-caniuse",
74
+ client: { enhance: "enhanceAppWithCaniuse" },
75
+ markdown: { config: (md) => {
76
+ md.use(caniuseMarkdownPlugin);
77
+ } },
78
+ vite: { ssr: { noExternal: ["vitepress-plugin-caniuse"] } }
79
+ }));
80
+ //#endregion
81
+ export { caniuseMarkdownPlugin, node_default as default };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "vitepress-plugin-caniuse",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "description": "Embed caniuse.com data in your VitePress site",
6
+ "author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo/)",
7
+ "license": "MIT",
8
+ "keywords": [
9
+ "vitepress",
10
+ "vitepress-plugin",
11
+ "caniuse",
12
+ "browser-compatibility"
13
+ ],
14
+ "exports": {
15
+ ".": "./dist/node/index.js",
16
+ "./client": {
17
+ "browser": "./dist/client/browser/index.js",
18
+ "default": "./dist/client/ssr/index.js"
19
+ },
20
+ "./style.css": "./dist/client/style.css"
21
+ },
22
+ "module": "./dist/node/index.js",
23
+ "types": "./dist/node/index.d.ts",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "peerDependencies": {
28
+ "vitepress": "^1.6.4 || ^2.0.0-alpha.17",
29
+ "vue": "^3.5.0"
30
+ },
31
+ "dependencies": {
32
+ "@vueuse/core": "^14.3.0",
33
+ "vitepress-plugin-toolkit": "0.1.0",
34
+ "vitepress-tuck": "0.1.0"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "clean": "rimraf --glob ./dist",
41
+ "dev": "pnpm '/(tsdown|copy):watch/'",
42
+ "build": "pnpm tsdown && pnpm copy",
43
+ "copy": "cpx \"src/**/*.css\" dist",
44
+ "copy:watch": "pnpm copy -w",
45
+ "tsdown": "tsdown --config-loader unrun",
46
+ "tsdown:watch": "pnpm tsdown -w"
47
+ }
48
+ }