Skip to content

Commit

Permalink
feat: 常规发布支持文章比对
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 5, 2023
1 parent e244c5f commit 4d34248
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 46 deletions.
17 changes: 9 additions & 8 deletions src/adaptors/api/base/baseBlogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
* or visit www.terwer.space if you need additional information or have any
* questions.
*/
import {BlogApi, BlogConfig} from "zhi-blog-api"
import {SiyuanKernelApi} from "zhi-siyuan-api"
import {CommonFetchClient} from "zhi-fetch-middleware"
import {AppInstance} from "~/src/appInstance.ts"
import {createAppLogger, ILogger} from "~/src/utils/appLogger.ts"
import {useSiyuanApi} from "~/src/composables/useSiyuanApi.ts"
import {JsonUtil, ObjectUtil, StrUtil} from "zhi-common"
import {isDev} from "~/src/utils/constants.ts"

import { BlogApi, BlogConfig } from "zhi-blog-api"
import { SiyuanKernelApi } from "zhi-siyuan-api"
import { CommonFetchClient } from "zhi-fetch-middleware"
import { AppInstance } from "~/src/appInstance.ts"
import { createAppLogger, ILogger } from "~/src/utils/appLogger.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common"
import { isDev } from "~/src/utils/constants.ts"

/**
* API授权统一封装基类
Expand Down
117 changes: 100 additions & 17 deletions src/adaptors/api/notion/adaptor/notionApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
* questions.
*/

import { UserBlog } from "zhi-blog-api"
import { CategoryInfo, Post, UserBlog } from "zhi-blog-api"
import { BaseBlogApi } from "~/src/adaptors/api/base/baseBlogApi.ts"
import { NotionConfig } from "~/src/adaptors/api/notion/config/notionConfig.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { ObjectUtil, StrUtil } from "zhi-common"

