Giter Club home page Giter Club logo

gitment's Introduction

Gitment

NPM version

Gitment is a comment system based on GitHub Issues, which can be used in the frontend without any server-side implementation.

Demo Page

中文简介

Features

  • GitHub Login
  • Markdown / GFM support
  • Syntax highlighting
  • Notifications from GitHub
  • Easy to customize
  • No server-side implementation

Get Started

1. Install

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>

or via npm:

$ npm i --save gitment
import 'gitment/style/default.css'
import Gitment from 'gitment'

2. Register An OAuth Application

Click here to register an OAuth application, and you will get a client ID and a client secret.

Make sure the callback URL is right. Generally it's the origin of your site, like https://imsun.net.

3. Render Gitment

const gitment = new Gitment({
  id: 'Your page ID', // optional
  owner: 'Your GitHub ID',
  repo: 'The repo to store comments',
  oauth: {
    client_id: 'Your client ID',
    client_secret: 'Your client secret',
  },
  // ...
  // For more available options, check out the documentation below
})

gitment.render('comments')
// or
// gitment.render(document.getElementById('comments'))
// or
// document.body.appendChild(gitment.render())

4. Initialize Your Comments

After the page is published, you should visit your page, login with your GitHub account(make sure you're repo's owner), and click the initialize button, to create a related issue in your repo. After that, others can leave their comments.

Methods

constructor(options)

options:

Type: object

  • owner: Your GitHub ID. Required.
  • repo: The repository to store your comments. Make sure you're repo's owner. Required.
  • oauth: An object contains your client ID and client secret. Required.
    • client_id: GitHub client ID. Required.
    • client_secret: GitHub client secret. Required.
  • id: An optional string to identify your page. Default location.href.
  • title: An optional title for your page, used as issue's title. Default document.title.
  • link: An optional link for your page, used in issue's body. Default location.href.
  • desc: An optional description for your page, used in issue's body. Default ''.
  • labels: An optional array of labels your want to add when creating the issue. Default [].
  • theme: An optional Gitment theme object. Default gitment.defaultTheme.
  • perPage: An optional number to which comments will be paginated. Default 20.
  • maxCommentHeight: An optional number to limit comments' max height, over which comments will be folded. Default 250.

gitment.render([element])

element

Type: HTMLElement or string

The DOM element to which comments will be rendered. Can be an HTML element or element's id. When omitted, this function will create a new div element.

This function returns the element to which comments be rendered.

gitment.renderHeader([element])

Same like gitment.render([element]). But only renders the header.

gitment.renderComments([element])

Same like gitment.render([element]). But only renders comments list.

gitment.renderEditor([element])

Same like gitment.render([element]). But only renders the editor.

gitment.renderFooter([element])

Same like gitment.render([element]). But only renders the footer.

gitment.init()

Initialize a new page. Returns a Promise and resolves when initialized.

gitment.update()

Update data and views. Returns a Promise and resolves when data updated.

gitment.post()

Post comment in the editor. Returns a Promise and resolves when posted.

gitment.markdown(text)

text

Type: string

Returns a Promise and resolves rendered text.

gitment.login()

Jump to GitHub OAuth page to login.

gitment.logout()

Log out current user.

goto(page)

page

Type: number

Jump to the target page of comments. Notice that page starts from 1. Returns a Promise and resolves when comments loaded.

gitment.like()

Like current page. Returns a Promise and resolves when liked.

gitment.unlike()

Unlike current page. Returns a Promise and resolves when unliked.

gitment.likeAComment(commentId)

commentId

Type: string

Like a comment. Returns a Promise and resolves when liked.

gitment.unlikeAComment(commentId)

commentId

Type: string

Unlike a comment. Returns a Promise and resolves when unliked.

Customize

