Skip to content

Commit

Permalink
feat: 新版挂件-适配Typecho配置
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 28, 2023
1 parent 3fb8ce9 commit f41d464
Show file tree
Hide file tree
Showing 16 changed files with 456 additions and 16 deletions.
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ declare module '@vue/runtime-core' {
SetIndex: typeof import('./src/components/set/SetIndex.vue')['default']
SettingEntry: typeof import('./src/components/set/publish/singleplatform/SettingEntry.vue')['default']
SiyuanTest: typeof import('./src/components/test/SiyuanTest.vue')['default']
TypechoSetting: typeof import('./src/components/set/publish/singleplatform/metaweblog/TypechoSetting.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']
Expand Down
72 changes: 72 additions & 0 deletions src/adaptors/api/typecho/adaptor/typechoApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 { BlogApi, UserBlog } from "zhi-blog-api"
import { CommonXmlrpcClient } from "zhi-xmlrpc-middleware"
import { AppInstance } from "~/src/appInstance.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { TypechoConfig } from "~/src/adaptors/api/typecho/config/typechoConfig.ts"
import { TypechoConstants } from "~/src/adaptors/api/typecho/typechoConstants.ts"

/**
* WordPress API 适配器
*
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
class TypechoApiAdaptor extends BlogApi {
private readonly logger
private readonly cfg: TypechoConfig
private readonly commonXmlrpcClient

/**
* 初始化 WordPress API 适配器
*
* @param appInstance 应用实例
* @param cfg 配置项
*/
constructor(appInstance: AppInstance, cfg: TypechoConfig) {
super()

this.cfg = cfg
this.logger = createAppLogger("typecho-api-adaptor")
this.commonXmlrpcClient = new CommonXmlrpcClient(appInstance, cfg.apiUrl)
}

public override async getUsersBlogs(): Promise<Array<UserBlog>> {
let result: UserBlog[] = []
result = await this.typechoCall(TypechoConstants.METHOD_GET_USERS_BLOGS, [])
this.logger.debug("getUsersBlogs=>", result)
return result
}

private async typechoCall(method: string, params: string[]) {
const parameters = ["typecho", this.cfg.username, this.cfg.password]
params.forEach((param) => parameters.push(param))
return await this.commonXmlrpcClient.methodCall(method, parameters, this.cfg.middlewareUrl)
}
}
export { TypechoApiAdaptor }
76 changes: 76 additions & 0 deletions src/adaptors/api/typecho/config/typechoConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 { MetaweblogConfig } from "~/src/adaptors/api/base/metaweblog/config/MetaweblogConfig.ts"
import { PageTypeEnum } from "zhi-blog-api"
import TypechoUtils from "~/src/adaptors/api/Typecho/TypechoUtils.ts"

/**
* Typecho 配置
*
* @author terwer
* @since 1.0.0
*/
class TypechoConfig extends MetaweblogConfig {
/**
* API 地址
*/
public override apiUrl = ""

/**
* 用户名
*/
public override username = ""

/**
* 密码
*/
public override password = ""

/**
* 代理地址
*/
public override middlewareUrl = ""

/**
* Typecho 配置项
*
* @param homeAddr Typecho 主页
* @param username 用户名
* @param password 密码
* @param middlewareUrl 代理地址
*/
constructor(homeAddr: string, username: string, password: string, middlewareUrl?: string) {
super(homeAddr, "", username, password, middlewareUrl)

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

export { TypechoConfig }
33 changes: 33 additions & 0 deletions src/adaptors/api/typecho/config/typechoPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { MetaweblogPlaceholder } from "~/src/adaptors/api/base/metaweblog/config/MetaweblogPlaceholder.ts"

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

export { TypechoPlaceholder }
1 change: 1 addition & 0 deletions src/adaptors/api/typecho/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TypechoApiAdaptor
37 changes: 37 additions & 0 deletions src/adaptors/api/typecho/typechoConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*/

/**
* 预定义 Typecho 变量
*
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
class TypechoConstants {
public static METHOD_GET_USERS_BLOGS = "metaWeblog.getUsersBlogs"
}

export { TypechoConstants }
56 changes: 56 additions & 0 deletions src/adaptors/api/typecho/typechoUtils.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"

/**
* 用于处理Typecho相关操作的实用工具类
*/
class TypechoUtils {
private static logger = createAppLogger("typecho-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("/index.php/action/xmlrpc")) {
apiUrl = home
home = home.replace("/index.php/action/xmlrpc", "")
} else {
home = home.replace(/\/$/, "")
apiUrl = `${home}/index.php/action/xmlrpc`
}

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

export default TypechoUtils
1 change: 1 addition & 0 deletions src/adaptors/api/wordpress/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# WordpressApiAdaptor
15 changes: 10 additions & 5 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
* questions.
*/

import { BlogAdaptor } from "zhi-blog-api"
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 {BlogAdaptor} from "zhi-blog-api"
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";
import {useTypechoApi} from "~/src/composables/api/useTypechoApi.ts";

/**
* 适配器统一入口
Expand Down Expand Up @@ -59,6 +59,11 @@ class Adaptors {
blogAdaptor = blogApi
break
}
case SubPlatformType.Metaweblog_Typecho:{
const { blogApi } = await useTypechoApi(key)
blogAdaptor = blogApi
break
}
default: {
break
}
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 @@ -31,6 +31,7 @@ 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"
import TypechoSetting from "~/src/components/set/publish/singleplatform/metaweblog/TypechoSetting.vue"
// uses
const { t } = useVueI18n()
Expand All @@ -46,6 +47,7 @@ const subtype = getSubPlatformTypeByKey(apiType)
<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" />
<typecho-setting v-else-if="subtype === SubPlatformType.Metaweblog_Typecho" :api-type="apiType" />
<span v-else>
<el-alert :closable="false" :title="t('setting.entry.not.supported')" class="top-tip" type="error" />
</span>
Expand Down
Loading

0 comments on commit f41d464

Please sign in to comment.