Skip to content

Commit

Permalink
feat: 支持 Metaweblog
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 1, 2023
1 parent 36d905a commit 6d442fb
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 33 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ This plugin promises that the basic functions will be free forever, and the foll

Names not listed in order

- [X] Cnblogs
- [ ] WordPress
- [X] Yuque
- [X] Metaweblog
- [X] Cnblogs
- [X] Typecho
- [X] WordPress
- [ ] Github
- [ ] Hexo
- [ ] Hugo
Expand Down
6 changes: 4 additions & 2 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@

排名不分先后

- [X] 博客园
- [ ] WordPress
- [X] 语雀
- [X] 博客园
- [X] Metaweblog
- [X] Typecho
- [X] WordPress
- [ ] Github
- [ ] Hexo
- [ ] Hugo
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"serve": "python scripts/serve.py",
"dev": "python scripts/dev.py",
"build": "python scripts/build.py",
"pluginDev": "python scripts/plugin_dev.py",
"pluginBuild": "python scripts/plugin_build.py",
"siyuanBuild": "python scripts/siyuan_build.py",
"vercelBuild": "python3 scripts/vercel_build.py",
Expand Down
38 changes: 38 additions & 0 deletions scripts/plugin_dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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 os

import scriptutils

if __name__ == "__main__":
# Switch to the working directory.
scriptutils.switch_workdir()

# Get the current working directory.
cwd = scriptutils.get_workdir()

# 获取当前工作目录
print(os.getcwd())

os.system("zhi-build --watch")
4 changes: 1 addition & 3 deletions siyuan/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
* questions.
*/

export const workspaceDir = `${(window as any).siyuan.config.system.workspaceDir}`
export const dataDir = `${(window as any).siyuan.config.system.dataDir}`
export const isDev = process.env.DEV_MODE === "true"
export const isSiyuanDev = process.env.DEV_MODE === "true"

export const siyuanApiToken = ""
export const siyuanApiUrl = ""
Expand Down
8 changes: 4 additions & 4 deletions siyuan/api/base-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* questions.
*/

import { isDev, siyuanApiToken, siyuanApiUrl } from "../Constants"
import { isSiyuanDev, siyuanApiToken, siyuanApiUrl } from "../Constants"
import { simpleLogger } from "zhi-lib-base"

/**
Expand All @@ -50,7 +50,7 @@ export class BaseApi {
private logger

constructor() {
this.logger = simpleLogger("base-api", "custom-slug", isDev)
this.logger = simpleLogger("base-api", "publisher", isSiyuanDev)
}

/**
Expand Down Expand Up @@ -86,14 +86,14 @@ export class BaseApi {
})
}

if (isDev) {
if (isSiyuanDev) {
this.logger.info("开始向思源请求数据,reqUrl=>", reqUrl)
this.logger.info("开始向思源请求数据,fetchOps=>", fetchOps)
}

const response = await fetch(reqUrl, fetchOps)
const resJson = (await response.json()) as SiyuanData
if (isDev) {
if (isSiyuanDev) {
this.logger.info("思源请求数据返回,resJson=>", resJson)
}
return resJson
Expand Down
8 changes: 4 additions & 4 deletions siyuan/appLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
* questions.
*/

import { isDev } from "./Constants"
import { isSiyuanDev } from "./Constants"
import { simpleLogger } from "zhi-lib-base"

/**
* 使用 eruda 更好的控制日志
*/
if (typeof window !== "undefined") {
const econole = (window as any)?.eruda?.get("console")
window.console = isDev && econole ? econole : window.console
window.console = isSiyuanDev && econole ? econole : window.console
}