Gitment is easy to customize. You can use your own CSS or write a theme. (The difference is that customized CSS can't modify DOM structure)

Use Customized CSS

Gitment does't use any atomic CSS, making it easier and more flexible to customize. You can inspect the DOM structure in the browser and write your own styles.

Write A Theme

A Gitment theme is an object contains several render functions.

By default Gitment has five render functions: render, renderHeader, renderComments, renderEditor, renderFooter. The last four render independent components and render functions render them together. All of them can be used independently.

You can override any render function above or write your own render function.

For example, you can override the render function to put an editor before the comment list, and render a new component.

const myTheme = {
  render(state, instance) {
    const container = document.createElement('div')
    container.lang = "en-US"
    container.className = 'gitment-container gitment-root-container'
    
     // your custom component
    container.appendChild(instance.renderSomething(state, instance))
    
    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
  },
  renderSomething(state, instance) {
    const container = document.createElement('div')
    container.lang = "en-US"
    if (state.user.login) {
      container.innerText = `Hello, ${state.user.login}`
    }
    return container
  }
}

const gitment = new Gitment({
  // ...
  theme: myTheme,
})

gitment.render(document.body)
// or
// gitment.renderSomthing(document.body)

Each render function should receive a state object and a gitment instance, and return an HTML element. It will be wrapped attached to the Gitment instance with the same name.

Gitment uses MobX to detect states used in render functions. Once used states change, Gitment will call the render function to get a new element and render it. Unused states' changing won't affect rendered elements.

Available states:

  • user: object. User info returned from GitHub Users API with two more keys.
    • isLoggingIn: bool. Indicates if user is logging in.
    • fromCache: bool. Gitment will cache user's information. Its value indicates if current user info is from cache.
  • error: Error Object. Will be null if no error occurs.
  • meta: object. Issue's info returned from GitHub Issues API.
  • comments: array. Array of comment returned from GitHub Issue Comments API. Will be undefined when comments not loaded.
  • reactions: array. Array of reactions added to current page, returned from GitHub Issues' Reactions API.
  • commentReactions: object. Object of reactions added to comments, with comment ID as key, returned from GitHub Issue Comments' Reactions API.
  • currentPage: number. Which page of comments is user on. Starts from 1.

About Security

Is it safe to make my client secret public?

Client secret is necessary for OAuth, without which users can't login or comment with their GitHub accounts. Although GitHub does't recommend to hard code client secret in the frontend, you can still do that because GitHub will verify your callback URL. In theory, no one else can use your secret except your site.

If you find a way to hack it, please open an issue.

Why does Gitment send a request to gh-oauth.imsun.net?

https://gh-oauth.imsun.net is an simple open-source service to proxy one request during users logging in. Because GitHub doesn't attach a CORS header to it.

This service won't record or store anything. It only attaches a CORS header to that request and provides proxy. So that users can login in the frontend without any server-side implementation.

For more details, checkout this project.

gitment's People

Contributors

imsun avatar littleaprilfool avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gitment's Issues

一点小设想

是不是可以在发布文章的时候触发一个 “新建issue” 的动作,然后将这个issue页面挂到博客下面去,这样大家也都不用登陆授权了,只要登陆了github就可以评论。

实质上还是访问的github的issue页面,但是看到的就是在文章下面了。

Gitment does not support Microsoft edge.

I cannot log into my github account when I open a page with gitment embedded. Thus, I cannot 'like' or write a comment on that page. Google Chrome supports gitment very well, though.

关于申请的OAuth的权限问题

对于评论者应该不需要public_repo的权限吧,对于评论者是否是user的权限就可以了?
我比较小白,就是问一下😨

indigo 主题基于gitment添加评论

我使用的是indigo主题
在themes/indigo/_config.yml进行了如下编辑:

gitment:
owner: 29686614
repo: comments.github.io
client_id: 76c3bbcb2879d954fc73
client_secret: 68c9bb46a9599446bb9d5e41365fcffda1390616

保存后,进行hexo g和hexo d ,显示如下:
https://superbigmouse.github.io/2017/06/26/test/
提示Error: not found

comments.github.io是我新建的准备保存评论的仓库
是不是还有什么地方需要设置啊?

关于初始化评论失败的问题

初始化评论框的时候会报错,在create issues时,post的title是空串
暂时把问题定位到gitment.js的53行和156行

平台是Arch Linux,Chrome 版本 59.0.3071.109(正式版本) (64 位)
浏览器代理信息:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36

deepinscreenshot20170625115535
deepinscreenshot20170625115605

各浏览器测试结果

  • Chrome通过
  • Firefox 能正常显示评论,但login时出现[object ProgressEvent]
  • Edge 只显示评论,无法进行评论
  • IE 11完全不显示

请问是我设置问题还是本来的问题

求助,Hexo apollo 主题怎么集成。。。

我用的 apollo 主题,不知道怎么集成,

<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',
  repo: '存储评论的 repo',
  oauth: {
    client_id: '你的 client ID',
    client_secret: '你的 client secret',
  },
})
gitment.render('container')
</script>

这一段代码我需要放在哪里啊? 然后还需要做什么吗?

