vuepress-plugin-twikoo 1.0.3 → 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 +94 -37
  2. package/package.json +1 -1
package/Comment.vue CHANGED
@@ -3,67 +3,124 @@
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
- this.moveContainer()
13
- this.initTwikoo()
14
- })
11
+ this.$nextTick(async () => {
12
+ // 先初始化 Twikoo(异步)
13
+ await this.initTwikoo();
14
+ });
15
15
  }
16
16
 
17
- this.$router.afterEach((to, from) => {
18
- if (to && from && to.path === from.path) {
19
- return
20
- }
21
- // 检查是否在浏览器环境中
22
- if (typeof window !== 'undefined') {
23
- // 重新定位评论组件
24
- this.moveContainer()
25
- this.initTwikoo()
26
- }
27
- })
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
+ );
28
28
  },
29
+
29
30
  methods: {
30
31
  async initTwikoo() {
31
32
  const frontmatter = {
32
33
  to: {},
33
34
  from: {},
34
35
  ...this.$frontmatter
35
- }
36
+ };
36
37
 
37
38
  if (!this.needComment(frontmatter)) {
38
- 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;
39
47
  }
40
48
 
41
- const twikoo = await import('twikoo')
42
- twikoo.init({
49
+ // 异步加载 Twikoo 并初始化
50
+ const twikoo = await import('twikoo');
51
+ // 初始化后返回 Promise,确保 DOM 生成完成
52
+ await twikoo.init({
43
53
  ...TWIKOO_OPTIONS,
44
54
  el: TWIKOO_OPTIONS.el || '#tcomment'
45
- })
55
+ });
56
+
57
+ this.moveContainerWithRetry();
46
58
  },
47
59
 
48
60
  needComment(frontmatter) {
49
- return frontmatter.twikoo !== false
61
+ return frontmatter.twikoo !== false;
50
62
  },
51
63
 
64
+ // 基础移动逻辑(抽离成独立函数)
52
65
  moveContainer() {
53
- if (TWIKOO_CONTAINER) {
54
- const targetContainer = document.querySelector(TWIKOO_CONTAINER)
55
- if (targetContainer) {
56
- // 将评论容器移动到目标位置
57
- const tkComments = document.querySelector('.tk-comments')
58
- const el = tkComments.parentElement
59
- const commentContainer = el || document.querySelector(TWIKOO_OPTIONS.el)
60
- if (commentContainer) {
61
- targetContainer.appendChild(commentContainer)
62
- }
63
- }
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
64
75
  }
65
- }
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
+ },
66
123
 
67
124
  }
68
- }
69
- </script>
125
+ };
126
+ </script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vuepress-plugin-twikoo",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Twikoo comment plugin for vuepress 1.x",
5
5
  "main": "index.js",
6
6
  "scripts": {