.. back to home

Add Gitalk 评论组件


1. 增加模板页在主题的目录下 themes/xxx/layouts/partials/gitalk.html

 1{{ if .Site.Params.enableGitalk }}
 2<div id="gitalk-container"></div>
 3<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
 4<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
 5<script>
 6  const gitalk = new Gitalk({
 7    clientID: '{{ .Site.Params.Gitalk.clientID }}',
 8    clientSecret: '{{ .Site.Params.Gitalk.clientSecret }}',
 9    repo: '{{ .Site.Params.Gitalk.repo }}',
10    owner: '{{ .Site.Params.Gitalk.owner }}',
11    admin: ['{{ .Site.Params.Gitalk.owner }}'],
12    id: location.pathname, // Ensure uniqueness and length less than 50
13    distractionFreeMode: false // Facebook-like distraction free mode
14  });
15  (function () {
16    if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
17      document.getElementById('gitalk-container').innerHTML = 'Gitalk comments not available by default when the website is previewed locally.';
18      return;
19    }
20    gitalk.render('gitalk-container');
21  })();
22</script>
23{{ end }}

其中有一些参数

2.配置参数 config.toml

 1[params]
 2  enableGitalk = true
 3  [params.theme_config]
 4    appearance = "light"
 5    back_home_text = ".."
 6    date_format = "2006-01-02"
 7  [params.gitalk] 
 8    clientID = "QQQQQQQQQQQQQQQQ" # 您刚才创建Github Application 的 Client ID
 9    clientSecret = "QQQQQQQQQQQQQQQQ" # 您刚才创建Github Application 的 Client Secret
10    repo = "livebug.github.io" # 您的博客的github地址Repository name,例如:xxxx.github.io
11    owner = "livebug" # 您的GitHub ID
12    admin= "livebug" # 您的GitHub ID
13    id= "location.pathname" # 文章页面的链接地址就是ID
14    labels= "gitalk" # Github issue labels. If you used to use Gitment, you can change it
15    perPage= 15 # Pagination size, with maximum 100.
16    pagerDirection= "last" # Comment sorting direction, available values are 'last' and 'first'.
17    createIssueManually= true # 设置为true,如果是管理员登录,会自动创建issue,如果是false,需要管理员手动添加第一个评论(issue)
18    distractionFreeMode= false # Enable hot key (cmd|ctrl + enter) submit comment.

其中有一些 gitalk 的基础信息,需要申请配置下

3. 申请gitalk信息

首先创建一个OAuth application应用,地址 https://github.com/settings/applications/new

申请之后,选择Generate a new client secret

clientIDclientSecret 填到配置中

有个问题,这样直接填不就都暴露了嘛??

https://github.com/gitalk/gitalk/issues/150#issuecomment-402366588

这里给了回答

获取或修改 GitHub 用户数据,需要 token,为了拿到 token,除了需要 OAuth App 的 client_id 和 client_secret 外,还需要一个 Authorization Code。
这个 code 是 GitHub 登录授权完成时,在跳转回 redirect_uri 的查询参数拿到的, redirect_uri 必须是在 OAuth App 配置的 callback URL 域名下。
这样即使别人用了你的 client_id 和 client_secret ,跳转之后也拿不到 code,所以,有 client_id 和 client_secret 也做不了什么。