Skip to content

Commit

Permalink
fix: allow custom set picbed service
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 30, 2024
1 parent 3fc76ea commit e13720c
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 172 deletions.
84 changes: 42 additions & 42 deletions README.md

Large diffs are not rendered by default.

84 changes: 42 additions & 42 deletions README_zh_CN.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@
"vue-i18n": "^9.10.2",
"vue-router": "^4.3.0",
"xmlbuilder2": "^3.1.1",
"zhi-blog-api": "^1.56.4",
"zhi-blog-api": "^1.59.0",
"zhi-common": "^1.33.0",
"zhi-device": "^2.11.0",
"zhi-fetch-middleware": "^0.10.0",
"zhi-github-middleware": "^0.4.17",
"zhi-gitlab-middleware": "^0.8.0",
"zhi-lib-base": "^0.8.0",
"zhi-notion-markdown": "^0.1.4",
"zhi-siyuan-api": "^2.20.0",
"zhi-siyuan-api": "^2.20.3",
"zhi-siyuan-picgo": "1.9.0",
"zhi-xmlrpc-middleware": "^0.6.21"
}
Expand Down
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 26 additions & 63 deletions src/adaptors/base/baseExtendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
MediaObject,
PageEditMode,
PageTypeEnum,
PicbedServiceTypeEnum,
Post,
PostUtil,
TagInfo,
Expand All @@ -48,16 +49,10 @@ import { usePicgoBridge } from "~/src/composables/usePicgoBridge.ts"
import { base64ToBuffer, path, remoteImageToBase64Info, toBase64Info } from "~/src/utils/polyfillUtils.ts"
import { DateUtil, HtmlUtil, ObjectUtil, StrUtil, YamlUtil } from "zhi-common"
import { useSiyuanDevice } from "~/src/composables/useSiyuanDevice.ts"
import { isFileExists } from "~/src/utils/siyuanUtils.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { SiyuanAttr, SiyuanKernelApi } from "zhi-siyuan-api"
import { DynamicConfig, getDynPlatformKeyFromPostidKey } from "~/src/platforms/dynamicConfig.ts"
import {
CATE_AUTO_NAME,
LEGENCY_SHARED_PROXT_MIDDLEWARE,
MUST_USE_OWN_PLATFORM,
MUST_USE_PICBED_PLATFORM,
} from "~/src/utils/constants.ts"
import { CATE_AUTO_NAME, LEGENCY_SHARED_PROXT_MIDDLEWARE } from "~/src/utils/constants.ts"
import { toRaw } from "vue"
import _ from "lodash-es"
import { usePreferenceSettingStore } from "~/src/stores/usePreferenceSettingStore.ts"
Expand Down Expand Up @@ -558,52 +553,21 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
const post = _.cloneDeep(doc) as Post
this.logger.debug("图片处理之前, post =>", { post: toRaw(post) })

// 判断key包含 custom_Zhihu 或者 /custom_Zhihu-\w+/
const mustUseOwnPlatform: string[] = MUST_USE_OWN_PLATFORM
const mustUsePicbedPlatform: string[] = MUST_USE_PICBED_PLATFORM
// const isPicgoInstalled: boolean = await this.checkPicgoInstalled()
// if (!isPicgoInstalled) {
// this.logger.warn("未安装 PicGO 插件,将使用平台上传图片")
// }

let mustUseOwn = false
let mustUsePicbed = false
if (dynCfg?.platformKey) {
// 注意如果 platformKey=custom_Zhihu 或者 custom_Zhihu-xxx custom_Notion-xxx 也算 可以参考 /custom_Zhihu-\w+/
mustUseOwn = mustUseOwnPlatform.some((platform) => {
const regex = new RegExp(`${platform}(-\\w+)?`)
return regex.test(dynCfg.platformKey)
})
mustUsePicbed = mustUsePicbedPlatform.some((platform) => {
const regex = new RegExp(`${platform}(-\\w+)?`)
return regex.test(dynCfg.platformKey)
})
}

