Skip to content

Commit

Permalink
feat(artalk): Add support for using custom imgUploader
Browse files Browse the repository at this point in the history
  • Loading branch information
inkss committed May 21, 2022
1 parent a11122c commit b69c502
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 11 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,19 @@ comments:
server: https://yours-artalk-domain # 修改为自建的后端服务地址
path: # 全局评论地址
placeholder: # 评论占位
# artalk 有三类图片:1.上传至服务端 2.上传至服务端后利用upgit上传至图床 3.前端上传至图床(本配置为此类)
# 配置此项时将覆盖服务端上传能力
imgUploader:
# 以兰空图床为例,下列填写内容为:
# url: 'https://xxxxxx/api/v1/upload'
# token: 'Bearer xxxxxxxxxxxxxx'
# resp: 'data.links.url'
api: # 图床地址
token: # 图床验证
resp: # 图片地址返回值的字段
# 更多置项可在 Artalk 后端进行配置,详见 https://artalk.js.org/guide/backend/fe-control.html
# 也可以在此处继续参考上方例子进行配置,不建议重写配置的字段除上方所述外还有:
# [el, useBackendConf, pageKey, pageTitle, site, darkMode, pvEl]
# [el, useBackendConf, pageKey, pageTitle, site, darkMode, pvEl, imgUploader]

# giscus
# https://giscus.app
Expand Down
23 changes: 22 additions & 1 deletion layout/_plugins/comments/artalk/script.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
if (path.length == 0) {
path = '<%= theme.comments.artalk.path %>' || decodeURI(window.location.pathname);
}
if(!'<%= config.permalink %>'.includes('/index.html')) {
path = path.replaceAll('/index.html', '/')
}
volantis.artalk = new Artalk(Object.assign(<%- JSON.stringify(theme.comments.artalk) %>, {
el: '#artalk_container',
Expand All @@ -22,7 +25,25 @@
useBackendConf: true,
site: '<%= config.title %>',
darkMode: volantis.dark.mode === "dark",
pvEl: '#artalk_visitors'
pvEl: '#artalk_visitors',
<% if(!!theme.comments.artalk.imgUploader?.api){ %>
imgUploader: function(file) {
let headers = new Headers();
headers.set('Accept', 'application/json');
<% if(!!theme.comments.artalk.imgUploader?.token) { %>
headers.set('Authorization', '<%= theme.comments.artalk.imgUploader?.token %>')
<% } %>
let formData = new FormData();
formData.append('file', file);
return fetch('<%= theme.comments.artalk.imgUploader?.api %>',{
method: 'POST',
body: formData,
headers: headers
}).then((resp) => resp.json())
.then((resp) => resp.<%= theme.comments.artalk.imgUploader?.resp %>)
},
<% } %>
}));
volantis.artalk.then(ctx => {
Expand Down

0 comments on commit b69c502

Please sign in to comment.