/**
* Notion API 适配器
Expand All @@ -40,19 +41,75 @@ class NotionApiAdaptor extends BaseBlogApi {
public async getUsersBlogs(): Promise<UserBlog[]> {
const result: UserBlog[] = []

// https://developers.notion.com/reference/post-search
const headers = {
Authorization: `Bearer ${this.cfg.password}`,
"Notion-Version": "2022-06-28",
const pages = await this.getPages()
// 数据适配
pages.forEach((item: any) => {
const userblog: UserBlog = new UserBlog()
userblog.blogid = item.id
const titles = item?.properties?.title?.title ?? []
userblog.blogName = titles.map((x: any) => x.plain_text).join("")
userblog.url = item.public_url
result.push(userblog)
})
this.logger.debug("get usersBlogs result=>", result)

return result
}

public async newPost(post: Post, publish?: boolean): Promise<string> {
return super.newPost(post)
}

public async editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
return await super.editPost(postid, post)
}

public async deletePost(postid: string): Promise<boolean> {
return await super.deletePost(postid)
}

public async getPost(postid: string, useSlug?: boolean): Promise<Post> {
return await super.getPost(postid)
}

public async getCategories(): Promise<CategoryInfo[]> {
const cats = [] as CategoryInfo[]

const pages: any[] = await this.getPages()
if (pages && pages.length > 0) {
pages.forEach((item: any) => {
const cat = new CategoryInfo()
cat.categoryId = item.id
const titles = item?.properties?.title?.title ?? []
cat.categoryName = titles.map((x: any) => x.plain_text).join("")
cat.description = cat.categoryName
cat.categoryDescription = cat.categoryName
cat.htmlUrl = item.public_url
cats.push(cat)
})
}

return cats
}

public async getPreviewUrl(postid: string): Promise<string> {
const purl = this.cfg.previewUrl ?? ""
const postUrl = purl.replace("[postid]", postid)
return StrUtil.pathJoin(this.cfg.home ?? "", postUrl)
}

// ================
// private methods
// ================
private async getPages(): Promise<any[]> {
const params = {
page_size: 10,
filter: {
value: "page",
property: "object",
},
}
const searchResp = await this.proxyFetch("/search", [headers], params, "POST", "application/json")
const searchResp = await this.notionRequest("/search", params, "POST")
this.logger.debug("notion searchResp=>", searchResp)
if (searchResp?.status === 401) {
throw new Error(searchResp?.message)
Expand All @@ -67,18 +124,44 @@ class NotionApiAdaptor extends BaseBlogApi {
)
}

// 数据适配
pages.forEach((item: any) => {
const userblog: UserBlog = new UserBlog()
userblog.blogid = item.id
const titles = item?.properties?.title?.title ?? []
userblog.blogName = titles.map((x: any) => x.plain_text).join("")
userblog.url = item.public_url
result.push(userblog)
})
this.logger.debug("get usersBlogs result=>", result)
return pages
}

return result
/**
* 向Notion请求数据
*
* @param url 请求地址
* @param params 数据
* @param method 请求方法 GET | POST | PUT | DELETE
* @private
*/
private async notionRequest(
url: string,
params?: any,
method: "GET" | "POST" | "PUT" | "DELETE" = "POST"
): Promise<any> {
const contentType = "application/json"
// https://developers.notion.com/reference/post-search
const headers = {
"Content-Type": contentType,
Authorization: `Bearer ${this.cfg.password}`,
"Notion-Version": "2022-06-28",
}

// 打印日志
this.logger.debug("向Notion请求数据,url =>", url)
this.logger.debug("向Notion请求数据,params =>", params)

// 使用兼容的fetch调用并返回统一的JSON数据
const body = ObjectUtil.isEmptyObject(params) ? {} : params
const resJson = await this.proxyFetch(url, [headers], body, method, contentType)
this.logger.debug("向Notion请求数据,resJson =>", resJson)

if (resJson?.status === 401) {
throw new Error(resJson?.message)
}

return resJson
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/adaptors/api/yuque/adaptor/yuqueApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ class YuqueApiAdaptor extends BaseBlogApi {
this.logger.debug("向语雀请求数据,params =>", params)

// 使用兼容的fetch调用并返回统一的JSON数据
let body = ""
if (!ObjectUtil.isEmptyObject(params)) {
body = JSON.stringify(params)
}
const body = ObjectUtil.isEmptyObject(params) ? "" : JSON.stringify(params);
const resJson = await this.proxyFetch(url, [headers], body, method, contentType)
this.logger.debug("向语雀请求数据,resJson =>", resJson)

Expand Down
47 changes: 43 additions & 4 deletions src/components/publish/SinglePublishDoPublish.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { pre } from "~/src/utils/import/pre.ts"
import { DynamicConfig } from "~/src/components/set/publish/platform/dynamicConfig.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { ElMessage } from "element-plus"
const logger = createAppLogger("single-publish-do-publish")
Expand All @@ -44,6 +45,7 @@ const route = useRoute()
const { doInitPage } = usePublish()
const { query } = useRoute()
const { kernelApi, blogApi } = useSiyuanApi()
const { doSinglePublish } = usePublish()
// datas
const sysKeys = pre.systemCfg.map((item) => {
Expand All @@ -65,10 +67,35 @@ const formData = reactive({
mergedPost: {} as Post,
singleFormData: {} as any,
changeTips: {
title: "",
},
actionEnable: true,
})
const handlePublish = async () => {}
const handlePublish = async () => {
try {
formData.isPublishLoading = true
logger.info("单个常规发布开始")
const processResult = await doSinglePublish(key, id, formData.mergedPost)
logger.info("normal publish processResult =>", processResult)
logger.info("单个常规发布结束")
// 刷新页面
formData.isInit = false
formData.actionEnable = false
await initPage()
// 需要刷新才能继续操作,防止重复提交
formData.isInit = true
formData.actionEnable = true
} catch (error) {
ElMessage.error(error.message)
} finally {
formData.isPublishLoading = false
}
}
const handleDelete = async () => {}
Expand All @@ -91,15 +118,22 @@ const showChangeTip = (v1: string, v2: string) => {
return `系统标题为 [${v1}] , 已在远程平台被修改为 [${v2}]`
}
onMounted(async () => {
logger.info("获取到的ID为=>", id)
const refreshChangeTips = () => {
formData.changeTips.title = showChangeTip(formData.siyuanPost.title, formData.platformPost.title)
}
const initPage = async () => {
try {
formData.singleFormData = await doInitPage(key, id, method)
// 文章元数据
formData.siyuanPost = formData.singleFormData.siyuanPost
formData.platformPost = formData.singleFormData.platformPost
formData.mergedPost = formData.singleFormData.mergedPost
// 刷新比对提示
refreshChangeTips()
logger.debug("formData.platformPost=>", formData.platformPost)
} catch (e) {
const errMsg = t("main.opt.failure") + "=>" + e
Expand All @@ -109,7 +143,12 @@ onMounted(async () => {
timeout: 7000,
})
}
}
onMounted(async () => {
logger.info("获取到的ID为=>", id)
await initPage()
formData.isInit = true
})
</script>
Expand All @@ -132,7 +171,7 @@ onMounted(async () => {
<el-alert
v-if="method === MethodEnum.METHOD_EDIT && formData.siyuanPost.title !== formData.platformPost.title"
class="top-tip"
:title="showChangeTip(formData.siyuanPost.title, formData.platformPost.title)"
:title="formData.changeTips.title"
type="error"
:closable="false"
/>
Expand Down
12 changes: 6 additions & 6 deletions src/utils/appLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
* questions.
*/

import { isDev } from "~/src/utils/constants.ts"
import { isDebugMode, isDev } from "~/src/utils/constants.ts"
import { simpleLogger } from "zhi-lib-base"

/**
* 使用 eruda 更好的控制日志
*/
window.console = isDev ? (window as any).eruda.get("console") : window.console
window.console = isDev && isDebugMode ? (window as any).eruda.get("console") : window.console

/**
* 简单的日志接口
Expand All @@ -55,7 +55,7 @@ export const createAppLogger = (name: string): ILogger => {
/**
* 销毁日志
*/
export const destroyLogger = (): void => {
const win = window as any
win.eruda.destroy()
}
// export const destroyLogger = (): void => {
// const win = window as any
// win.eruda.destroy()
// }
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

export const isDev = process.env.DEV_MODE === "true"
export const isDebugMode = process.env.DEBUG_MODE === "true"

/**
* 动态配置key,全系统唯一,请勿更改
Expand Down
18 changes: 11 additions & 7 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import { nodePolyfills } from "vite-plugin-node-polyfills"

// methods start
const getAppBase = (isSiyuanBuild: boolean, isWidgetBuild: boolean, isStaticBuild: boolean): string => {
if (isSiyuanBuild) {
return "/plugins/siyuan-plugin-publisher/"
Expand All @@ -22,13 +23,14 @@ const getAppBase = (isSiyuanBuild: boolean, isWidgetBuild: boolean, isStaticBuil
}
}

const getDefineEnv = (isDevMode: boolean) => {
const getDefineEnv = (isDevMode: boolean, debugMode: boolean) => {
const mode = process.env.NODE_ENV
console.log("isServe=>", isServe)
console.log("mode=>", mode)

const defaultEnv = {
DEV_MODE: `${isDevMode}`,
DEBUG_MODE: `${debugMode}`,
APP_BASE: `${appBase}`,
NODE_ENV: "development",
VITE_DEFAULT_TYPE: `siyuan`,
Expand All @@ -50,13 +52,15 @@ const getDefineEnv = (isDevMode: boolean) => {

return defineEnv
}
// methods end

// config
const args = minimist(process.argv.slice(2))
const debugMode = false
// 开启之后可以同eruda接管日志
const debugMode = process.env.DEBUG_MODE === "true"
const isServe = process.env.IS_SERVE
const isWatch = args.watch || args.w || false
const isDev = isServe || isWatch || debugMode
// const isDev = false
const isWindows = process.platform === "win32"
let devDistDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
// let devDistDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/plugins/siyuan-plugin-publisher"
Expand All @@ -67,7 +71,7 @@ if (isWindows) {
const isSiyuanBuild = process.env.BUILD_TYPE === "siyuan"
const isWidgetBuild = process.env.BUILD_TYPE === "widget"
const isStaticBuild = process.env.BUILD_TYPE === "static"
const isChromeBuild = process.env.BUILD_TYPE === "chrome"
// const isChromeBuild = process.env.BUILD_TYPE === "chrome"
const distDir = isWatch ? devDistDir : isWidgetBuild ? "widget" : "./dist"
const appBase = getAppBase(isSiyuanBuild, isWidgetBuild, isStaticBuild)

Expand Down Expand Up @@ -97,7 +101,7 @@ export default defineConfig({
inject: {
// 在 body 标签底部插入指定的 JavaScript 文件
tags:
isDev && !isChromeBuild
isDev && debugMode
? [
{
tag: "script",
Expand Down Expand Up @@ -125,7 +129,7 @@ export default defineConfig({
],
data: {
title: "eruda",
injectScript: isDev && !isChromeBuild ? `<script>eruda.init();</script>` : "",
injectScript: isDev && debugMode ? `<script>eruda.init();</script>` : "",
},
},
}),
Expand Down Expand Up @@ -156,7 +160,7 @@ export default defineConfig({
// https://vitejs.dev/guide/env-and-mode.html#env-files
// https://github.com/vitejs/vite/discussions/3058#discussioncomment-2115319
// 在这里自定义变量
define: getDefineEnv(isDev),
define: getDefineEnv(isDev, debugMode),

resolve: {
alias: {
Expand Down

0 comments on commit 4d34248

Please sign in to comment.