if (mustUseOwn) {
this.logger.warn("该平台不支持 Picgo 插件,将使用平台上传图片")
}
// const usePicgo: boolean = isPicgoInstalled && !mustUseOwn
const usePicgo: boolean = !mustUseOwn

if (usePicgo) {
// ==========================
// 使用 PicGO上传图片
// ==========================
// 图片替换
this.logger.info("使用 PicGO上传图片")
this.logger.debug("开始图片处理, post =>", { post: toRaw(post) })
post.markdown = await this.picgoBridge.handlePicgo(id, post.markdown)
this.logger.debug("图片处理完毕, post.markdown =>", { md: post.markdown })
} else {
if (mustUsePicbed) {
const errMsg = "检测到您未安装Picgo插件,该平台的图片将无法处理,如需使用图床功能,请在集市下载并配置Picgo插件"
this.logger.error(errMsg)
await this.kernelApi.pushMsg({
msg: errMsg,
timeout: 7000,
})
} else {
// 获取图床服务
const picbedService = cfg.picbedService
switch (picbedService) {
case PicbedServiceTypeEnum.PicGo: {
// ==========================
// 使用 PicGO上传图片
// ==========================
// 图片替换
this.logger.info("使用 PicGO上传图片")
this.logger.debug("开始图片处理, post =>", { post: toRaw(post) })
post.markdown = await this.picgoBridge.handlePicgo(id, post.markdown)
this.logger.debug("图片处理完毕, post.markdown =>", { md: post.markdown })
break
}
case PicbedServiceTypeEnum.Bundled: {
// ==========================
// 使用平台上传图片
// ==========================
Expand Down Expand Up @@ -656,6 +620,15 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
return urlMap[match] || match
}
post.markdown = post.markdown.replace(pictureReplacePattern, replaceUrl)
break
}
default: {
await this.kernelApi.pushMsg({
msg: "未指定上传图片服务,不处理图片",
timeout: 7000,
})
this.logger.warn("未指定上传图片服务,不处理图片")
break
}
}

Expand All @@ -667,16 +640,6 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
return post
}

/**
* 检查 Picgo 是否已安装
*
* @returns 一个 Promise,解析为布尔值,表示是否已安装 Picgo
*/
private async checkPicgoInstalled() {
// 检测是否安装 picgo 插件
return await isFileExists(this.kernelApi, "/data/plugins/siyuan-plugin-picgo/plugin.json", "text")
}

