vitepress-plugin-giscus 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) 2020 sugar
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,93 @@
1
+ # vitepress-plugin-giscus
2
+
3
+ [English](https://github.com/ATQQ/sugar-blog/blob/master/packages/vitepress-plugin-giscus/README-en.md) | 简体中文
4
+
5
+ VitePress 评论组件插件,集成了 [Giscus](https://giscus.app/zh-CN) 评论系统,并包含了一个悬浮的评论跳转按钮(带移动端适配)。
6
+
7
+ ![示例](https://github.com/ATQQ/sugar-blog/blob/master/packages/vitepress-plugin-giscus/image.png?raw=true)
8
+ ## 安装
9
+
10
+ ```bash
11
+ pnpm add vitepress-plugin-giscus @giscus/vue
12
+ ```
13
+
14
+ ## 使用
15
+
16
+ 在 `.vitepress/config.ts` 中引入插件:
17
+
18
+ ```ts
19
+ import { defineConfig } from 'vitepress'
20
+ import { giscusPlugin } from 'vitepress-plugin-giscus'
21
+
22
+ export default defineConfig({
23
+ vite: {
24
+ plugins: [
25
+ giscusPlugin({
26
+ repo: 'your-github-username/your-repo-name',
27
+ repoId: 'your-repo-id',
28
+ category: 'Announcements',
29
+ categoryId: 'your-category-id',
30
+ mapping: 'pathname',
31
+ // ...其他 giscus 配置
32
+ })
33
+ ]
34
+ }
35
+ })
36
+ ```
37
+
38
+ ## Frontmatter 配置
39
+
40
+ 你也可以在单篇文章的 `frontmatter` 中动态覆盖或关闭评论配置:
41
+
42
+ ```yaml
43
+ ---
44
+ # 关闭评论
45
+ comment: false
46
+ ---
47
+
48
+ ---
49
+ # 覆盖评论配置
50
+ comment:
51
+ label: 交流
52
+ mobileMinify: false
53
+ ---
54
+ ```
55
+
56
+ ## 选项
57
+
58
+ 继承自 Giscus 的配置,同时包含以下插件独有配置:
59
+
60
+ | 参数 | 说明 | 类型 | 默认值 |
61
+ | --- | --- | --- | --- |
62
+ | repo | GitHub 仓库名 (格式: `user/repo`) | `string` | **必填** |
63
+ | repoId | 仓库 ID | `string` | **必填** |
64
+ | category | 讨论分类名 | `string` | - |
65
+ | categoryId | 分类 ID | `string` | - |
66
+ | mapping | 页面映射方式 | `string` | `'pathname'` |
67
+ | inputPosition | 输入框位置 | `'top' \| 'bottom'` | `'top'` |
68
+ | lang | 语言 | `string` | `'zh-CN'` |
69
+ | loading | 加载方式 | `'lazy' \| 'eager'` | `'eager'` |
70
+ | showCommentBtn | 是否显示右下角悬浮评论跳转按钮 | `boolean` | `true` |
71
+ | label | 悬浮按钮旁边的文字提示 | `string` | `'评论'` |
72
+ | mobileMinify | 移动端下是否隐藏文字提示仅显示图标 | `boolean` | `true` |
73
+ | icon | 自定义 SVG 图标代码 | `string` | - |
74
+
75
+ ## 类型定义
76
+
77
+ ```ts
78
+ export interface GiscusPluginOptions {
79
+ repo: string
80
+ repoId: string
81
+ category?: string
82
+ categoryId?: string
83
+ mapping?: string
84
+ inputPosition?: 'top' | 'bottom'
85
+ lang?: string
86
+ loading?: 'lazy' | 'eager'
87
+ mobileMinify?: boolean
88
+ label?: string
89
+ icon?: string
90
+ showCommentBtn?: boolean
91
+ [key: string]: any
92
+ }
93
+ ```
@@ -0,0 +1,164 @@
1
+ <script setup lang="ts">
2
+ import { useData, useRoute } from 'vitepress'
3
+ import { computed, nextTick, onMounted, ref, watch } from 'vue'
4
+ import Giscus from '@giscus/vue'
5
+
6
+ // @ts-expect-error
7
+ import pluginOptions from 'virtual:giscus-plugin-options'
8
+ import { useElementSize, useElementVisibility, useWindowSize } from '@vueuse/core'
9
+ import Icon from './Icon.vue'
10
+
11
+ // 监听元素变化
12
+
13
+ const { frontmatter, isDark } = useData()
14
+
15
+ const commentConfig = computed(() => {
16
+ // If explicitly disabled in frontmatter
17
+ if (frontmatter.value.comment === false) {
18
+ return false
19
+ }
20
+
21
+ // Merge frontmatter config if available, otherwise use plugin options
22
+ const fmConfig = frontmatter.value.comment
23
+ if (typeof fmConfig === 'object') {
24
+ return { ...pluginOptions, ...fmConfig }
25
+ }
26
+
27
+ return pluginOptions
28
+ })
29
+
30
+ const route = useRoute()
31
+ const showComment = ref(false)
32
+ watch(
33
+ route,
34
+ () => {
35
+ showComment.value = false
36
+ nextTick(() => {
37
+ showComment.value = true
38
+ })
39
+ },
40
+ {
41
+ immediate: true
42
+ }
43
+ )
44
+
45
+ // Wrapper logic
46
+ const commentEl = ref(null)
47
+ const commentIsVisible = useElementVisibility(commentEl)
48
+
49
+ function handleScrollToComment() {
50
+ // @ts-expect-error
51
+ commentEl.value?.scrollIntoView({
52
+ behavior: 'smooth',
53
+ block: 'start'
54
+ })
55
+ }
56
+
57
+ const { width } = useWindowSize()
58
+ const mobileMinify = computed(() => width.value < 768 && (commentConfig.value?.mobileMinify ?? true))
59
+
60
+ const $vpDoc = typeof document !== 'undefined' ? document.querySelector('.vp-doc') || document.body : null
61
+ const el = ref<any>($vpDoc)
62
+ const { width: _docWidth } = useElementSize(el)
63
+ const docWidth = computed(() => `${_docWidth.value}px`)
64
+
65
+ onMounted(() => {
66
+ const vpDoc = document.querySelector('.vp-doc')
67
+ if (vpDoc) {
68
+ el.value = vpDoc
69
+ }
70
+ })
71
+
72
+ const labelText = computed(() => {
73
+ return commentConfig.value?.label ?? '评论'
74
+ })
75
+
76
+ const showCommentBtn = computed(() => {
77
+ return commentConfig.value?.showCommentBtn ?? true
78
+ })
79
+ </script>
80
+
81
+ <template>
82
+ <div v-if="commentConfig && showComment" id="blog-comment-wrapper" ref="commentEl" class="blog-comment-wrapper" data-pagefind-ignore="all">
83
+ <Giscus
84
+ :repo="commentConfig.repo"
85
+ :repo-id="commentConfig.repoId"
86
+ :category="commentConfig.category"
87
+ :category-id="commentConfig.categoryId"
88
+ :mapping="commentConfig.mapping || 'pathname'"
89
+ reactions-enabled="1"
90
+ emit-metadata="0"
91
+ :input-position="commentConfig.inputPosition || 'top'"
92
+ :theme="isDark ? 'dark' : 'light'"
93
+ :lang="commentConfig.lang || 'zh-CN'"
94
+ :loading="commentConfig.loading || 'eager'"
95
+ />
96
+ <div v-if="showCommentBtn && _docWidth" v-show="!commentIsVisible" class="comment-btn-wrapper">
97
+ <span v-if="!mobileMinify && labelText" class="icon-wrapper-text" @click="handleScrollToComment">
98
+ <Icon :size="20" :icon="commentConfig?.icon">
99
+ <svg data-v-f0aeb853="" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 1024 1024"><path fill="currentColor" d="M736 504a56 56 0 1 1 0-112 56 56 0 0 1 0 112m-224 0a56 56 0 1 1 0-112 56 56 0 0 1 0 112m-224 0a56 56 0 1 1 0-112 56 56 0 0 1 0 112M128 128v640h192v160l224-160h352V128z" /></svg>
100
+ </Icon>
101
+ <span class="text">
102
+ {{ labelText }}
103
+ </span>
104
+ </span>
105
+ <span v-else class="icon-wrapper" @click="handleScrollToComment">
106
+ <Icon :size="20" :icon="commentConfig?.icon">
107
+ <svg data-v-f0aeb853="" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 1024 1024"><path fill="currentColor" d="M736 504a56 56 0 1 1 0-112 56 56 0 0 1 0 112m-224 0a56 56 0 1 1 0-112 56 56 0 0 1 0 112m-224 0a56 56 0 1 1 0-112 56 56 0 0 1 0 112M128 128v640h192v160l224-160h352V128z" /></svg>
108
+ </Icon>
109
+ </span>
110
+ </div>
111
+ </div>
112
+ </template>
113
+
114
+ <style scoped>
115
+ .comment-btn-wrapper {
116
+ position: fixed;
117
+ width: v-bind(docWidth);
118
+ text-align: right;
119
+ bottom: 40px;
120
+ font-size: 16px;
121
+ transition: all 0.3s ease-in-out;
122
+ opacity: 0.6;
123
+ display: flex;
124
+ justify-content: right;
125
+ z-index: 200;
126
+ }
127
+ .comment-btn-wrapper:hover {
128
+ opacity: 1;
129
+ }
130
+ .comment-btn-wrapper .icon-wrapper,
131
+ .comment-btn-wrapper .icon-wrapper-text {
132
+ cursor: pointer;
133
+ border-radius: 50%;
134
+ position: relative;
135
+ right: -80px;
136
+ background-color: var(--vp-c-bg);
137
+ box-shadow: var(--box-shadow);
138
+ padding: 4px;
139
+ display: flex;
140
+ align-items: center;
141
+ justify-content: center;
142
+ background-color: var(--vp-c-brand-soft);
143
+ color: var(--vp-c-brand-1);
144
+ }
145
+ .comment-btn-wrapper .icon-wrapper:hover,
146
+ .comment-btn-wrapper .icon-wrapper-text:hover {
147
+ box-shadow: var(--box-shadow-hover);
148
+ }
149
+ .comment-btn-wrapper .icon-wrapper-text {
150
+ border-radius: 2px;
151
+ padding: 2px 6px;
152
+ }
153
+ .comment-btn-wrapper .icon-wrapper-text span.text {
154
+ font-size: 12px;
155
+ margin-left: 4px;
156
+ }
157
+
158
+ @media screen and (max-width: 1200px) {
159
+ .comment-btn-wrapper .icon-wrapper,
160
+ .comment-btn-wrapper .icon-wrapper-text {
161
+ position: static;
162
+ }
163
+ }
164
+ </style>
@@ -0,0 +1,33 @@
1
+ <script lang="ts" setup>
2
+ import { computed } from 'vue'
3
+
4
+ const props = defineProps<{
5
+ size?: string | number
6
+ icon?: string
7
+ }>()
8
+
9
+ const size = computed(() => props.size && (typeof props.size === 'number' ? `${props.size}px` : props.size))
10
+ </script>
11
+
12
+ <template>
13
+ <i v-if="props.icon" class="sugar-theme-icon" :style="{ fontSize: size }" v-html="props.icon" />
14
+ <i v-else class="sugar-theme-icon" :style="{ fontSize: size }">
15
+ <slot />
16
+ </i>
17
+ </template>
18
+
19
+ <style lang="css" scoped>
20
+ .sugar-theme-icon {
21
+ --color: inherit;
22
+ align-items: center;
23
+ display: inline-flex;
24
+ height: 1em;
25
+ justify-content: center;
26
+ line-height: 1em;
27
+ position: relative;
28
+ width: 1em;
29
+ fill: currentColor;
30
+ color: var(--color);
31
+ font-size: inherit;
32
+ }
33
+ </style>
@@ -0,0 +1,15 @@
1
+ /* TODO:按实际情况添加不同模式的变量 */
2
+ html .demo {
3
+ --box-shadow: 0 1px 8px 0 rgba(0, 0, 0, 0.1);
4
+ }
5
+
6
+ html.dark .demo {
7
+ --box-shadow: 0 1px 8px 0 rgba(0, 0, 0, 0.6);
8
+ }
9
+
10
+ .parent-demo{
11
+ text-align: center;
12
+ }
13
+ .parent-demo h1{
14
+ color: red;
15
+ }
@@ -0,0 +1,3 @@
1
+ export declare function parseStringStyle(cssText: string): Record<string, string | number>;
2
+ export declare function useDebounceFn<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: any[]) => void;
3
+ export declare const inBrowser: boolean;
@@ -0,0 +1,23 @@
1
+ const listDelimiterRE = /;(?![^(]*\))/g;
2
+ const propertyDelimiterRE = /:([^]+)/;
3
+ const styleCommentRE = /\/\*[^]*?\*\//g;
4
+ export function parseStringStyle(cssText) {
5
+ const ret = {};
6
+ cssText.replace(styleCommentRE, '').split(listDelimiterRE).forEach((item) => {
7
+ if (item) {
8
+ const tmp = item.split(propertyDelimiterRE);
9
+ tmp.length > 1 && (ret[tmp[0].trim()] = tmp[1].trim());
10
+ }
11
+ });
12
+ return ret;
13
+ }
14
+ export function useDebounceFn(fn, delay) {
15
+ let timer;
16
+ return (...args) => {
17
+ clearTimeout(timer);
18
+ timer = setTimeout(() => {
19
+ fn(...args);
20
+ }, delay);
21
+ };
22
+ }
23
+ export const inBrowser = typeof document !== 'undefined';
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/components/util.ts"],"names":[],"mappings":"AAAA,MAAM,eAAe,GAAG,eAAe,CAAA;AACvC,MAAM,mBAAmB,GAAG,SAAS,CAAA;AACrC,MAAM,cAAc,GAAG,gBAAgB,CAAA;AAEvC,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC9C,MAAM,GAAG,GAAoC,EAAE,CAAA;IAC/C,OAAO,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC1E,IAAI,IAAI,EAAE,CAAC;YACT,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAA;YAC3C,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAA;QACxD,CAAC;IACH,CAAC,CAAC,CAAA;IACF,OAAO,GAAG,CAAA;AACZ,CAAC;AAED,MAAM,UAAU,aAAa,CAAoC,EAAK,EAAE,KAAa;IACnF,IAAI,KAAU,CAAA;IACd,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;QACxB,YAAY,CAAC,KAAK,CAAC,CAAA;QACnB,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;YACtB,EAAE,CAAC,GAAG,IAAI,CAAC,CAAA;QACb,CAAC,EAAE,KAAK,CAAC,CAAA;IACX,CAAC,CAAA;AACH,CAAC;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,OAAO,QAAQ,KAAK,WAAW,CAAA"}
@@ -0,0 +1,21 @@
1
+ import { PluginOption } from 'vite';
2
+
3
+ interface GiscusPluginOptions {
4
+ repo: string;
5
+ repoId: string;
6
+ category?: string;
7
+ categoryId?: string;
8
+ mapping?: string;
9
+ inputPosition?: 'top' | 'bottom';
10
+ lang?: string;
11
+ loading?: 'lazy' | 'eager';
12
+ mobileMinify?: boolean;
13
+ label?: string;
14
+ icon?: string;
15
+ showCommentBtn?: boolean;
16
+ [key: string]: any;
17
+ }
18
+
19
+ declare function giscusPlugin(options?: GiscusPluginOptions): PluginOption;
20
+
21
+ export { GiscusPluginOptions, giscusPlugin };
@@ -0,0 +1,21 @@
1
+ import { PluginOption } from 'vite';
2
+
3
+ interface GiscusPluginOptions {
4
+ repo: string;
5
+ repoId: string;
6
+ category?: string;
7
+ categoryId?: string;
8
+ mapping?: string;
9
+ inputPosition?: 'top' | 'bottom';
10
+ lang?: string;
11
+ loading?: 'lazy' | 'eager';
12
+ mobileMinify?: boolean;
13
+ label?: string;
14
+ icon?: string;
15
+ showCommentBtn?: boolean;
16
+ [key: string]: any;
17
+ }
18
+
19
+ declare function giscusPlugin(options?: GiscusPluginOptions): PluginOption;
20
+
21
+ export { GiscusPluginOptions, giscusPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ giscusPlugin: () => giscusPlugin
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_javascript_stringify = require("javascript-stringify");
37
+
38
+ // src/util.ts
39
+ var import_node_path = __toESM(require("path"));
40
+ var import_node_url = require("url");
41
+ var import_meta = {};
42
+ function isESM() {
43
+ return typeof __filename === "undefined" || typeof __dirname === "undefined";
44
+ }
45
+ function getDirname() {
46
+ return isESM() ? import_node_path.default.dirname((0, import_node_url.fileURLToPath)(import_meta.url)) : __dirname;
47
+ }
48
+
49
+ // src/index.ts
50
+ var componentName = "GiscusComment";
51
+ var componentFile = `${componentName}.vue`;
52
+ var aliasComponentFile = `${getDirname()}/components/${componentFile}`;
53
+ var virtualModuleId = "virtual:giscus-plugin-options";
54
+ var resolvedVirtualModuleId = `\0${virtualModuleId}`;
55
+ var slots = ["doc-after"];
56
+ function giscusPlugin(options) {
57
+ const componentOptions = {
58
+ ...options
59
+ };
60
+ const pluginOps = {
61
+ name: "vitepress-plugin-giscus",
62
+ enforce: "pre",
63
+ config: () => {
64
+ return {
65
+ resolve: {
66
+ alias: {
67
+ [`./${componentFile}`]: aliasComponentFile
68
+ }
69
+ }
70
+ };
71
+ },
72
+ transform(code, id) {
73
+ if (id.endsWith("vitepress/dist/client/theme-default/Layout.vue")) {
74
+ let transformResult = code;
75
+ for (const element of slots) {
76
+ const slotPosition = `<slot name="${element}" />`;
77
+ transformResult = transformResult.replace(slotPosition, `${slotPosition}<ClientOnly><${componentName} /></ClientOnly>`);
78
+ }
79
+ const setupPosition = '<script setup lang="ts">';
80
+ transformResult = transformResult.replace(setupPosition, `${setupPosition}
81
+ import ${componentName} from './${componentName}.vue'`);
82
+ return transformResult;
83
+ }
84
+ },
85
+ resolveId(id) {
86
+ if (id === virtualModuleId) {
87
+ return resolvedVirtualModuleId;
88
+ }
89
+ },
90
+ load(id) {
91
+ if (id === resolvedVirtualModuleId) {
92
+ return `export default ${(0, import_javascript_stringify.stringify)(componentOptions)}`;
93
+ }
94
+ }
95
+ };
96
+ return pluginOps;
97
+ }
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ giscusPlugin
101
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,65 @@
1
+ // src/index.ts
2
+ import { stringify } from "javascript-stringify";
3
+
4
+ // src/util.ts
5
+ import path from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+ function isESM() {
8
+ return typeof __filename === "undefined" || typeof __dirname === "undefined";
9
+ }
10
+ function getDirname() {
11
+ return isESM() ? path.dirname(fileURLToPath(import.meta.url)) : __dirname;
12
+ }
13
+
14
+ // src/index.ts
15
+ var componentName = "GiscusComment";
16
+ var componentFile = `${componentName}.vue`;
17
+ var aliasComponentFile = `${getDirname()}/components/${componentFile}`;
18
+ var virtualModuleId = "virtual:giscus-plugin-options";
19
+ var resolvedVirtualModuleId = `\0${virtualModuleId}`;
20
+ var slots = ["doc-after"];
21
+ function giscusPlugin(options) {
22
+ const componentOptions = {
23
+ ...options
24
+ };
25
+ const pluginOps = {
26
+ name: "vitepress-plugin-giscus",
27
+ enforce: "pre",
28
+ config: () => {
29
+ return {
30
+ resolve: {
31
+ alias: {
32
+ [`./${componentFile}`]: aliasComponentFile
33
+ }
34
+ }
35
+ };
36
+ },
37
+ transform(code, id) {
38
+ if (id.endsWith("vitepress/dist/client/theme-default/Layout.vue")) {
39
+ let transformResult = code;
40
+ for (const element of slots) {
41
+ const slotPosition = `<slot name="${element}" />`;
42
+ transformResult = transformResult.replace(slotPosition, `${slotPosition}<ClientOnly><${componentName} /></ClientOnly>`);
43
+ }
44
+ const setupPosition = '<script setup lang="ts">';
45
+ transformResult = transformResult.replace(setupPosition, `${setupPosition}
46
+ import ${componentName} from './${componentName}.vue'`);
47
+ return transformResult;
48
+ }
49
+ },
50
+ resolveId(id) {
51
+ if (id === virtualModuleId) {
52
+ return resolvedVirtualModuleId;
53
+ }
54
+ },
55
+ load(id) {
56
+ if (id === resolvedVirtualModuleId) {
57
+ return `export default ${stringify(componentOptions)}`;
58
+ }
59
+ }
60
+ };
61
+ return pluginOps;
62
+ }
63
+ export {
64
+ giscusPlugin
65
+ };
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "vitepress-plugin-giscus",
3
+ "version": "0.1.0",
4
+ "description": "vitepress plugin giscus",
5
+ "author": "sugar",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/ATQQ/sugar-blog/tree/master/packages/vitepress-plugin-giscus",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/ATQQ/sugar-blog.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/ATQQ/sugar-blog/issues"
14
+ },
15
+ "keywords": [
16
+ "vitepress",
17
+ "plugin",
18
+ "giscus",
19
+ "comment"
20
+ ],
21
+ "exports": {
22
+ ".": {
23
+ "import": "./dist/index.mjs",
24
+ "require": "./dist/index.js"
25
+ }
26
+ },
27
+ "main": "dist/index.js",
28
+ "module": "dist/index.mjs",
29
+ "types": "dist/index.d.ts",
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "peerDependencies": {
34
+ "vitepress": "^1 || ^2"
35
+ },
36
+ "dependencies": {
37
+ "@giscus/vue": "^3.1.1",
38
+ "@vueuse/core": "^14.1.0",
39
+ "javascript-stringify": "^2.1.0"
40
+ },
41
+ "devDependencies": {
42
+ "chokidar": "^3.6.0",
43
+ "fs-extra": "^11.1.1",
44
+ "tinyglobby": "^0.2.6",
45
+ "tsup": " ^7.2.0",
46
+ "typescript": "^5.5.4",
47
+ "vite": "^5",
48
+ "vitepress": "2.0.0-alpha.16",
49
+ "vue": "^3.5.24"
50
+ },
51
+ "scripts": {
52
+ "dev": "pnpm run /^dev:.*/",
53
+ "dev:plugin": "npx tsup src/index.ts --dts --watch --format esm,cjs --external vitepress",
54
+ "dev:component": "tsc --sourcemap -w --preserveWatchOutput -p src/components",
55
+ "dev:watch": "node scripts/watchAndCopy.mjs",
56
+ "build": "pnpm run /^build:.*/",
57
+ "build:plugin": "npx tsup src/index.ts --dts --format esm,cjs --external vitepress --silent",
58
+ "build:component": "tsc -p src/components && node scripts/copyComponents.mjs"
59
+ }
60
+ }