Optimize changyan comment (#415)

This commit is contained in:
Liu Can 2022-01-01 03:46:02 +08:00 committed by GitHub
parent 3dca98b7cf
commit 0ed5fff721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 22 deletions

View File

@ -10,7 +10,7 @@ hexo.extend.filter.register('theme_inject', injects => {
const config = hexo.theme.config.changyan;
if (!config.enable || !config.appid || !config.appkey) return;
injects.comment.raw('changyan', '<div class="comments" id="SOHUCS"></div>', {}, { cache: true });
injects.comment.raw('changyan', '<div class="comments" id="SOHUCS" sid="{{ gitalk_md5(page.path) }}"></div>', {}, {});
injects.bodyEnd.file('changyan', path.join(hexo.theme_dir, 'layout/_third-party/comments/changyan.njk'));
@ -25,15 +25,9 @@ hexo.extend.filter.register('theme_inject', injects => {
{% if post.comments %}
<span class="post-meta-item">
${iconText('far fa-comment', 'changyan')}
{% if is_post() %}
<a title="changyan" href="{{ url_for(post.path) }}#SOHUCS" itemprop="discussionUrl">
<span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="{{ post.path }}" itemprop="commentCount"></span>
<a title="{{ post.title }}" href="{{ url_for(post.path) }}#SOHUCS" itemprop="discussionUrl">
<span id="sourceId::{{ gitalk_md5(post.path) }}" class="cy_cmt_count" itemprop="commentCount"></span>
</a>
{% else %}
<a title="changyan" href="{{ url_for(post.path) }}#SOHUCS" itemprop="discussionUrl">
<span id="url::{{ post.permalink }}" class="cy_cmt_count" data-xid="{{ post.path }}" itemprop="commentCount"></span>
</a>
{% endif %}
</span>
{% endif %}
`, {}, {});

View File

@ -1,24 +1,39 @@
/* global NexT, CONFIG */
document.addEventListener('page:loaded', () => {
if (CONFIG.page.isHome) {
NexT.utils.getScript(
`https://changyan.sohu.com/upload/plugins/plugins.list.count.js?clientId=${CONFIG.changyan.appid}`,
{ attributes: { id: 'cy_cmt_num' } }
);
} else if (CONFIG.page.comments) {
const { appid, appkey } = CONFIG.changyan;
const mainJs = 'https://cy-cdn.kuaizhan.com/upload/changyan.js';
const countJs = `https://cy-cdn.kuaizhan.com/upload/plugins/plugins.list.count.js?clientId=${appid}`;
// Get the number of comments
setTimeout(() => {
return NexT.utils.getScript(countJs, {
attributes: {
async: true,
id : 'cy_cmt_num'
}
});
}, 0);
// When scroll to comment section
if (CONFIG.page.comments && !CONFIG.page.isHome) {
NexT.utils.loadComments('#SOHUCS')
.then(() => {
delete window.changyan;
delete window.cyan;
return NexT.utils.getScript('https://changyan.sohu.com/upload/changyan.js');
return NexT.utils.getScript(mainJs, {
attributes: {
async: true
}
});
})
.then(() => {
window.changyan.api.config({
appid: CONFIG.changyan.appid,
conf : CONFIG.changyan.appkey
appid,
conf: appkey
});
})
.catch(error => {
// eslint-disable-next-line no-console
console.error('Failed to load Changyan', error);
});
NexT.utils.getScript('https://assets.changyan.sohu.com/upload/plugins/plugins.count.js');
}
});