Skip to content

Commit

Permalink
Merge pull request #403 from terwer/dev
Browse files Browse the repository at this point in the history
feat: support outline and docTree
  • Loading branch information
terwer authored Nov 11, 2024
2 parents 1586dd0 + 4ff83dd commit 624d2e6
Show file tree
Hide file tree
Showing 26 changed files with 497 additions and 112 deletions.
49 changes: 49 additions & 0 deletions libs/zhi-blog-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
# zhi-blog-api

## 1.67.0

### Minor Changes

- feat: support outline and docTree
- feat: add outline and docTree

## 1.66.0

### Minor Changes

- feat: change uploadFile

## 1.65.0

### Minor Changes

- feat: passwordLabel default as undefined

## 1.64.0

### Minor Changes

- feat: add usernameLabel and passwordLabel option

## 1.63.0

### Minor Changes

- feat: fix post status

## 1.62.0

### Minor Changes

- feat: add post status enum

## 1.61.0

### Minor Changes

- feat: add forceProxy for telegra.ph like platforms

## 1.60.0

### Minor Changes

- feat: add image store path support for github and gitlab

## 1.59.0

### Minor 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.59.0",
"version": "1.67.0",
"type": "module",
"description": "a common blog interface",
"main": "./dist/index.js",
Expand Down
6 changes: 3 additions & 3 deletions libs/zhi-blog-api/src/lib/IWebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { IBlogApi } from "./IBlogApi"
import Post from "./models/post"
import ElectronCookie from "./models/ElectronCookie"
import WebConfig from "./WebConfig"
import MediaObject from "./models/mediaObject"

/**
* 通用博客接口
Expand Down Expand Up @@ -79,11 +80,10 @@ interface IWebApi extends IBlogApi {
/**
* 上传图片:调用平台 API 上传图片
*
* @param file 图片文件
* @param filename 文件名,可选
* @param mediaObject
* @returns Promise<string> 上传后的图片地址
*/
uploadFile(file: File, filename?: string): Promise<any>
uploadFile(mediaObject: MediaObject): Promise<any>

/**
* 更新文章:调用平台 API 更新文章(发布工具内部通过该接口替换文章内图片地址)
Expand Down
24 changes: 24 additions & 0 deletions libs/zhi-blog-api/src/lib/PreferenceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,35 @@ class PreferenceConfig {
*/
public removeMdWidgetTag: boolean

/**
* 是否启用目录
*/
public outlineEnable: boolean

/**
* 目录层级
*/
public outlineLevel: number

/**
* 是否启用文档树
*/
public docTreeEnable: boolean

/**
* 文档树层级
*/
public docTreeLevel: number

constructor() {
this.fixTitle = false
this.keepTitle = false
this.removeFirstH1 = false
this.removeMdWidgetTag = false
this.outlineEnable = false
this.outlineLevel = 3
this.docTreeEnable = false
this.docTreeLevel = 3
}
}

