Skip to content

Commit

Permalink
fix: Vuepress 支持自动推测发布目录
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Oct 11, 2023
1 parent c8e934c commit 71e57d3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
65 changes: 56 additions & 9 deletions src/adaptors/api/gitlab-vuepress/gitlabvuepressApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { CommonGitlabApiAdaptor } from "~/src/adaptors/api/base/gitlab/commonGit
import _ from "lodash"
import { GitlabvuepressYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-vuepress/gitlabvuepressYamlConverterAdaptor.ts"
import { YamlUtil } from "zhi-common"
import { SiyuanDevice } from "zhi-device"

/**
* Hexo API 适配器
Expand Down Expand Up @@ -65,18 +66,64 @@ class GitlabvuepressApiAdaptor extends CommonGitlabApiAdaptor {
}
// ======

updatedPost.markdown = `${yfm}\n${updatedMd}`
this.logger.info("Gitlabvuepress 正文处理完毕")
this.logger.debug("updatedMd =>", { yfm: yfm, updatedMd: updatedMd })
// ======
// 自动推测保存路径
const selectedPath = updatedPost.cate_slugs?.[0] ?? cfg.blogid
if ("docs" === selectedPath) {
const doc = await this.baseExtendApi.kernelApi.getDoc(id)
const docPath = doc.path
const saveFile = await this.autoMapPublishDir(docPath)
updatedPost.cate_slugs = [saveFile]
// 自动生成一级目录
// ======

updatedPost.markdown = `${yfm}\n${updatedMd}`
this.logger.info("Gitlabvuepress 正文处理完毕")
this.logger.debug("updatedMd =>", { yfm: yfm, updatedMd: updatedMd })

// 发布格式
if (cfg?.pageType == PageTypeEnum.Markdown) {
updatedPost.description = updatedPost.markdown
} else {
updatedPost.description = updatedPost.html
}

// 发布格式
if (cfg?.pageType == PageTypeEnum.Markdown) {
updatedPost.description = updatedPost.markdown
} else {
updatedPost.description = updatedPost.html
return updatedPost
}
}

// ================
// private methods
// ================
private async autoMapPublishDir(docPath: string) {
this.logger.info("start autoMapPublishDir, docPath =>", docPath)

const win = SiyuanDevice.siyuanWindow()
const path = win.require("path")
const paths = docPath
.replace(/\.sy/, "")
.split("/")
.filter((x: string) => x !== "")

let save_dir: string
let save_file: string
const dir_arr = []
for (const item of paths) {
const attrs = await this.baseExtendApi.kernelApi.getBlockAttrs(item)
dir_arr.push(attrs.title)
}
const toDir = path.join("docs", ...dir_arr)
const toFile = toDir + ".md"
save_dir = path.dirname(toFile)
save_file = toFile
this.logger.info("finished autoMapPublishDir, save_dir =>", save_dir)
this.logger.info("finished autoMapPublishDir, save_file =>", save_file)

// 生成一级目录
// 暂时不做

return updatedPost
// 注意:这里不包括文件名
return save_dir
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/base/baseExtendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
private readonly api: BaseBlogApi | BaseWebApi
protected readonly picgoBridge: any
private readonly isSiyuanOrSiyuanNewWin: boolean
private readonly kernelApi: SiyuanKernelApi
public readonly kernelApi: SiyuanKernelApi

/**
* 构造函数用于创建一个新的实例
Expand Down

0 comments on commit 71e57d3

Please sign in to comment.