Hexo博客框架下Gitment取代多说评论

作者: 仁伯 | 来源:发表于2017-06-29 20:34 被阅读3456次

前言

以往的儿童节都会给儿子说:“亲爱的儿子,不好意思,今年你爹又没追到你娘,你再等等吧,提前祝你六一快乐”。今年六一略显不同,多说正式关闭服务,心塞。曾也想转回Disqus(墙),也尝试注册了畅言(奈何要备案网站),最终种种原因不得不放弃了。
近期,不经意间看到了,imsun实现的一款基于 GitHub Issues 的评论系统Gitment,作为菜鸟的我看到后,还是蛮佩服的。

GitHub授权接入

Gitment是使用的GitHub Issues作为评论系统,在接入Gitment前,需要获得GitHub的授权,获得相应的客户端id和客户端私钥,以备站点使用。具体步骤如下图所示:
OAuth application注册接入

GitHub授权接入.png
GitHub授权接入0.png

Hexo框架Next主题下添加Gitment

如果您使用的是Hexo框架的Next主题博客,想要添加Gitment的时候,记得将自己的Next主题更新下版本,开始本人使用的是version 5.1.0的,配置后,会遇到每次打开文章,都需要重新点击Initialize comments的情况,以往评论历史数据获取也有问题。鉴于此,我更新了Next主题(当前为version 5.1.1),现在使用正常,大家可先来本人博客看看效果:仁伯安GitHub主页,(首先得谢谢DotaWang11同学的支持评论O(∩_∩)O~),如果您从事iOS工作,还请在仁伯安的GitHub给个Star。

Gitment评论框.png

** Next主题下Gitment配置 **

引自:Gitment - 使用 GitHub Issues 搭建评论系统

<div id="container"></div>
<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
  var gitment = new Gitment({
    id: '页面 ID', // 可选。默认为 location.href
    owner: '你的 GitHub ID', // 可以是你的GitHub用户名,也可以是github id
    repo: '存储评论的 github repo',
    oauth: {
      client_id: '你的 client id',
      client_secret: '你的 client secret',
    },
  })
  gitment.render('container')
</script>

在配置owner的时候,可以在浏览器中输入:https://api.github.com/users/GitHub用户名,来获取对应数据id。

GitHub id获取.png

引入Gitment需要将以上引文代码写入对应的页面即可,在Hexo框架的Next主题中配置Gitment的步骤如下:

  • 主题文件config.yml配置
    在主题下themes/next/_config.yml文件中添加:
# Gitment
 # Introduction: https://imsun.net/posts/gitment-introduction/
gitment:
  enable: true
  githubID: zonghongyan
  repo: zonghongyan.github.io
  ClientID: ------
  ClientSecret: ------
  lazy: false

其中lazy的含义,是否懒加载相应评论框,如果为true,文章底部评论是收起状态,提示显示 Gitment 评论按钮,单击展开评论框。

  • 懒加载按钮显示的文字设置:

在主题下languages/en.yml文件中添加:

 gitmentbutton: Show comments from Gitment 

在主题下languages/zh-Hans.yml文件中添加:

 gitmentbutton: 显示 Gitment 评论 

在主题下languages/zh-hk.yml文件中添加:

 gitmentbutton: 顯示 Gitment 評論 

在主题下languages/zh-tw.yml文件中添加:

 gitmentbutton: 顯示 Gitment 評論 
  • 懒加载按钮div及事件配置