初始化评论框方案讨论

我不太建议使用插件或cli工具之类的. 插件的话, 博客不止hexo, Jekyll. 这样照顾不了那么多博客工具.

cli的话. 有些博客还得抓取所有连接才行. 所有我写了个简单的方案, 大家一起讨论一下.

加载部分

  1. 加载评论框[不管有没有初始化都加载][可自定义在评论上边还是下边]
  2. GET /repos/:owner/:repo/issues 利用 [标签] 列出当前文章标题的issues
  3. 判断每个issue的[body|body_html]中是否存在当前页面连接[可自定义连接]

    <a href="http://demo.com/post/1" data-url="/post/1">http://demo.com/post/1</a>
    可增加功能, 判断href和当前页面域名不一致时更新issue , 当更换域名时所有用

  4. 找到了就使用GET /repos/:owner/:repo/issues/:number/comments加载评论内容, 并记录当前评论为初始化成功
  5. 没找到就不加载评论, 也不提示没初始化什么的. 并记录当前评论为未初始化[未创建issue]

评论部分

  1. 用户评论, 判断 [初始化状态] 已初始化的直接提交评论
  2. 如果未初始化, 则先调用POST /repos/:owner/:repo/issues 创建issue[初始化], 创建成功后提交评论

评论能否按照时间倒序排列?

首先必须点赞!

但是目前最新评论貌似显示在最后,能否将最新的放在前面呢 这样似乎更科学。(当然,因为 github API 的限制也能理解哈)

建议为文档添加创建container容器的说明

在引入js和css后,按照文档执行如下代码报错:

gitment.render('comments')

抛出mobx error,targetContainer为null所以找不到它的firstChild,粗略看了下源码发现似乎没有完成js创建容器的步骤。

其实demo页面的代码有,不过大部分人一开始就是从文档看起,所以还是建议文档中添加一句说明,需要在html中加入容器:

<!-- the id in render function -->
<div id="comments"></div>

请教 如何初始化评论

我的评论框处的 Issue Page 指向 当前文章 url 下的 ./undefined,并提示 Error: Not Found
而不是指定仓库下的 issues ,

gitment貌似不支持organization下的page

我在用个人账户维护一个organization,该organization有项目主页,使用hexo和gitment。
然而gitment貌似不支持organization,我通过很丑的hack搞得可以用了,但还是应该有官方支持。

谢谢!

移动端 login 无法点击

窄屏下 “Preview“ 和 ”Login with GitHub”重叠,移动端可能需要特殊处理下样式。😀😀😀

Slowly in Palemoon (Firefox) browser

mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create1 gitment.browser.js:103:8

organization下的仓库无法使用gitment

Gitment 是一个很好用的评论插件!
但是,我在使用中发现 organization下的仓库无法使用gitment。我们有一个团队项目想要在项目主页中使用 Gitment 作为评论系统,但 owner 不能直接写团队名(没法登陆来初始化);若强行修改 js 使得团队成员能够初始化,每次刷新页面都会重新提示要初始化。
我们的网站生成器是自己写的,url 以及标题都不会发生变化,url 中存在中文(但是个人仓库并没有问题)。配置都采用了默认配置。
谢谢你!

请教:直接把代码复制到generate后的index中,deploy后,出现一点错误

default

我是直接把

<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',
  repo: '存储评论的 repo',
  oauth: {
    client_id: '你的 client ID',
    client_secret: '你的 client secret',
  },
})
gitment.render('container')
</script>

复制到我生成的public文件夹里的index.html中的,我不知道是不是这样处理??
结果就如上图所示。
不知该怎么解决。
话说我已经安装的gitment,但是并不知道怎么用,是要在markdown文件里写函数吗??

建议添加 URL 尾部参数屏蔽

在加入 Gitment 的页面进行 OAuth 后, CallBack 会在页面 URL 末端加上一个 ?code= 的 OAuth Key ,此时进行初始化评论 Issue 标签末尾也会带参数。并且从微信朋友圈打开文章末尾会带上 ?from=timeline 参数,相同页面加入不同参数获取到的评论结果不同,望支持屏蔽末尾参数。

8 m p z uqzbab a0 judp4

支持通过issue number获取issue信息

Hi, sun
非常感谢你的贡献,gitment是一个非常赞的实现。

我这里有一个小小的需求,想一起讨论下,支持类似

