vuepress-plugin-twikoo 1.0.4 → 1.0.5

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 +93 -41
  2. package/package.json +1 -1
package/Comment.vue CHANGED
@@ -3,32 +3,28 @@
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,94 @@ 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;
40
+ }
41
+
42
+ // 第一步:带重试查找 #tcomment 容器(解决 DOM 渲染慢的问题)
43
+ const tcommentEl = await this.findElementWithRetry('#tcomment', 20, 100);
44
+ if (!tcommentEl) {
45
+ console.error('多次重试后仍未找到 #tcomment 容器,Twikoo 初始化失败');
46
+ return;
44
47
  }
45
48
 
46
- const twikoo = await import('twikoo')
47
- twikoo.init({
49
+ // 异步加载 Twikoo 并初始化
50
+ const twikoo = await import('twikoo');
51
+ // 初始化后返回 Promise,确保 DOM 生成完成
52
+ await twikoo.init({
48
53
  ...TWIKOO_OPTIONS,
49
54
  el: TWIKOO_OPTIONS.el || '#tcomment'
50
- })
55
+ });
56
+
57
+ this.moveContainerWithRetry();
51
58
  },
52
59
 
53
60
  needComment(frontmatter) {
54
- return frontmatter.twikoo !== false
61
+ return frontmatter.twikoo !== false;
55
62
  },
56
63
 
64
+ // 基础移动逻辑(抽离成独立函数)
57
65
  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
- }
66
+ if (!TWIKOO_CONTAINER) return; // 无目标容器则直接返回
67
+
68
+ const targetContainer = document.querySelector(TWIKOO_CONTAINER);
69
+ if (!targetContainer) return; // 目标容器不存在则返回
70
+
71
+ const commentContainer = document.querySelector(TWIKOO_OPTIONS.el);
72
+ if (commentContainer) {
73
+ targetContainer.appendChild(commentContainer);
74
+ return true; // 移动成功,返回 true
69
75
  }
70
- }
76
+ return false;
77
+ },
78
+
79
+ // 带重试机制的移动逻辑(解决异步 DOM 生成问题)
80
+ moveContainerWithRetry(maxRetries = 5, interval = 100) {
81
+ let retries = 0;
82
+
83
+ const retry = () => {
84
+ const success = this.moveContainer();
85
+ retries++;
86
+
87
+ // 移动成功 或 达到最大重试次数,终止重试
88
+ if (success || retries >= maxRetries) {
89
+ if (!success) console.warn('Twikoo 容器移动失败:未找到 .tk-comments 元素');
90
+ return;
91
+ }
92
+
93
+ // 继续重试
94
+ setTimeout(retry, interval);
95
+ };
96
+
97
+ retry();
98
+ },
99
+
100
+ // 新增:通用的「带重试查找 DOM 元素」函数(可复用)
101
+ findElementWithRetry(selector, maxRetries = 20, interval = 100) {
102
+ return new Promise((resolve) => {
103
+ let retries = 0;
104
+
105
+ const checkElement = () => {
106
+ const el = document.querySelector(selector);
107
+ retries++;
108
+
109
+ // 找到元素 或 达到最大重试次数,终止并返回结果
110
+ if (el || retries >= maxRetries) {
111
+ resolve(el);
112
+ return;
113
+ }
114
+
115
+ // 未找到则继续重试
116
+ setTimeout(checkElement, interval);
117
+ };
118
+
119
+ // 立即执行第一次检查
120
+ checkElement();
121
+ });
122
+ },
71
123
 
72
124
  }
73
- }
74
- </script>
125
+ };
126
+ </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.5",
4
4
  "description": "Twikoo comment plugin for vuepress 1.x",
5
5
  "main": "index.js",
6
6
  "scripts": {