Expand Down
22 changes: 22 additions & 0 deletions libs/zhi-blog-api/src/lib/blogConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ abstract class BlogConfig {
*/
public username?: string

/**
* 用户名标题,不设置自动选择,默认 undefined
*/
public usernameLabel?: string

/**
* 密码类型
*/
Expand All @@ -110,6 +115,11 @@ abstract class BlogConfig {
*/
public password: string

/**
* 密码标题,不设置自动选择,默认 undefined
*/
public passwordLabel?: string

/**
* 密码/token设置地址
*/
Expand Down Expand Up @@ -275,11 +285,21 @@ abstract class BlogConfig {
*/
public bundledPicbedSupported?: boolean

/**
* 图片存储目录,部分平台会用到,相对于文章存储目录,默认为 images
*/
public imageStorePath?: string

/**
* 图床服务类型
*/
public picbedService?: PicbedServiceTypeEnum

/**
* 强制使用代理
*/
public forceProxy?: boolean

protected constructor() {
this.home = ""
this.apiUrl = ""
Expand Down Expand Up @@ -318,6 +338,8 @@ abstract class BlogConfig {
this.picgoPicbedSupported = false
this.bundledPicbedSupported = false
this.picbedService = PicbedServiceTypeEnum.None
this.imageStorePath = "images"
this.forceProxy = false
}
}

Expand Down
28 changes: 25 additions & 3 deletions libs/zhi-blog-api/src/lib/enums/postStatusEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,42 @@

/**
* 文章状态枚举
*
* @link https://codex.wordpress.org/Post_Status_Transitions
*/
enum PostStatusEnum {
/**
* 已发布
* 无先前状态
*/
PostStatusEnum_New = "new",
/**
* 发布
*/
PostStatusEnum_Publish = "publish",
/**
* 待审核
*/
PostStatusEnum_Pending = "pending",
/**
* 草稿
*/
PostStatusEnum_Draft = "draft",
/**
* 继承
* 自动草稿
*/
PostStatusEnum_AutoDraft = "auto-draft",
/**
* 定时发布
*/
PostStatusEnum_Future = "future",
/**
* 私密
*/
PostStatusEnum_Private = "private",
/**
* 垃圾箱
*/
PostStatusEnum_Inherit = "inherit",
PostStatusEnum_Trash = "trash",
}

export default PostStatusEnum
28 changes: 25 additions & 3 deletions libs/zhi-blog-api/src/lib/models/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
* questions.
*/

import PostStatusEnum from "../enums/postStatusEnum";
import PageEditMode from "./pageEditMode";
import YamlStrategy from "./yamlStrategy";
import PostStatusEnum from "../enums/postStatusEnum"
import PageEditMode from "./pageEditMode"
import YamlStrategy from "./yamlStrategy"

/**
* 通用文章模型定义
Expand Down Expand Up @@ -173,6 +173,24 @@ class Post {
*/
yamlType?: YamlStrategy

/**
* 目录
*/
outline?: any[]
/**
* 目录层级
*/
outlineLevel?: number

/**
* 文档树
*/
docTree?: any[]
/**
* 文档树层级
*/
docTreeLevel?: number

constructor() {
this.postid = ""
this.originalId = ""
Expand All @@ -197,6 +215,10 @@ class Post {
this.attrs = "{}"
this.editMode = PageEditMode.EditMode_simple
this.yamlType = YamlStrategy.YAML_default
this.outline = []
this.outlineLevel = 3
this.docTree = []
this.docTreeLevel = 3
}
}

Expand Down
5 changes: 3 additions & 2 deletions libs/zhi-blog-api/src/lib/webAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { simpleLogger } from "zhi-lib-base"
import ElectronCookie from "./models/ElectronCookie"
import WebConfig from "./WebConfig"
import WebApi from "./webApi"
import MediaObject from "./models/mediaObject";

/**
* 网页授权核心基类
Expand Down Expand Up @@ -81,8 +82,8 @@ class WebAdaptor extends BlogAdaptor {
return await this.webAdaptor.addPost(post)
}

public async uploadFile(file: File, filename?: string): Promise<any> {
return await this.webAdaptor.uploadFile(file, filename)
public async uploadFile(mediaObject: MediaObject): Promise<any> {
return await this.webAdaptor.uploadFile(mediaObject)
}

public async editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
Expand Down
3 changes: 2 additions & 1 deletion libs/zhi-blog-api/src/lib/webApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { NotImplementedException } from "zhi-lib-base"
import ElectronCookie from "./models/ElectronCookie"
import WebConfig from "./WebConfig"
import BlogApi from "./blogApi"
import MediaObject from "./models/mediaObject";

/**
* 网页授权基类
Expand All @@ -54,7 +55,7 @@ class WebApi extends BlogApi implements IWebApi {
throw new NotImplementedException("You must implement addPost in sub class")
}

public async uploadFile(file: File, filename?: string): Promise<any> {
public async uploadFile(mediaObject: MediaObject): Promise<any> {
throw new NotImplementedException("You must implement uploadFile in sub class")
}

Expand Down
12 changes: 12 additions & 0 deletions libs/zhi-fetch-middleware/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# zhi-fetch-middleware

## 0.12.0

### Minor Changes

- feat: check is resp is text

## 0.11.0

### Minor Changes

- feat: handle fetch for text/xml

## 0.10.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion libs/zhi-fetch-middleware/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-fetch-middleware",
"version": "0.10.0",
"version": "0.12.0",
"type": "module",
"description": "an intermediate tier prepared for fetch requests",
"main": "./dist/index.js",
Expand Down
26 changes: 15 additions & 11 deletions libs/zhi-fetch-middleware/src/lib/commonFetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class CommonFetchClient implements ICommonFetchClient {
throw new Error("请求异常,response is undefined")
}

let resJson: any
let resp: any

const isResponse = response?.status !== undefined && response?.headers !== undefined && response?.url !== undefined
const isStream = !isResponse && response?.body instanceof ReadableStream
Expand All @@ -115,7 +115,7 @@ class CommonFetchClient implements ICommonFetchClient {

if (isStream) {
this.logger.info("检测到response不是Response的实例", typeof response)
resJson = response
resp = response
} else {
// 解析响应体并返回响应结果
const statusCode = response.status
Expand Down Expand Up @@ -148,24 +148,28 @@ class CommonFetchClient implements ICommonFetchClient {
}
}

const contentType = fetchOptions.headers["Content-Type"]
const isText = contentType === "text/xml" || contentType === "text/html" || contentType === "text/plain"

this.logger.info("isText=>", isText)
this.logger.info("isNode=>", BrowserUtil.isNode)
this.logger.info("isElectron=>", BrowserUtil.isElectron())
this.logger.info("isInSiyuanWidget=>", SiyuanDevice.isInSiyuanWidget())
if (BrowserUtil.isNode) {
resJson = await this.safeParseBodyJson(response)
} else if (BrowserUtil.isElectron()) {
resJson = await this.safeParseBodyJson(response)
} else if (SiyuanDevice.isInSiyuanWidget()) {
resJson = await this.safeParseBodyJson(response)
if (BrowserUtil.isNode || BrowserUtil.isElectron() || SiyuanDevice.isInSiyuanWidget()) {
if (isText) {
resp = await response.text()
} else {
resp = await this.safeParseBodyJson(response)
}
} else {
this.logger.debug("开始解析CORSBody")
const corsJson = await this.safeParseBodyJson(response)
resJson = this.parseCORSBody(corsJson)
resp = this.parseCORSBody(corsJson)
}
}

this.logger.debug("全部处理完毕,resJson=>", resJson)
return resJson
this.logger.debug("全部处理完毕,resJson=>", resp)
return resp
}

/**
Expand Down
Loading

0 comments on commit 624d2e6

Please sign in to comment.