/**
* 读取文件并将其转换为 Base64 编码
*
Expand Down
3 changes: 3 additions & 0 deletions src/adaptors/web/csdn/useCsdnWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const useCsdnWeb = async (key?: string, newCfg?: CsdnConfig) => {
cfg.categoryType = CategoryTypeEnum.CategoryType_Multi
cfg.allowCateChange = true
cfg.knowledgeSpaceEnabled = false
// picbed service
cfg.picgoPicbedSupported = false
cfg.bundledPicbedSupported = true

const webApi = new CsdnWebAdaptor(appInstance, cfg)
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ import { SypConfig } from "~/syp.config.ts"
import { CommonBlogConfig } from "~/src/adaptors/api/base/commonBlogConfig.ts"
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common"
import { DYNAMIC_CONFIG_KEY } from "~/src/utils/constants.ts"
import { BlogAdaptor, PageTypeEnum, PasswordType, UserBlog } from "zhi-blog-api"
import { BlogAdaptor, PageTypeEnum, PasswordType, PicbedServiceTypeEnum, UserBlog } from "zhi-blog-api"
import Adaptors from "~/src/adaptors"
import { Utils } from "~/src/utils/utils.ts"
import { ElMessage } from "element-plus"
import { useProxy } from "~/src/composables/useProxy.ts"
import { usePicgoBridge } from "~/src/composables/usePicgoBridge.ts"

const logger = createAppLogger("commonblog-setting")
// appInstance
Expand All @@ -47,6 +48,7 @@ const appInstance = new PublisherAppInstance()
// uses
const { t } = useVueI18n()
const { getSetting, updateSetting } = usePublishSettingStore()
const { getPicbedServiceType } = usePicgoBridge()

const props = defineProps({
apiType: {
Expand Down Expand Up @@ -316,6 +318,9 @@ onMounted(async () => {
formData.proxy.useCorsAnywhere = !StrUtil.isEmptyString(formData.cfg.corsAnywhereUrl)
formData.proxy.useMiddleware = !StrUtil.isEmptyString(formData.cfg.middlewareUrl)

// set picbed service
formData.cfg.picbedService = await getPicbedServiceType(formData.cfg)

formData.isInit = true
})
</script>
Expand Down Expand Up @@ -435,6 +440,19 @@ onMounted(async () => {
<el-option v-for="item in formData.kwSpaces" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</el-form-item>
<el-form-item :label="t('publisher.picbed.service')">
<el-radio-group v-model="formData.cfg.picbedService" class="ml-4">
<el-radio :value="PicbedServiceTypeEnum.None" size="large">{{ t("publisher.picbed.none") }}</el-radio>
<el-radio v-if="formData.cfg.picgoPicbedSupported" :value="PicbedServiceTypeEnum.PicGo" size="large">
{{ t("publisher.picbed.picgo") }}
<sup>{{ t("publisher.picbed.recom1") }}</sup>
</el-radio>
<el-radio v-if="formData.cfg.bundledPicbedSupported" :value="PicbedServiceTypeEnum.Bundled" size="large">
{{ t("publisher.picbed.bundled") }}
<sup>{{ t("publisher.picbed.recom2") }}</sup>
</el-radio>
</el-radio-group>
</el-form-item>
<!-- 跨域代理地址 -->
<el-form-item
v-if="!formData.proxy.useSiyuanProxy && !formData.proxy.useCorsAnywhere"
Expand Down
43 changes: 43 additions & 0 deletions src/composables/usePicgoBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { createAppLogger } from "~/src/utils/appLogger.ts"
import { StrUtil } from "zhi-common"
import { isDev } from "~/src/utils/constants.ts"
import { SiyuanKernelApi } from "zhi-siyuan-api"
import { BlogConfig, PicbedServiceTypeEnum } from "zhi-blog-api"
import { isFileExists } from "~/src/utils/siyuanUtils.ts"

let needUpdate = false
const checkConfig = async (siyuanApi: SiyuanKernelApi, picgo: SiyuanPicgoPostApi) => {
Expand Down Expand Up @@ -141,9 +143,50 @@ const usePicgoBridge = () => {
return imageItemArray
}

/**
* 获取当前图床服务
*
* @param cfg
*/
const getPicbedServiceType = async (cfg: BlogConfig): Promise<PicbedServiceTypeEnum> => {
let s: PicbedServiceTypeEnum = PicbedServiceTypeEnum.None
// 如果没有选择,使用默认的
if (StrUtil.isEmptyString(cfg.picbedService)) {
// 如果安装了 PicGo 插件,优先使用 PicGo 插件
const isPicgoInstalled: boolean = await checkPicgoInstalled()
if (isPicgoInstalled && cfg.picgoPicbedSupported) {
s = PicbedServiceTypeEnum.PicGo
} else if (cfg.bundledPicbedSupported) {
// 如果支持自有的,使用自有的
s = PicbedServiceTypeEnum.Bundled
} else {
// 其他情况,不使用图床
s = PicbedServiceTypeEnum.None
}
logger.info("使用默认的图床服务")
} else {
s = cfg.picbedService
logger.info("使用自定义图床服务")
}

return s
}

/**
* 检查 Picgo 是否已安装
*
* @returns 一个 Promise,解析为布尔值,表示是否已安装 Picgo
*/
const checkPicgoInstalled = async () => {
const { kernelApi } = useSiyuanApi()
// 检测是否安装 picgo 插件
return await isFileExists(kernelApi, "/data/plugins/siyuan-plugin-picgo/plugin.json", "text")
}

