在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:imsun/gitment开源软件地址:https://github.com/imsun/gitment开源编程语言:JavaScript 55.0%开源软件介绍:GitmentGitment is a comment system based on GitHub Issues, which can be used in the frontend without any server-side implementation. Features
Get Started1. 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 ApplicationClick 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 Gitmentconst 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 CommentsAfter 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. Methodsconstructor(options)options:Type:
gitment.render([element])elementType: 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 This function returns the element to which comments be rendered. gitment.renderHeader([element])Same like gitment.renderComments([element])Same like gitment.renderEditor([element])Same like gitment.renderFooter([element])Same like gitment.init()Initialize a new page. Returns a gitment.update()Update data and views. Returns a gitment.post()Post comment in the editor. Returns a gitment.markdown(text)textType: Returns a gitment.login()Jump to GitHub OAuth page to login. gitment.logout()Log out current user. goto(page)pageType: Jump to the target page of comments. Notice that gitment.like()Like current page. Returns a gitment.unlike()Unlike current page. Returns a gitment.likeAComment(commentId)commentIdType: Like a comment. Returns a gitment.unlikeAComment(commentId)commentIdType: Unlike a comment. Returns a CustomizeGitment 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 CSSGitment 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 ThemeA Gitment theme is an object contains several render functions. By default Gitment has five render functions: You can override any render function above or write your own render function. For example, you can override the 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:
About SecurityIs 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. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论