用twikoo实现列表评论数

This commit is contained in:
tangly1024
2023-05-07 18:30:56 +08:00
parent 74ace70a7b
commit eecac656e1
9 changed files with 156 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
import BLOG from '@/blog.config'
import React from 'react'
import twikoo from 'twikoo'
import { loadExternalResource } from '@/lib/utils'
import { useEffect } from 'react'
// import twikoo from 'twikoo'
/**
* Giscus评论 @see https://giscus.app/zh-CN
@@ -10,17 +11,48 @@ import twikoo from 'twikoo'
*/
const Twikoo = ({ isDarkMode }) => {
React.useEffect(() => {
twikoo({
envId: BLOG.COMMENT_TWIKOO_ENV_ID, // 腾讯云环境填 envIdVercel 环境填地址https://xxx.vercel.app
el: '#twikoo', // 容器元素
lang: BLOG.LANG // 用于手动设定评论区语言,支持的语言列表 https://github.com/imaegoo/twikoo/blob/main/src/client/utils/i18n/index.js
// region: 'ap-guangzhou', // 环境地域,默认为 ap-shanghai腾讯云环境填 ap-shanghai 或 ap-guangzhouVercel 环境不填
// path: location.pathname, // 用于区分不同文章的自定义 js 路径,如果您的文章路径不是 location.pathname需传此参数
})
})
const loadTwikoo = async () => {
try {
const url = await loadExternalResource(BLOG.COMMENT_TWIKOO_CDN_URL, 'js')
console.log('twikoo 加载成功', url)
const twikoo = window.twikoo
twikoo.init({
envId: BLOG.COMMENT_TWIKOO_ENV_ID, // 腾讯云环境填 envIdVercel 环境填地址https://xxx.vercel.app
el: '#twikoo', // 容器元素
lang: BLOG.LANG // 用于手动设定评论区语言,支持的语言列表 https://github.com/imaegoo/twikoo/blob/main/src/client/utils/i18n/index.js
// region: 'ap-guangzhou', // 环境地域,默认为 ap-shanghai腾讯云环境填 ap-shanghai 或 ap-guangzhouVercel 环境不填
// path: location.pathname, // 用于区分不同文章的自定义 js 路径,如果您的文章路径不是 location.pathname需传此参数
})
twikoo.getCommentsCount({
envId: BLOG.COMMENT_TWIKOO_ENV_ID, // 环境 ID
// region: 'ap-guangzhou', // 环境地域,默认为 ap-shanghai如果您的环境地域不是上海需传此参数
urls: [ // 不包含协议、域名、参数的文章路径列表,必传参数
'/article/notion-next',
'/article/notion-next-guide'
],
includeReply: false // 评论数是否包括回复默认false
}).then(function (res) {
console.log(res)
// 返回示例: [
// { url: '/2020/10/post-1.html', count: 10 },
// { url: '/2020/11/post-2.html', count: 0 },
// { url: '/2020/12/post-3.html', count: 20 }
// ]
}).catch(function (err) {
// 发生错误
console.error(err)
})
} catch (error) {
console.error('twikoo 加载失败', error)
}
}
useEffect(() => {
loadTwikoo()
}, [])
return (
<div id="twikoo"></div>
<div id="twikoo"></div>
)
}