var gitment = new Gitment({
  number: '<%=number%>',
  owner: 'Yikun',
  repo: 'yikun.github.com',
// ... ...
}

的方式,通过issue number获取issue和comments信息

需求背景

解释下,为什么有这个需求:我使用issue作为博客的载体,之前搞过一个hexo-migrator-github-issue,把github issue导入到hexo中,这样每次我只需要在issue中编辑博客就可以了(就像这样),天然享用了github issue的图床、后台存储、分类。

gitment目前的实现,我需要指定creator和labels(比如issues?creator=imsun&labels=gitment-introduction),然而,issues number天生唯一,有了这个number,我就是在hexo-migrator-github-issue导出的插件中,直接生成github number然后传给gitment就好了。

可行性验证

修改类似这个patch:Yikun@bb24c87
我已经在我的博客上用上了,示例页面
页面请求:
1

当然,patch中有点暴力,直接把id改了,只是为了验证功能。如最开始描述的那样,我们可以兼容增加number字段:

  1. 若传入了number字段,则使用/repos/${owner}/${repo}/issues/${id}
  2. 若未传入number字段,则使用/repos/${owner}/${repo}/issues?creator=owner&labels=id

如果OK的话,我可以提个PR,或者有什么更好的方式,也可以一起讨论下。

最后,欢迎大家使用hexo-migrator-github-issue用issue写博客,然后加上gitment获取评论,完美!哈哈

认证重定向错误

目测因为url中带有中文符号()导致认证出错。
比如在以下页面登录的时候出错:

http://blog.geekaholic.cn/2017/03/06/%E5%88%B6%E4%BD%9CHexo%E4%B8%BB%E9%A2%98%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B%EF%BC%882%EF%BC%89/

错误页面的url:

http://blog.geekaholic.cn/2017/03/06/%E5%88%B6%E4%BD%9CHexo%E4%B8%BB%E9%A2%98%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B(2)/?code=b95790c4c5be5d4322d6

控制台输出404:

/2017/03/06/%E5%88%B6%E4%BD%9CHexo%E4%B8%BB%E9%A2%98%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B(2)/?code=b95790c4c5be5d4322d6:1 GET http://blog.geekaholic.cn/2017/03/06/%E5%88%B6%E4%BD%9CHexo%E4%B8%BB%E9%A2%98%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B(2)/?code=b95790c4c5be5d4322d6 404 (Not Found)

而页面的id不是默认,自己设置的id: window.location.pathname,,主要是为了考虑以后的域名更换。
以上。

Error: Comments Not Initialized

先上代码:

<% if (!index && post.comments && theme.gitment){ %>
<section id="comments">
  <div id="gitment_thread"></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 labels = "blog,gitment";
        labels = labels.split(",");
        var gitment = new Gitment({
            id: window.location.pathname,
            owner: '<%- theme.gitment.owner %>',
            repo: '<%- theme.gitment.repo %>',
            oauth: {
                client_id: '<%- theme.gitment.client_id %>',
                client_secret: '<%- theme.gitment.client_secret %>',
            },
            labels: labels
        })
        gitment.render('comments')
    </script>
</section>
<% } %>

无法初始化是什么问题?哪里配错了?

我的 ejs 应该都加对了,但是 hexo s 还是报错

我的 ejs 应该都加对了,但是 hexo s 还是报错 ( 主题是 indigo )

INFO  Start processing
ERROR Theme config load failed.
ERROR Process failed: _config.yml
    at generateError (C:\blog\node_modules\js-yaml\lib\js-yaml\loader.js:162:10)
    at throwError (C:\blog\node_modules\js-yaml\lib\js-yaml\loader.js:168:9) 
.........

是主题配置的不对吗?

# 开启
gitment:
  owner: 1464125
  repo: comments
  client_id: f851449a81bef86857ac
  client_secret: 6f3ce61b5e4f9518a13640a35773e5371a575898

添加到了 Next 主题支持

实现支持双评论(Gitment 和 Disqus),可以选择默认加载一个或两个,点击按钮后在加载等功能。

由于更改个性化东西太多,提交 Next 主题 PR 冲突太多,所以有需要的可以留言给我,我单独发送!

参考效果 https://ehlxr.me/about/

请问gitment要怎么装到hexo博客上?

我用github基于hexo建立一个博客,用的Next主题。我想用gitment作为我的评论系统,我照着文档做,但是我不知道要怎么把gitment引入,我的html是hexo自动生成的,我不知道应该把那段代码放在哪里。能不能请你给我一些帮助和提示?谢谢~

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.