为hexo博客添加基于gitment评论功能

关于gitment

gitment其实就是利用你的代码仓库的Issues,来实现评论。每一篇文章对应该代码仓库中的
一个Issues,Issues中的评论对应你的博客每篇文章中的评论。如果你是用github的博客的话
用起来将会十分的方便。

注册github应用

首先需要在这注册一个OAuth Application, 请戳此处。在注册的过程中,你需要输入以下的东西:

Application name 随意就好

Homepage URL 你的博客地址,例如https://detectivehlh.github.io/

Application description 随意就好

Authorization callback URL 也是博客地址,例如https://detectivehlh.github.io/

输入完成之后,点击注册就OK了。成功之后就会拿到Client IDClient Secret,然后先进行一下步,暂时还不会用到这个。

修改主题配置文件

下一步就是要修改你的博客所使用的主题的配置文件。定位到# Comments,添加如下代码。

gitment:
  enable: true
  mint: true
  count: true
  lazy: false
  cleanly: false
  language:
  github_user: detectiveHLH
  github_repo: detectiveHLH.github.io
  client_id: xxx
  client_secret: xxx
  proxy_gateway:
  redirect_protocol:

将上面代码的github_user和github_repo改成你自己的就可以了。

为gitment添加样式和layout

打开你所使用的主题的目录。打开layout/_partial/comments.ejs,在原文件后加入如下代码。

<% if(theme.gitment.enable) { %>
<div id="gitment_title" class="gitment_title"></div>
<div id="container" style="display:none"></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 src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
  const myTheme = {
    render(state, instance) {
      const container = document.createElement('div');
      container.lang = "en-US";
      container.className = 'gitment-container gitment-root-container';
      container.appendChild(instance.renderHeader(state, instance));
      container.appendChild(instance.renderEditor(state, instance));
      container.appendChild(instance.renderComments(state, instance));
      container.appendChild(instance.renderFooter(state, instance));
      return container;
    }
  }

  function showGitment() {
    $("#gitment_title").attr("style", "display:none");
    $("#container").attr("style", "").addClass("gitment_container");
    var gitment = new Gitment({
      id: decodeURI(window.location.pathname),
      theme: myTheme,
      owner: 'detectiveHLH',
      repo: 'detectiveHLH.github.io',
      oauth: {
        client_id: 'xxx',
        client_secret: 'xxx'
      }
    });
    gitment.render('container');
  }

  showGitment();
</script>
<% } %>

将上面代码中的owner、repo、oauth中的信息换成你自己的就可以了,client_id和client_secret
就是第一步申请github应用得到的。我查了网上很多教程,都是需要点击按钮才能显示评论,我
做了一点改动,引用之后可以直接的显示评论。然后打开source/css/_partial/_gitment.styl,如果没有就新建文件。添加如下代码。

.gitment_title:hover {
  color: #fff;
  background: #0a9caf;
  background-image: initial;
  background-position-x: initial;
  background-position-y: initial;
  background-size: initial;
  background-repeat-x: initial;
  background-repeat-y: initial;
  background-attachment: initial;
  background-origin: initial;
  background-clip: initial;
  background-color: rgb(10, 156, 175);
}
.gitment_title {
  border: 1px solid #0a9caf;
  border-top-color: rgb(10, 156, 175);
  border-top-style: solid;
  border-top- 1px;
  border-right-color: rgb(10, 156, 175);
  border-right-style: solid;
  border-right- 1px;
  border-bottom-color: rgb(10, 156, 175);
  border-bottom-style: solid;
  border-bottom- 1px;
  border-left-color: rgb(10, 156, 175);
  border-left-style: solid;
  border-left- 1px;
  border-image-source: initial;
  border-image-slice: initial;
  border-image- initial;
  border-image-outset: initial;
  border-image-repeat: initial;
  border-radius: 4px;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  border-bottom-left-radius: 4px;
}
.gitment_title {
  display: inline-block;
  padding: 0 15px;
  padding-top: 0px;
  padding-right: 15px;
  padding-bottom: 0px;
  padding-left: 15px;
  color: #0a9caf;
  cursor: pointer;
  font-size: 14px;
}

然后打开source/css/style.styl,在原有文件后面添加如下代码,引入gitment相关的样式。

@import "partial/_gitment.styl"

结束

到此为止,更新你的博客。就可以看到评论了。

个人博客传送门 detectiveHLH
github传送门 detectiveHLH

原文地址:https://www.cnblogs.com/detectiveHLH/p/9359419.html