Gitment is a comment system based on GitHub Issues, which can be used in the frontend without any server-side implementation.
Gitment 是一款基于 Github Issues 實現(xiàn)的評論系統(tǒng),支持在前端直接引入,不需要任何后端代碼??梢栽陧撁孢M(jìn)行登錄、查看、評論、點贊等操作,同時有完整的 Markdown / GFM 和代碼高亮支持。尤為適合各種基于 GitHub Pages 的靜態(tài)博客或項目頁面。
注冊 OAuth Application
點擊 OAuth Application 前往注冊
- Application name:github 用戶名或者 id
- Homepage URL: 個人博客的地址(如我的是 https://wyh888.github.io)
- Application description: 應(yīng)用描述,可以隨意填寫
- Authorization callback URl: 回調(diào)地址,填授權(quán)后返回的地址,一般是個人博客地址(如我的是 https://wyh888.github.io),網(wǎng)上有些教程說是填寫 https://github.com,親測不行,接入后會報這個錯 Error: Comments Not Initialized,初始化失敗
注冊成功后會得到一個 client ID 和一個 client secret,用于之后的用戶登錄。
接入 Gitment
添加下列代碼到頁面中:
<div id="container"></div>
<link rel="stylesheet" >
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
id: '<%= page.date %>', // 可選。默認(rèn)為 location.href。這個id的作用,就是針對一個文章有唯一的標(biāo)識來判斷這篇本章。如果 id 太長會導(dǎo)致初始化失敗,因此要讓 id 保證在 50 個字符內(nèi),最好是使用 page.date
owner: '', // Github 用戶名或是 Github ID
repo: '', // 存儲評論的 repo,注意這里只要填寫倉庫名就可以了,如 blog-comment,而不是 https://github.com/wyh888/blog-comment
oauth: {
client_id: '', // 上文注冊得到的 client ID
client_secret: '', // 上文注冊得到的 client secret
},
})
gitment.render('container')
</script>
初始化評論
配置正確的文章,第一次顯示時,會提示還沒有初始化,需要文章作者登陸 Github 授權(quán)后,會出現(xiàn)初始化按鈕,點擊按鈕后(注意,不要多點按鈕,否則 issues 出現(xiàn)多條一樣的),評論功能正常初始化。
關(guān)于自動初始化所有文章的功能,到本文編寫時,還沒有一個完善的解決方法,就是用腳本來執(zhí)行自動化,有需要的可以詳細(xì)了解:https://github.com/imsun/gitment/issues/5