在主题下layout/_partials/comments.swig文件中添加:

        <div id="lv-container" data-id="city" data-uid="{{ theme.livere_uid }}"></div>
      {% elseif theme.changyan.appid and theme.changyan.appkey %}
        <div id="SOHUCS"></div>
     {% elseif theme.gitment.enable %}
       {% if theme.gitment.lazy %}
         <div onclick="ShowGitment()" id="gitment-display-button">{{ __('gitmentbutton') }}</div>
         <div id="gitment-container" style="display:none"></div>
       {% else %}
         <div id="gitment-container"></div>
       {% endif %}
      {% endif %}
    </div>
  {% endif %}
  • 对应页面生成JS代码配置
    在主题下layout/_third-party/comments/目录下中添加文件gitment.swig:
{% if theme.gitment.enable %}
     {% set owner = theme.gitment.githubID %}
     {% set repo = theme.gitment.repo %}
     {% set cid = theme.gitment.ClientID %}
     {% set cs = theme.gitment.ClientSecret %}
     <link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
     <script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
     {% if not theme.gitment.lazy %}
         <script type="text/javascript">
             var gitment = new Gitment({
                id: document.location.href, 
                 owner: '{{owner}}',
                 repo: '{{repo}}',
                 oauth: {
                     client_id: '{{cid}}',
                     client_secret: '{{cs}}',
                 }});
             gitment.render('gitment-container');
         </script>
     {% else %}
         <script type="text/javascript">
             function ShowGitment(){
                 document.getElementById("gitment-display-button").style.display = "none";
                 document.getElementById("gitment-container").style.display = "block";
                 var gitment = new Gitment({
                     id: document.location.href, 
                     owner: '{{owner}}',
                     repo: '{{repo}}',
                     oauth: {
                         client_id: '{{cid}}',
                         client_secret: '{{cs}}',
                     }});
                 gitment.render('gitment-container');
             }
         </script>
     {% endif %}
 {% endif %}

添加gitment.swig文件后,在主题下layout/_third-party/comments/index.swig文件中引入gitment.swig文件:

{% include 'gitment.swig' %}
  • 设置CSS样式
    在主题下source/css/_common/components/third-party/目录下添加gitment.styl文件,设置button的样式:
#gitment-display-button{
     display: inline-block;
     padding: 0 15px;
     color: #0a9caf;
     cursor: pointer;
     font-size: 14px;
     border: 1px solid #0a9caf;
     border-radius: 4px;
 }
 #gitment-display-button:hover{
     color: #fff;
     background: #0a9caf;
 }

之后,在主题下 source/css/_common/components/third-party/third-party.styl文件中引入相应的CSS样式即可

@import "gitment";

详见:Add Gitment Support
文有不当之处还请见谅!!!

常见问题:

参考:

本文已在版权印备案,如需转载请在版权印获取授权。
获取版权

相关文章

网友评论

  • ilom:作者啊,我试了下,我的博客直接不能访问了,启动成功也没报错,但是访问不了网页
    紫玄: @ilom 好的👌
    ilom:@mystic_cc 也许我操作不对,你研究好了,麻烦告诉我哦:blush:
    紫玄:吓得我不敢加了:sweat:
  • 小神猫:折腾了一天,放弃了
  • Gitmoe:作者啊,我按git官网上的教程添加代码hexo g也没有报错但这个评论就是没有在文章下面现出来,遇到过这个问题吗谢谢
    仁伯:@格林小寒 注意更新最新的next主题
    Gitmoe: @仁伯安 作者啊,我已经放弃评论了。hexo这个博客系统,next主题。唉
    仁伯:你用的啥框架
  • ifelseboyxx:博主,我想问下

    <div id="container"></div>
    <link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css&quot;>
    <script src="https://imsun.github.io/gitment/dist/gitment.browser.js&quot;></script>
    <script>
    var gitment = new Gitment({
    id: '页面 ID', // 可选。默认为 location.href
    owner: '你的 GitHub ID', // 可以是你的GitHub用户名,也可以是github id
    repo: '存储评论的 github repo',
    oauth: {
    client_id: '你的 client id',
    client_secret: '你的 client secret',
    },
    })
    gitment.render('container')
    </script>

    这段代码放哪里啊? 我用的 其他的 主题
    仁伯:@Random_Speaker 如果使用的Hexo next主题的博客,按照文章步骤即可,如果其他,可去gitment作者博客https://imsun.net
    0010d068eac7:@仁伯安 怎么让它自己被引用呢?
    仁伯:引到自己需要的每个页面,https://imsun.net/posts/gitment-introduction/,这是giement作者写的博客

本文标题:Hexo博客框架下Gitment取代多说评论

本文链接:https://www.haomeiwen.com/subject/hcwycxtx.html