Skip to content

Commit

Permalink
feat: 支持文档路径自动映射为分类
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Oct 28, 2023
1 parent f5d0db7 commit 3787ed1
Show file tree
Hide file tree
Showing 49 changed files with 95 additions and 251 deletions.
2 changes: 0 additions & 2 deletions src/adaptors/api/base/commonBlogConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export class CommonBlogConfig extends BlogConfig {
this.allowPreviewUrlChange = true
this.showTokenTip = false
this.yamlLinkEnabled = true
this.useMdFilename = false
this.usePathCategory = false
this.placeholder = new CommonBlogPlaceholder()
}
}
47 changes: 0 additions & 47 deletions src/adaptors/api/base/github/commonGithubApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,53 +204,6 @@ class CommonGithubApiAdaptor extends BaseBlogApi {

return previewUrl
}

// ================
// private methods
// ================
// 在 baseExtendApi 里面处理了
// private processFilename(post: Post, cfg: CommonGitlabConfig) {
// // 处理文件规则
// const created = DateUtil.formatIsoToZhDate(post.dateCreated.toISOString(), true)
// const datearr = created.split(" ")[0]
// const numarr = datearr.split("-")
// const y = numarr[0]
// const m = numarr[1]
// const d = numarr[2]
// this.logger.debug("created numarr=>", numarr)
// let filename = cfg.mdFilenameRule.replace(/\.md/g, "")
// if (cfg.useMdFilename) {
// // 使用真实文件名作为MD文件名
// filename = filename.replace(/\[filename\]/g, post.title)
// } else {
// // 使用别名作为MD文件名
// filename = filename.replace(/\[slug\]/g, post.wp_slug)
// }
// // 年月日
// filename = filename
// .replace(/\[yyyy\]/g, y)
// .replace(/\[MM\]/g, m)
// .replace(/\[mm\]/g, m)
// .replace(/\[dd\]/g, d)
//
// return filename
// }
//
// private processPathCategory(savePath: string, cfg: CommonGitlabConfig) {
// let categories = []
// if (cfg.usePathCategory) {
// this.logger.debug("savePath=>", savePath)
// const docPathArray = savePath.split("/")
// if (docPathArray.length > 1) {
// for (let i = 1; i < docPathArray.length - 1; i++) {
// const docCate = HtmlUtil.removeTitleNumber(docPathArray[i])
// categories.push(docCate)
// }
// }
// }
//
// return categories
// }
}

