vuepress-plugin-twikoo 1.0.4 → 1.0.6

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.
Files changed (2) hide show
  1. package/Comment.vue +64 -44
  2. package/package.json +1 -1
package/Comment.vue CHANGED
@@ -1,34 +1,30 @@
1
1
  <template>
2
- <div id="tcomment"></div>
2
+ <div></div>
3
3
  </template>
4
4
 
5
5
  <script>
6
+
6
7
  export default {
7
8
  mounted() {
8
- // 检查是否在浏览器环境中
9
+ // 页面刷新/组件挂载时,直接执行初始化+移动逻辑
9
10
  if (typeof window !== 'undefined') {
10
- // 如果指定了容器,将评论组件移动到指定位置
11
- this.$nextTick(() => {
12
- // 这里操作 DOM 更安全,能确保组件相关的 DOM 全部渲染
13
- this.moveContainer()
14
- this.initTwikoo()
15
- })
11
+ this.$nextTick(async () => {
12
+ // 先初始化 Twikoo(异步)
13
+ await this.initTwikoo();
14
+ });
16
15
  }
17
16
 
18
- this.$router.afterEach((to, from) => {
19
- if (to && from && to.path === from.path) {
20
- return
21
- }
22
- // 检查是否在浏览器环境中
23
- if (typeof window !== 'undefined') {
24
- // 重新定位评论组件
25
- this.$nextTick(() => {
26
- // 这里操作 DOM 更安全,能确保组件相关的 DOM 全部渲染
27
- this.moveContainer()
28
- this.initTwikoo()
29
- })
30
- }
31
- })
17
+ // 监听路由变化(仅处理路由跳转,刷新不触发)
18
+ this.$watch(
19
+ () => this.$route,
20
+ async (to, from) => {
21
+ if (to.path === from.path) return; // 仅 hash 变化时跳过
22
+ if (typeof window !== 'undefined') {
23
+ await this.initTwikoo();
24
+ }
25
+ },
26
+ { immediate: false }
27
+ );
32
28
  },
33
29
 
34
30
  methods: {
@@ -37,38 +33,62 @@ export default {
37
33
  to: {},
38
34
  from: {},
39
35
  ...this.$frontmatter
40
- }
36
+ };
41
37
 
42
38
  if (!this.needComment(frontmatter)) {
43
- return
39
+ return;
44
40
  }
45
41
 
46
- const twikoo = await import('twikoo')
47
- twikoo.init({
48
- ...TWIKOO_OPTIONS,
49
- el: TWIKOO_OPTIONS.el || '#tcomment'
50
- })
42
+ // 异步加载 Twikoo 并初始化
43
+ const twikoo = await import('twikoo');
44
+ // 初始化后返回 Promise,确保 DOM 生成完成
45
+ await twikoo.init({
46
+ ...TWIKOO_OPTIONS
47
+ });
48
+
49
+ this.moveContainerWithRetry();
51
50
  },
52
51
 
53
52
  needComment(frontmatter) {
54
- return frontmatter.twikoo !== false
53
+ return frontmatter.twikoo !== false;
55
54
  },
56
55
 
56
+ // 基础移动逻辑(抽离成独立函数)
57
57
  moveContainer() {
58
- if (TWIKOO_CONTAINER) {
59
- const targetContainer = document.querySelector(TWIKOO_CONTAINER)
60
- if (targetContainer) {
61
- // 将评论容器移动到目标位置
62
- const tkComments = document.querySelector('.tk-comments')
63
- const el = tkComments.parentElement
64
- const commentContainer = el || document.querySelector(TWIKOO_OPTIONS.el)
65
- if (commentContainer) {
66
- targetContainer.appendChild(commentContainer)
67
- }
68
- }
58
+ if (!TWIKOO_CONTAINER) return; // 无目标容器则直接返回
59
+
60
+ const targetContainer = document.querySelector(TWIKOO_CONTAINER);
61
+ if (!targetContainer) return; // 目标容器不存在则返回
62
+
63
+ const commentContainer = document.querySelector('#twikoo');
64
+ if (commentContainer) {
65
+ targetContainer.appendChild(commentContainer);
66
+ return true; // 移动成功,返回 true
69
67
  }
70
- }
68
+ return false;
69
+ },
70
+
71
+ // 带重试机制的移动逻辑(解决异步 DOM 生成问题)
72
+ moveContainerWithRetry(maxRetries = 5, interval = 100) {
73
+ let retries = 0;
74
+
75
+ const retry = () => {
76
+ const success = this.moveContainer();
77
+ retries++;
78
+
79
+ // 移动成功 或 达到最大重试次数,终止重试
80
+ if (success || retries >= maxRetries) {
81
+ if (!success) console.warn('Twikoo 容器移动失败:未找到 Twikoo 评论容器元素');
82
+ return;
83
+ }
84
+
85
+ // 继续重试
86
+ setTimeout(retry, interval);
87
+ };
88
+
89
+ retry();
90
+ },
71
91
 
72
92
  }
73
- }
74
- </script>
93
+ };
94
+ </script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vuepress-plugin-twikoo",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Twikoo comment plugin for vuepress 1.x",
5
5
  "main": "index.js",
6
6
  "scripts": {