Skip to content

Commit

Permalink
feat: 新版挂件-适配WordPress配置
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 28, 2023
1 parent 997a44e commit a780b81
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 25 deletions.
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare module '@vue/runtime-core' {
SiyuanTest: typeof import('./src/components/test/SiyuanTest.vue')['default']
TypechoTest: typeof import('./src/components/test/TypechoTest.vue')['default']
VitepressTest: typeof import('./src/components/test/VitepressTest.vue')['default']
WordpressSetting: typeof import('./src/components/set/publish/singleplatform/metaweblog/WordpressSetting.vue')['default']
WordpressTest: typeof import('./src/components/test/WordpressTest.vue')['default']
YuqueTest: typeof import('./src/components/test/YuqueTest.vue')['default']
ZhihuTest: typeof import('./src/components/test/ZhihuTest.vue')['default']
Expand Down
5 changes: 5 additions & 0 deletions src/adaptors/api/base/metaweblog/config/MetaweblogConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export class MetaweblogConfig extends BlogConfig {
*/
public override placeholder = {} as MetaweblogPlaceholder

/**
* 跨域请求代理
*/
public override middlewareUrl = ""

constructor(home: string, apiUrl: string, username: string, password: string, middlewareUrl?: string) {
super()
this.home = home
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/api/cnblogs/config/cnblogsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CnblogsConfig extends MetaweblogConfig {
* @param middlewareUrl 代理地址
*/
constructor(apiUrl: string, username: string, password: string, middlewareUrl?: string) {
super("https://www.cnblogs.com/[yourname]", apiUrl, username, password, middlewareUrl)
super("https://www.cnblogs.com/[your-blog-name]", apiUrl, username, password, middlewareUrl)
this.previewUrl = "/p/[postid].html"
this.pageType = PageTypeEnum.Markdown
}
Expand Down
20 changes: 12 additions & 8 deletions src/adaptors/api/wordpress/config/wordpressConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
* questions.
*/

import { BlogConfig } from "zhi-blog-api"
import { MetaweblogConfig } from "~/src/adaptors/api/base/metaweblog/config/MetaweblogConfig.ts"
import { PageTypeEnum } from "zhi-blog-api"
import WordpressUtils from "~/src/adaptors/api/wordpress/wordpressUtils.ts"

/**
* WordPress 配置
*
* @author terwer
* @since 1.0.0
*/
class WordpressConfig extends BlogConfig {
class WordpressConfig extends MetaweblogConfig {
/**
* API 地址
*/
Expand All @@ -55,17 +57,19 @@ class WordpressConfig extends BlogConfig {
/**
* WordPress 配置项
*
* @param apiUrl API 地址
* @param homeAddr WordPress 主页
* @param username 用户名
* @param password 密码
* @param middlewareUrl 代理地址
*/
constructor(apiUrl: string, username: string, password: string, middlewareUrl?: string) {
super()
constructor(homeAddr: string, username: string, password: string, middlewareUrl?: string) {
super(homeAddr, "", username, password, middlewareUrl)

const { home, apiUrl } = WordpressUtils.parseHomeAndUrl(homeAddr)
this.home = home
this.apiUrl = apiUrl
this.username = username
this.password = password
this.middlewareUrl = middlewareUrl
this.previewUrl = "/?p=[postid]"
this.pageType = PageTypeEnum.Markdown
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/adaptors/api/wordpress/config/wordpressPlaceholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
* questions.
*/

import { BlogPlaceholder } from "zhi-blog-api"
import { MetaweblogPlaceholder } from "~/src/adaptors/api/base/metaweblog/config/MetaweblogPlaceholder.ts"

/**
* WordPress 操作提示
*/
class WordpressPlaceholder extends BlogPlaceholder {}
class WordpressPlaceholder extends MetaweblogPlaceholder {}

export { WordpressPlaceholder }
56 changes: 56 additions & 0 deletions src/adaptors/api/wordpress/wordpressUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { createAppLogger } from "~/src/utils/appLogger.ts"

/**
* 用于处理WordPress相关操作的实用工具类
*/
class WordpressUtils {
private static logger = createAppLogger("wordpress-utils")

/**
* 解析给定的主页地址并生成相应的apiUrl地址
*
* @param home - 主页地址
*/
public static parseHomeAndUrl(home: string): { home: string; apiUrl: string } {
this.logger.debug(`Parsing Home address: ${home}`)
// 解析主页地址
let apiUrl = ""
if (home.endsWith("/xmlrpc.php")) {
apiUrl = home
home = home.replace("/xmlrpc.php", "")
} else {
home = home.replace(/\/$/, "")
apiUrl = `${home}/xmlrpc.php`
}

this.logger.debug(`Parse result: home=${home}, apiUrl=${apiUrl}`)
return { home, apiUrl }
}
}

export default WordpressUtils
6 changes: 6 additions & 0 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useSiyuanApi } from "~/src/composables/api/useSiyuanApi.ts"
import { getSubPlatformTypeByKey, SubPlatformType } from "~/src/components/set/publish/platform/dynamicConfig.ts"
import { useCnblogsApi } from "~/src/composables/api/useCnblogsApi.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import {useWordpressApi} from "~/src/composables/api/useWordpressApi.ts";

/**
* 适配器统一入口
Expand All @@ -53,6 +54,11 @@ class Adaptors {
blogAdaptor = blogApi
break
}
case SubPlatformType.Wordpress_Wordpress:{
const { blogApi } = await useWordpressApi(key)
blogAdaptor = blogApi
break
}
default: {
break
}
Expand Down
5 changes: 1 addition & 4 deletions src/components/set/PublishSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ const handleSinglePlatformDelete = (cfg: DynamicConfig) => {
formData.setting[DYNAMIC_CONFIG_KEY] = JSON.stringify(dynJsonCfg)
// 删除配置
delete formData.setting[cfg.platformKey]
// 删除文章key
const postidKey = getDynPostidKey(cfg.platformKey)
delete formData.setting[postidKey]
await updateSetting(formData.setting)
// 重新加载列表
Expand Down Expand Up @@ -240,7 +237,7 @@ onMounted(async () => {
platform.isAuth ? '已授权' : platform.authMode === AuthMode.API ? '设置无效' : '没有授权'
"
class="badge-item"
:type="platform.isAuth ? 'success' : 'error'"
:type="platform.isAuth ? 'success' : 'danger'"
>
<span>{{ platform.platformName }}</span>
<span class="name-edit" @click="handleChangePlatformDefine(platform)">
Expand Down
24 changes: 23 additions & 1 deletion src/components/set/publish/singleplatform/MetaweblogSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const props = defineProps({
},
})
const emit = defineEmits(["onHomeChange"])
const apiTypeInfo = ref(t("setting.blog.platform.support.metaweblog") + props.apiType + " ")
const isLoading = ref(false)
Expand Down Expand Up @@ -125,6 +127,12 @@ const saveConf = async (hideTip?: any) => {
}
}
const handleHomeChange = (value: string | number): void => {
if (emit) {
emit("onHomeChange", value, formData.cfg)
}
}
const initConf = async () => {
formData.setting = await getSetting()
const dynJsonCfg = JsonUtil.safeParse<DynamicJsonCfg>(formData.setting[DYNAMIC_CONFIG_KEY], {} as DynamicJsonCfg)
Expand Down Expand Up @@ -156,7 +164,11 @@ onMounted(async () => {
<el-alert :closable="false" :title="apiTypeInfo + formData.cfg.blogName" class="top-tip" type="info" />
<!-- 首页 -->
<el-form-item :label="t('setting.blog.url')">
<el-input v-model="formData.cfg.home" :placeholder="props.cfg?.placeholder.homePlaceholder" />
<el-input
v-model="formData.cfg.home"
:placeholder="props.cfg?.placeholder.homePlaceholder"
@input="handleHomeChange"
/>
</el-form-item>
<!-- API 地址 -->
<el-form-item :label="t('setting.blog.apiurl')">
Expand Down Expand Up @@ -186,6 +198,16 @@ onMounted(async () => {
<el-radio :label="PageTypeEnum.Kramdown" size="large">Kramdown</el-radio>
</el-radio-group>
</el-form-item>
<!-- 跨域代理地址 -->
<el-form-item :label="t('setting.blog.middlewareUrl')">
<el-input v-model="formData.cfg.middlewareUrl" :placeholder="t('setting.blog.middlewareUrl.tip')" />
<el-alert
:closable="false"
:title="t('setting.blog.middlewareUrl.my.tip')"
class="top-tip"
type="warning"
></el-alert>
</el-form-item>
<!-- 校验 -->
<el-form-item>
<el-button type="primary" :loading="isLoading" @click="valiConf">
Expand Down
2 changes: 2 additions & 0 deletions src/components/set/publish/singleplatform/SettingEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useRoute } from "vue-router"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { getSubPlatformTypeByKey, SubPlatformType } from "~/src/components/set/publish/platform/dynamicConfig.ts"
import CnblogsSetting from "~/src/components/set/publish/singleplatform/metaweblog/CnblogsSetting.vue"
import WordpressSetting from "~/src/components/set/publish/singleplatform/metaweblog/WordpressSetting.vue"
// uses
const { t } = useVueI18n()
Expand All @@ -44,6 +45,7 @@ const subtype = getSubPlatformTypeByKey(apiType)
<template>
<back-page :title="t('setting.entry.title') + apiType">
<cnblogs-setting v-if="subtype === SubPlatformType.Metaweblog_Cnblogs" :api-type="apiType" />
<wordpress-setting v-else-if="subtype === SubPlatformType.Wordpress_Wordpress" :api-type="apiType" />
<span v-else>
<el-alert :closable="false" :title="t('setting.entry.not.supported')" class="top-tip" type="error" />
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { useCnblogsApi } from "~/src/composables/api/useCnblogsApi.ts"
import MetaweblogSetting from "~/src/components/set/publish/singleplatform/MetaweblogSetting.vue"
import { MetaweblogPlaceholder } from "~/src/adaptors/api/base/metaweblog/config/MetaweblogPlaceholder.ts"
import { CnblogsPlaceholder } from "~/src/adaptors/api/cnblogs/config/cnblogsPlaceholder.ts"
import { CnblogsConfig } from "~/src/adaptors/api/cnblogs/config/cnblogsConfig.ts"
import { MetaweblogConfig } from "~/src/adaptors/api/base/metaweblog/config/MetaweblogConfig.ts"
const props = defineProps({
apiType: {
Expand All @@ -40,8 +39,8 @@ const props = defineProps({
const { t } = useVueI18n()
const { cfg } = await useCnblogsApi(props.apiType)
const cnblogsCfg = cfg as MetaweblogConfig
const cnblogsPlaceholder = new MetaweblogPlaceholder()
const cnblogsCfg = cfg as CnblogsConfig
const cnblogsPlaceholder = new CnblogsPlaceholder()
cnblogsPlaceholder.homePlaceholder = t("setting.cnblogs.home.tip")
cnblogsPlaceholder.usernamePlaceholder = t("setting.cnblogs.username.tip")
cnblogsPlaceholder.passwordPlaceholder = t("setting.cnblogs.password.tip")
Expand All @@ -51,5 +50,5 @@ cnblogsCfg.placeholder = cnblogsPlaceholder
</script>

<template>
<metaweblog-setting :api-type="props.apiType" :cfg="cfg" />
<metaweblog-setting :api-type="props.apiType" :cfg="cnblogsCfg" />
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!--
- Copyright (c) 2022-2023, Terwer . All rights reserved.
- DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-
- This code is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License version 2 only, as
- published by the Free Software Foundation. Terwer designates this
- particular file as subject to the "Classpath" exception as provided
- by Terwer in the LICENSE file that accompanied this code.
-
- This code is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- version 2 for more details (a copy is included in the LICENSE file that
- accompanied this code).
-
- You should have received a copy of the GNU General Public License version
- 2 along with this work; if not, write to the Free Software Foundation,
- Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-
- Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
- or visit www.terwer.space if you need additional information or have any
- questions.
-->

<script lang="ts" setup>
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import MetaweblogSetting from "~/src/components/set/publish/singleplatform/MetaweblogSetting.vue"
import { useWordpressApi } from "~/src/composables/api/useWordpressApi.ts"
import { WordpressConfig } from "~/src/adaptors/api/wordpress/config/wordpressConfig.ts"
import { WordpressPlaceholder } from "~/src/adaptors/api/wordpress/config/wordpressPlaceholder.ts"
import WordpressUtils from "~/src/adaptors/api/wordpress/wordpressUtils.ts"
import { StrUtil } from "zhi-common"
const props = defineProps({
apiType: {
type: String,
default: "",
},
})
// 处理事件的方法
const onHomeChange = (value: string, cfg: WordpressConfig) => {
const { home, apiUrl } = WordpressUtils.parseHomeAndUrl(value)
cfg.home = home
cfg.apiUrl = apiUrl
if (StrUtil.isEmptyString(cfg.home)) {
cfg.apiUrl = ""
}
}
const { t } = useVueI18n()
const { cfg } = await useWordpressApi(props.apiType)
const wpCfg = cfg as WordpressConfig
const wpPlaceholder = new WordpressPlaceholder()
wpPlaceholder.homePlaceholder = t("setting.wordpress.home.tip")
wpPlaceholder.usernamePlaceholder = t("setting.wordpress.username.tip")
wpPlaceholder.passwordPlaceholder = t("setting.wordpress.password.tip")
wpPlaceholder.apiUrlPlaceholder = t("setting.wordpress.apiUrl.tip")
wpPlaceholder.previewUrlPlaceholder = t("setting.wordpress.previewUrl.tip")
wpCfg.placeholder = wpPlaceholder
</script>

<template>
<metaweblog-setting :api-type="props.apiType" :cfg="wpCfg" @onHomeChange="onHomeChange" />
</template>
2 changes: 1 addition & 1 deletion src/composables/api/useCnblogsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const useCnblogsApi = async (key?: string) => {
const appInstance = new AppInstance()

// 从环境变量获取Cnblogs API的URL、用户名、认证令牌和中间件URL
const cnblogsApiUrl = Utils.emptyOrDefault(process.env.VITE_CNBLOGS_API_URL, "https://rpc.cnblogs.com/metaweblog/[yourname]")
const cnblogsApiUrl = Utils.emptyOrDefault(process.env.VITE_CNBLOGS_API_URL, "https://rpc.cnblogs.com/metaweblog/[your-blog-name]")
const cnblogsUsername = Utils.emptyOrDefault(process.env.VITE_CNBLOGS_USERNAME, "")
const cnblogsAuthToken = Utils.emptyOrDefault(process.env.VITE_CNBLOGS_AUTH_TOKEN, "")
const middlewareUrl = Utils.emptyOrDefault(process.env.VITE_MIDDLEWARE_URL, "https://api.terwer.space/api/middleware")
Expand Down
Loading

0 comments on commit a780b81

Please sign in to comment.