vuepress-plugin-twikoo 1.0.2 → 1.0.4

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 (3) hide show
  1. package/Comment.vue +30 -25
  2. package/index.js +1 -1
  3. package/package.json +1 -1
package/Comment.vue CHANGED
@@ -8,19 +8,11 @@ export default {
8
8
  // 检查是否在浏览器环境中
9
9
  if (typeof window !== 'undefined') {
10
10
  // 如果指定了容器,将评论组件移动到指定位置
11
- if (TWIKOO_CONTAINER && TWIKOO_CONTAINER !== '#tcomment') {
12
- const targetContainer = document.querySelector(TWIKOO_CONTAINER)
13
- if (targetContainer) {
14
- // 将评论容器移动到目标位置(不清空目标容器,与现有内容共存)
15
- const commentContainer = document.getElementById('tcomment')
16
- if (commentContainer) {
17
- targetContainer.appendChild(commentContainer)
18
- }
19
- } else {
20
- console.warn(`Twikoo target container not found: ${TWIKOO_CONTAINER}`)
21
- }
22
- }
23
- this.initTwikoo()
11
+ this.$nextTick(() => {
12
+ // 这里操作 DOM 更安全,能确保组件相关的 DOM 全部渲染
13
+ this.moveContainer()
14
+ this.initTwikoo()
15
+ })
24
16
  }
25
17
 
26
18
  this.$router.afterEach((to, from) => {
@@ -30,19 +22,15 @@ export default {
30
22
  // 检查是否在浏览器环境中
31
23
  if (typeof window !== 'undefined') {
32
24
  // 重新定位评论组件
33
- if (TWIKOO_CONTAINER && TWIKOO_CONTAINER !== '#tcomment') {
34
- const targetContainer = document.querySelector(TWIKOO_CONTAINER)
35
- if (targetContainer) {
36
- const commentContainer = document.getElementById('tcomment')
37
- if (commentContainer && commentContainer.parentNode !== targetContainer) {
38
- targetContainer.appendChild(commentContainer)
39
- }
40
- }
41
- }
42
- this.initTwikoo()
25
+ this.$nextTick(() => {
26
+ // 这里操作 DOM 更安全,能确保组件相关的 DOM 全部渲染
27
+ this.moveContainer()
28
+ this.initTwikoo()
29
+ })
43
30
  }
44
31
  })
45
32
  },
33
+
46
34
  methods: {
47
35
  async initTwikoo() {
48
36
  const frontmatter = {
@@ -58,12 +46,29 @@ export default {
58
46
  const twikoo = await import('twikoo')
59
47
  twikoo.init({
60
48
  ...TWIKOO_OPTIONS,
61
- el: '#tcomment'
49
+ el: TWIKOO_OPTIONS.el || '#tcomment'
62
50
  })
63
51
  },
52
+
64
53
  needComment(frontmatter) {
65
- return frontmatter.comment !== false && frontmatter.comments !== false
54
+ return frontmatter.twikoo !== false
55
+ },
56
+
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
+ }
69
+ }
66
70
  }
71
+
67
72
  }
68
73
  }
69
74
  </script>
package/index.js CHANGED
@@ -7,6 +7,6 @@ module.exports = (opts, ctx) => {
7
7
  TWIKOO_CONTAINER: opts.container || 'main.page'
8
8
  },
9
9
  enhanceAppFiles: path.resolve(__dirname, 'comment.js'),
10
- globalUIComponents: opts.global !== false ? 'Comment' : undefined
10
+ globalUIComponents: 'Comment'
11
11
  }
12
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vuepress-plugin-twikoo",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Twikoo comment plugin for vuepress 1.x",
5
5
  "main": "index.js",
6
6
  "scripts": {