Skip to content

Commit

Permalink
Merge pull request #390 from terwer/dev
Browse files Browse the repository at this point in the history
feat: fit latest siyuan note
  • Loading branch information
terwer authored Oct 9, 2023
2 parents 89c6d9f + 3e107ab commit 7f45bc3
Show file tree
Hide file tree
Showing 106 changed files with 11,987 additions and 408 deletions.
122 changes: 122 additions & 0 deletions libs/zhi-blog-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,127 @@
# zhi-blog-api

## 1.45.0

### Minor Changes

- feat: not force lowercase

## 1.44.4

### Patch Changes

- fix: remove same item

## 1.44.3

### Patch Changes

- Updated dependencies
- [email protected]

## 1.44.2

### Patch Changes

- fix: add search

## 1.44.1

### Patch Changes

- fix: add search option

## 1.44.0

### Minor Changes

- feat: add search for getCategories

## 1.43.0

### Minor Changes

- feat: add mdFilename

## 1.42.0

### Minor Changes

- feat: add usePathCategory

## 1.41.0

### Minor Changes

- feat: add useMdFilename

## 1.40.0

### Minor Changes

- feat: add mdFilename

## 1.39.0

### Minor Changes

- feat: export TagInfo

## 1.38.0

### Minor Changes

- feat: add getTags method

## 1.37.1

### Patch Changes

- feat: add tagSlugEnabled

## 1.37.0

### Minor Changes

- feat: add tags_slugs field

## 1.36.3

### Patch Changes

- feat: add filename for uploadFile

## 1.36.2

### Patch Changes

- fix: deduplicate duplicate null values

## 1.36.1

### Patch Changes

- fix; change empty categories

## 1.36.0

### Minor Changes

- feat: YAML serialization requires merge labels and classifications

## 1.35.0

### Minor Changes

- feat: Extract fromYaml and toYamlObj

## 1.34.14

### Patch Changes

- Updated dependencies
- [email protected]

## 1.34.13

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion libs/zhi-blog-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-blog-api",
"version": "1.34.13",
"version": "1.45.0",
"type": "module",
"description": "a common blog interface",
"main": "./dist/index.js",
Expand Down
5 changes: 4 additions & 1 deletion libs/zhi-blog-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import CategoryTypeEnum from "./lib/enums/categoryTypeEnum"
import YamlFormatObj from "./lib/models/yamlFormatObj"
import YamlConvertAdaptor from "./lib/yamlConvertAdaptor"
import PreferenceConfig from "./lib/PreferenceConfig"
import PostUtil from "./lib/PostUtil"
import TagInfo from "./lib/models/tagInfo"

export { BlogApi, BlogAdaptor }
export { WebApi, WebAdaptor }
export { BlogConfig, BlogPlaceholder, PasswordType, PageType, PostStatusEnum }
export { WebConfig, WebPlaceholder }
export { Post, UserBlog, SiteConfig, CategoryInfo, MediaObject, Attachment }
export { Post, UserBlog, SiteConfig, CategoryInfo, TagInfo, MediaObject, Attachment }
export { BlogConstants, BlogTypeEnum, PageTypeEnum, CategoryTypeEnum }
export { YamlFormatObj, YamlConvertAdaptor }
export { PostUtil }
export { PreferenceConfig }
export { type ElectronCookie }
13 changes: 11 additions & 2 deletions libs/zhi-blog-api/src/lib/IBlogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import MediaObject from "./models/mediaObject"
import CategoryInfo from "./models/categoryInfo"
import Attachment from "./models/attachmentInfo"
import YamlConvertAdaptor from "./yamlConvertAdaptor"
import TagInfo from "./models/tagInfo"

/**
* 通用博客接口
Expand All @@ -42,10 +43,12 @@ import YamlConvertAdaptor from "./yamlConvertAdaptor"
interface IBlogApi {
/**
* 博客配置列表
*
* @param keyword - 搜索关键字,部分平台不支持
* @see {@link https://codex.wordpress.org/XML-RPC_MetaWeblog_API#metaWeblog.getUsersBlogs getUsersBlogs}
* @returns {Promise<Array<UserBlog>>}
*/
getUsersBlogs(): Promise<Array<UserBlog>>
getUsersBlogs(keyword?: string): Promise<Array<UserBlog>>

/**
* 最新文章数目
Expand Down Expand Up @@ -161,10 +164,16 @@ interface IBlogApi {
/**
* 获取分类列表
*
* @param keyword - 搜索关键字,部分平台不支持
* @see {@link https://codex.wordpress.org/XML-RPC_MetaWeblog_API#metaWeblog.getCategories getCategories}
* @returns {Promise<CategoryInfo[]>}
*/
getCategories(): Promise<CategoryInfo[]>
getCategories(keyword?: string): Promise<CategoryInfo[]>

/**
* 获取标签列表
*/
getTags(): Promise<TagInfo[]>

/**
* 获取文件树列表
Expand Down
3 changes: 2 additions & 1 deletion libs/zhi-blog-api/src/lib/IWebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ interface IWebApi extends IBlogApi {
* 上传图片:调用平台 API 上传图片
*
* @param file 图片文件
* @param filename 文件名,可选
* @returns Promise<string> 上传后的图片地址
*/
uploadFile(file: File): Promise<any>
uploadFile(file: File, filename?: string): Promise<any>