export { CommonGithubApiAdaptor }
2 changes: 0 additions & 2 deletions src/adaptors/api/base/github/commonGithubConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ class CommonGithubConfig extends CommonBlogConfig {
this.email = "youweics@163.com"
this.site = StrUtil.pathJoin(this.home, "/" + this.username)
this.mdFilenameRule = "[filename].md"
this.useMdFilename = false
this.usePathCategory = false

this.middlewareUrl = middlewareUrl
}
Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/base/metaweblog/metaweblogConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,5 @@ export class MetaweblogConfig extends CommonBlogConfig {
this.categoryType = CategoryTypeEnum.CategoryType_Multi
this.allowCateChange = true
this.knowledgeSpaceEnabled = false
this.useMdFilename = false
this.usePathCategory = false
}
}
2 changes: 0 additions & 2 deletions src/adaptors/api/docsify/docsifyConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class DocsifyConfig extends CommonGithubConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Docsify 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-docsify/GitlabdocsifyConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class GitlabdocsifyConfig extends CommonGithubConfig {
this.placeholder.knowledgeSpaceReadonlyModeTip =
"Gitlab Docsify 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-hexo/gitlabhexoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class GitlabhexoConfig extends HexoConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Githubhexo 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-hexo/useGitlabhexoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const useGitlabhexoApi = async (key: string, newCfg?: GitlabhexoConfig) => {

// 文件规则
cfg.mdFilenameRule = "[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/api/gitlab-hugo/gitlabhugoApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GitlabhugoApiAdaptor extends CommonGitlabApiAdaptor {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// HEXO 自带的处理
// HUGO 自带的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-hugo/gitlabhugoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class GitlabhugoConfig extends HugoConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Githubhugo 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/adaptors/api/gitlab-hugo/useGitlabhugoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,8 @@ const useGitlabhugoApi = async (key: string, newCfg?: GitlabhugoConfig) => {

// 文件规则
cfg.mdFilenameRule = "[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
cfg.useMdFilename = false
cfg.usePathCategory = false
// 分类
cfg.cateEnabled = true
cfg.allowCateChange = true
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/api/gitlab-jekyll/gitlabjekyllApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GitlabjekyllApiAdaptor extends CommonGitlabApiAdaptor {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// HEXO 自带的处理
// Jekyll 自带的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-jekyll/gitlabjekyllConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ class GitlabjekyllConfig extends JekyllConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Gitlabjekyll 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-jekyll/useGitlabjekyllApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const useGitlabjekyllApi = async (key: string, newCfg?: GitlabjekyllConfig) => {

// 文件规则
cfg.mdFilenameRule = "[yyyy]-[mm]-[dd]-[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GitlabvitepressApiAdaptor extends CommonGitlabApiAdaptor {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// HEXO 自带的处理
// Vitepress 自带的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-vitepress/gitlabvitepressConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class GitlabvitepressConfig extends VitepressConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Gitlabvitepress 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-vitepress/useGitlabvitepressApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const useGitlabvitepressApi = async (key: string, newCfg?: GitlabvitepressConfig

// 文件规则
cfg.mdFilenameRule = "[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
49 changes: 2 additions & 47 deletions src/adaptors/api/gitlab-vuepress/gitlabvuepressApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import _ from "lodash"
import { GitlabvuepressYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-vuepress/gitlabvuepressYamlConverterAdaptor.ts"
import { YamlUtil } from "zhi-common"
import { SiyuanDevice } from "zhi-device"
import { CATE_AUTO_NAME } from "~/src/utils/constants.ts"

/**
* Hexo API 适配器
Expand All @@ -46,7 +47,7 @@ class GitlabvuepressApiAdaptor extends CommonGitlabApiAdaptor {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// HEXO 自带的处理
// Vuepress 自带的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post

Expand All @@ -66,18 +67,6 @@ class GitlabvuepressApiAdaptor extends CommonGitlabApiAdaptor {
}
// ======

// ======
// 自动推测保存路径
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 })
Expand All @@ -91,40 +80,6 @@ class GitlabvuepressApiAdaptor extends CommonGitlabApiAdaptor {

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 save_dir
}
}

export { GitlabvuepressApiAdaptor }
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-vuepress/gitlabvuepressConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class GitlabvuepressConfig extends VuepressConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Gitlabvuepress 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = true
this.usePathCategory = true
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-vuepress/useGitlabvuepressApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const useGitlabvuepressApi = async (key: string, newCfg?: GitlabvuepressConfig)

// 文件规则
cfg.mdFilenameRule = "[filename].md"
cfg.useMdFilename = true
cfg.usePathCategory = true
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Gitlabvuepress2ApiAdaptor extends CommonGitlabApiAdaptor {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// HEXO 自带的处理
// Vuepress2 自带的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-vuepress2/gitlabvuepress2Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class Gitlabvuepress2Config extends Vuepress2Config {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Gitlabvuepress2 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/gitlab-vuepress2/useGitlabvuepress2Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const useGitlabvuepress2Api = async (key: string, newCfg?: Gitlabvuepress2Config

// 文件规则
cfg.mdFilenameRule = "[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/hexo/hexoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class HexoConfig extends CommonGithubConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Hexo 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/hexo/useHexoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ const useHexoApi = async (key: string, newCfg?: HexoConfig) => {
// [mm] 月
// [dd] 日
cfg.mdFilenameRule = "[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/api/hugo/hugoApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class HugoApiAdaptor extends CommonGithubApiAdaptor {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// HEXO 自带的处理
// HUGO 自带的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/hugo/hugoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class HugoConfig extends CommonGithubConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Hugo 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/hugo/useHugoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const useHugoApi = async (key: string, newCfg?: HugoConfig) => {

// 文件规则
cfg.mdFilenameRule = "[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/api/jekyll/jekyllApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class JekyllApiAdaptor extends CommonGithubApiAdaptor {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// HEXO 自带的处理
// Jekyll 自带的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/jekyll/jekyllConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class JekyllConfig extends CommonGithubConfig {
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Jekyll 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
this.useMdFilename = false
this.usePathCategory = false
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/api/jekyll/useJekyllApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ const useJekyllApi = async (key: string, newCfg?: JekyllConfig) => {

// 文件规则
cfg.mdFilenameRule = "[yyyy]-[mm]-[dd]-[slug].md"
cfg.useMdFilename = false
cfg.usePathCategory = false
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
Loading

0 comments on commit 3787ed1

Please sign in to comment.