From b69c502565d8b4d6d1ad1a58fdc3fc00ac200cef Mon Sep 17 00:00:00 2001 From: inkss Date: Sat, 21 May 2022 23:09:53 +0800 Subject: [PATCH] feat(artalk): Add support for using custom imgUploader --- _config.yml | 12 ++++++++++- layout/_plugins/comments/artalk/script.ejs | 23 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/_config.yml b/_config.yml index c619f59b0..0528568d1 100755 --- a/_config.yml +++ b/_config.yml @@ -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 diff --git a/layout/_plugins/comments/artalk/script.ejs b/layout/_plugins/comments/artalk/script.ejs index 2fc7fc37b..042155a82 100644 --- a/layout/_plugins/comments/artalk/script.ejs +++ b/layout/_plugins/comments/artalk/script.ejs @@ -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', @@ -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 => {