/**
* 更新文章:调用平台 API 更新文章(发布工具内部通过该接口替换文章内图片地址)
Expand Down
32 changes: 32 additions & 0 deletions libs/zhi-blog-api/src/lib/PostUtil.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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, [email protected]
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { describe, it } from "vitest"

describe("test PostUtil", () => {
it("test fromYaml", () => {

})
})
106 changes: 106 additions & 0 deletions libs/zhi-blog-api/src/lib/PostUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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, [email protected]
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import Post from "./models/post"
import { DateUtil } from "zhi-common"

/**
* 文章处理工具类
*
* @since 1.11.2
*/
class PostUtil {
/**
* 将当前对象的数据转换为适用于 YAML 的对象
*
* @returns {Object} 表示数据的适用于 YAML 的对象
*/
public static toYamlObj(post: Post): Record<string, any> {
const yamlObj: Record<string, any> = {}

post.dateCreated && (yamlObj.created = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true))
post.dateUpdated && (yamlObj.updated = DateUtil.formatIsoToZh(post.dateUpdated.toISOString(), true))
post.title && (yamlObj.title = post.title)
post.wp_slug && (yamlObj.slug = post.wp_slug)
post.permalink && (yamlObj.permalink = post.permalink)
post.shortDesc && (yamlObj.desc = post.shortDesc)
post.mt_keywords && (yamlObj.tags = post.mt_keywords?.split(","))
post.categories && (yamlObj.categories = post.categories)

return yamlObj
}

/**
* 使用来自适用于 YAML 的对象的数据填充当前对象的属性
*
* @param post - 文章对象
* @param {Object} yamlObj - 包含要填充对象属性的数据的适用于 YAML 的对象
*/
public static fromYaml(post: Post, yamlObj: Record<string, any>): void {
post.dateCreated = yamlObj?.created ? DateUtil.convertStringToDate(yamlObj.created) : post.dateCreated
post.dateUpdated =
(yamlObj?.updated ? DateUtil.convertStringToDate(yamlObj.updated) : post.dateUpdated) ?? new Date()
post.title = yamlObj?.title ?? post.title
post.wp_slug = yamlObj?.slug ?? post.wp_slug
post.permalink = yamlObj?.permalink ?? post.permalink
post.shortDesc = yamlObj?.desc ?? post.shortDesc

// // 标签合并
// if (yamlObj?.tags && yamlObj.tags.length > 0) {
// const existingTags = post.mt_keywords.split(",")
// const uniqueKeywords = [...new Set([...existingTags, ...yamlObj.tags])].filter((tag) => tag.trim() !== "")
// post.mt_keywords = uniqueKeywords.length > 0 ? uniqueKeywords.join(",") : ""
// }
//
// // 分类合并
// if (yamlObj?.categories && yamlObj.categories.length > 0) {
// const combinedCategories = [...new Set([...post.categories, ...yamlObj.categories])].filter(
// (cate) => cate.trim() !== ""
// )
// post.categories = combinedCategories.length > 0 ? combinedCategories : []
// }

// 标签合并
if (yamlObj?.tags && yamlObj.tags.length > 0) {
const existingTags = post.mt_keywords.split(",")
const uniqueKeywords = [
...new Set([...existingTags.map((tag) => tag.trim()), ...yamlObj.tags.map((tag) => tag.trim())]),
].filter((tag) => tag.trim() !== "")

post.mt_keywords = uniqueKeywords.length > 0 ? uniqueKeywords.join(",") : ""
}

// 分类合并
if (yamlObj?.categories && yamlObj.categories.length > 0) {
const combinedCategories = [
...new Set([...post.categories.map((cate) => cate.trim()), ...yamlObj.categories.map((cate) => cate.trim())]),
].filter((cate) => cate.trim() !== "")

post.categories = combinedCategories.length > 0 ? combinedCategories : []
}
}
}

export default PostUtil
16 changes: 12 additions & 4 deletions libs/zhi-blog-api/src/lib/blogAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { simpleLogger } from "zhi-lib-base"
import Attachment from "./models/attachmentInfo"
import BlogApi from "./blogApi"
import YamlConvertAdaptor from "./yamlConvertAdaptor"
import TagInfo from "./models/tagInfo"

/**
* 博客API
Expand Down Expand Up @@ -63,8 +64,8 @@ class BlogAdaptor implements IBlogApi {
/**
* 博客配置列表
*/
public async getUsersBlogs(): Promise<Array<UserBlog>> {
return await this.apiAdaptor.getUsersBlogs()
public async getUsersBlogs(keyword?: string): Promise<Array<UserBlog>> {
return await this.apiAdaptor.getUsersBlogs(keyword)
}

/**
Expand Down Expand Up @@ -147,11 +148,18 @@ class BlogAdaptor implements IBlogApi {
return await this.apiAdaptor.deletePost(postid)
}

/**
* 获取标签列表
*/
public async getTags(): Promise<TagInfo[]> {
return await this.apiAdaptor.getTags()
}

/**
* 获取分类列表
*/
public async getCategories(): Promise<CategoryInfo[]> {
return await this.apiAdaptor.getCategories()
public async getCategories(keyword?: string): Promise<CategoryInfo[]> {
return await this.apiAdaptor.getCategories(keyword)
}

/**
Expand Down
Loading

0 comments on commit 7f45bc3

Please sign in to comment.