return {
handlePicgo,
getImageItemsFromMd,
getPicbedServiceType,
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ export default {
"post.bind.kms.postid": "KMS ID",
"post.bind.conf.save": "Save",
"post.bind.conf.v081": "Examine the v0.8.1 metadata and import it",
"post.bind.auto.error": "No ID detected, kindly input the document root ID manually, before proceeding with the repair.",
"post.bind.auto.error":
"No ID detected, kindly input the document root ID manually, before proceeding with the repair.",
"post.bind.auto.empty": "This document has never been published, hence no repair is necessary",
"post.bind.conf.cancel": "Cancel",
"form.validate.name.required": "Please input name",
Expand Down Expand Up @@ -684,5 +685,13 @@ export default {
"platform.new.platform":
"It has been observed that there are still [count] internal platforms [platform] awaiting importation. You may utilize the platform importation tool for a bulk import, or alternatively, manually add them from the plugin store.",

"import.v081.warn": "Given that v0.8.1 is considered a significantly antiquated iteration, this feature may no longer be pertinent. Should you find yourself in need of this functionality, please direct your feedback to [email protected], and we shall deliberate on its implementation based on demand."
"import.v081.warn":
"Given that v0.8.1 is considered a significantly antiquated iteration, this feature may no longer be pertinent. Should you find yourself in need of this functionality, please direct your feedback to [email protected], and we shall deliberate on its implementation based on demand.",

"publisher.picbed.service": "Image Hosting Service",
"publisher.picbed.none": "Not in use",
"publisher.picbed.picgo": "PicGO Plugin",
"publisher.picbed.bundled": "Current Platform",
"publisher.picbed.recom1": "Strongly Recommended",
"publisher.picbed.recom2": "Recommended",
}
10 changes: 9 additions & 1 deletion src/locales/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ export default {
"setting.wordpressdotcom.home.tip": "WordPress.com 首页",
"setting.wordpressdotcom.username.tip": "WordPress.com 登录名,注意:是登录名或者邮箱,不是用户昵称哦(*^▽^*)",
"setting.wordpressdotcom.password.tip": "WordPress.com 登录密码",
"setting.wordpressdotcom.apiUrl.tip": "WordPress.com 的xmlrpc远程发布地址,通常是:https://<WordPress登录名>.wordpress.com/xmlrpc.php",
"setting.wordpressdotcom.apiUrl.tip":
"WordPress.com 的xmlrpc远程发布地址,通常是:https://<WordPress登录名>.wordpress.com/xmlrpc.php",
"setting.wordpressdotcom.previewUrl.tip": "WordPress.com 平台的文章预览规则,通常是:?p=[postid]",

"setting.oschina.home.tip": "开源中国首页,通常是:https://my.oschina.net/<您的用户名>",
Expand Down Expand Up @@ -669,4 +670,11 @@ export default {

"import.v081.warn":
"考虑到 v0.8.1 已经是很早的版本了,此功能可能不再需要。如果您需要此功能,可反馈至 [email protected] ,我们会根据需求决定是否实现此功能。",

"publisher.picbed.service": "图床服务",
"publisher.picbed.none": "不使用",
"publisher.picbed.picgo": "PicGO 插件",
"publisher.picbed.bundled": "当前平台",
"publisher.picbed.recom1": "强烈推荐",
"publisher.picbed.recom2": "推荐",
}
9 changes: 0 additions & 9 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ export const aboutUrl = "https://terwer.space/about"
*/
export const DYNAMIC_CONFIG_KEY = "dynamic-config"

/**
* 必须使用平台自带的图片上传的平台
*/
export const MUST_USE_OWN_PLATFORM = ["custom_Zhihu", "custom_Csdn", "custom_Wechat", "custom_Jianshu"]

/**
* 必须使用图床的平台
*/
export const MUST_USE_PICBED_PLATFORM = ["common_Yuque", "common_Notion", "common_Hexo", "gitlab_Gitlabhexo"]

/**
* 自动映射分类占位符
Expand Down

0 comments on commit e13720c

Please sign in to comment.