/**
Expand All @@ -51,6 +51,6 @@ interface ILogger {
* @version 1.0.0
* @since 1.0.0
*/
export const createAppLogger = (name: string): ILogger => {
return simpleLogger(name, "publisher", isDev)
export const createSiyuanAppLogger = (name: string): ILogger => {
return simpleLogger(name, "publisher", isSiyuanDev)
}
8 changes: 6 additions & 2 deletions siyuan/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
// 建议使用 iconfont ,可以调色,可以调整大小
// https://fontawesome.com/search?q=yuque&o=r&m=free
// https://www.iconfont.cn/search/index?searchType=icon&q=cnblogs&page=1&tag=&fromCollection=1&fills=
.iconfont-icon{
.iconfont-icon
width 12px
height 12px
margin-right 10px
margin-top 2.5px
}

.img-icon img
width 16px !important
height 16px !important
margin-right 8px
4 changes: 2 additions & 2 deletions siyuan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { App, getFrontend, IObject, Plugin } from "siyuan"
import { SiyuanConfig, SiyuanKernelApi } from "zhi-siyuan-api"
import { createAppLogger } from "./appLogger"
import { createSiyuanAppLogger } from "./appLogger"
import { WidgetInvoke } from "./invoke/widgetInvoke"
import { Topbar } from "./topbar"

Expand All @@ -44,7 +44,7 @@ export default class PublisherPlugin extends Plugin {
constructor(options: { app: App; id: string; name: string; i18n: IObject }) {
super(options)

this.logger = createAppLogger("index")
this.logger = createSiyuanAppLogger("index")

const frontEnd = getFrontend()
this.isMobile = frontEnd === "mobile" || frontEnd === "browser-mobile"
Expand Down
4 changes: 2 additions & 2 deletions siyuan/invoke/pluginInvoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import PublisherPlugin from "../index"
import { createAppLogger } from "../appLogger"
import { createSiyuanAppLogger } from "../appLogger"
import { showIframeDialog } from "../iframeDialog"
import PageUtil from "../utils/pageUtil"
import { IObject } from "siyuan"
Expand All @@ -40,7 +40,7 @@ export class PluginInvoke {
private blogPluginBase = "/plugins/siyuan-blog/#"

constructor(pluginInstance: PublisherPlugin) {
this.logger = createAppLogger("plugin-invoke")
this.logger = createSiyuanAppLogger("plugin-invoke")
this.pluginInstance = pluginInstance
}

Expand Down
5 changes: 2 additions & 3 deletions siyuan/invoke/widgetInvoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
*/

import { DeviceDetection, DeviceTypeEnum } from "zhi-device"
import { createAppLogger } from "../appLogger"
import { createSiyuanAppLogger } from "../appLogger"
import PageUtil from "../utils/pageUtil"
import { showIframeDialog } from "../iframeDialog"
import PublisherPlugin from "../index"
import { StrUtil } from "zhi-common"
import { showMessage } from "siyuan"
import { isFileExists } from "../utils/utils"

/**
* 挂件相关
Expand All @@ -40,7 +39,7 @@ export class WidgetInvoke {
private readonly pluginInstance

constructor(pluginInstance: PublisherPlugin) {
this.logger = createAppLogger("widget-invoke")
this.logger = createSiyuanAppLogger("widget-invoke")
this.pluginInstance = pluginInstance
}

Expand Down
17 changes: 14 additions & 3 deletions siyuan/topbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { icons } from "./utils/svg"
import { IMenuItemOption, Menu, showMessage } from "siyuan"
import PageUtil from "./utils/pageUtil"
import HtmlUtils from "./utils/htmlUtils"
import { createAppLogger } from "./appLogger"
import { createSiyuanAppLogger } from "./appLogger"
import { WidgetInvoke } from "./invoke/widgetInvoke"
import { PluginInvoke } from "./invoke/pluginInvoke"
import { ObjectUtil } from "zhi-common"
Expand All @@ -44,7 +44,7 @@ export class Topbar {
private pluginInvoke

constructor(pluginInstance: PublisherPlugin) {
this.logger = createAppLogger("topbar")
this.logger = createSiyuanAppLogger("topbar")
this.pluginInstance = pluginInstance
this.pluginInvoke = new PluginInvoke(pluginInstance)
this.widgetInvoke = new WidgetInvoke(pluginInstance)
Expand Down Expand Up @@ -75,9 +75,20 @@ export class Topbar {
this.logger.info("dynJsonCfg =>", dynJsonCfg.totalCfg)
// 构造发布菜单
dynJsonCfg.totalCfg.forEach((config: any) => {
let icon = `<span class="iconfont-icon">${config.platformIcon}</span>`
// 修复图片不展示问题
if (/^\<img/.test(config.platformIcon) && config.platformIcon.indexOf("./images") > -1) {
icon = config.platformIcon.replace(
/\.\/images/g,
`${window.location.origin}/plugins/siyuan-plugin-publisher/images`
)
icon = `<span class="img-icon">${icon}</span>`
}
if (config.isEnabled === true) {
// http://127.0.0.1:6806/plugins/siyuan-plugin-publisher/i

const submenu = {
iconHTML: `<span class="iconfont-icon">${config.platformIcon}</span>`,
iconHTML: `${icon}`,
label: config.platformName,
disabled: !config.isAuth,
click: async () => {
Expand Down
36 changes: 36 additions & 0 deletions src/adaptors/api/metaweblog/adaptor/metaweblogApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { MetaweblogBlogApi } from "~/src/adaptors/api/base/metaweblog/metaweblogBlogApi.ts"

/**
* Metaweblog API 适配器
*
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
class MetaweblogApiAdaptor extends MetaweblogBlogApi {}
export { MetaweblogApiAdaptor }
96 changes: 96 additions & 0 deletions src/adaptors/api/metaweblog/useMetaweblogApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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 { createAppLogger } from "~/src/utils/appLogger.ts"
import { AppInstance } from "~/src/appInstance.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common"
import { Utils } from "~/src/utils/utils.ts"
import { getDynPostidKey } from "~/src/components/set/publish/platform/dynamicConfig.ts"
import { MetaweblogApiAdaptor } from "~/src/adaptors/api/metaweblog/adaptor/metaweblogApiAdaptor.ts"

/**
* 使用Metaweblog API的自定义hook
*
* @param key 配置键值,可选参数
* @param newCfg
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
export const useMetaweblogApi = async (key?: string, newCfg?: MetaweblogConfig) => {
// 创建应用日志记录器
const logger = createAppLogger("use-metaweblog-api")

// 记录开始使用Metaweblog API
logger.info("Start using Metaweblog API...")

// 创建应用实例
const appInstance = new AppInstance()

let cfg: MetaweblogConfig
if (newCfg) {
logger.info("Initialize with the latest newCfg passed in...")
cfg = newCfg
} else {
// 从配置中获取数据
const { getSetting } = useSettingStore()
const setting = await getSetting()
cfg = JsonUtil.safeParse<MetaweblogConfig>(setting[key], {} as MetaweblogConfig)
// 如果配置为空,则使用默认的环境变量值,并记录日志
if (ObjectUtil.isEmptyObject(cfg)) {
// 从环境变量获取Metaweblog API的URL、用户名、认证令牌和中间件URL
const metaweblogApiUrl = ""
const metaweblogUsername = ""
const metaweblogAuthToken = ""
const middlewareUrl = Utils.emptyOrDefault(
process.env.VITE_MIDDLEWARE_URL,
"https://api.terwer.space/api/middleware"
)

cfg = new MetaweblogConfig("", metaweblogApiUrl, metaweblogUsername, metaweblogAuthToken, middlewareUrl)
logger.debug("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
}
// 初始化posidKey
if (StrUtil.isEmptyString(cfg.posidKey)) {
// 默认值
cfg.posidKey = getDynPostidKey(key)
}
}

// 创建Metaweblog API适配器
const blogApi = new MetaweblogApiAdaptor(appInstance, cfg)

// 记录Metaweblog API创建成功
logger.info("Metaweblog API created successfully.")

return {
cfg,
blogApi,
}
}

0 comments on commit 6d442fb

Please sign in to comment.