From a9df74e7c950c5b5f70fcf9e47d890b3baea5c18 Mon Sep 17 00:00:00 2001 From: terwer Date: Thu, 14 Sep 2023 15:30:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=A8=E9=9D=A2=E6=94=AF=E6=8C=81Git?= =?UTF-8?q?hub=E5=92=8CGitlab=E5=90=84=E5=B9=B3=E5=8F=B0-Vitepress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vitepressYamlConverterAdaptor.ts | 113 +++++++++--------- .../vuepress/vuepressYamlConverterAdaptor.ts | 4 +- 2 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/adaptors/api/vitepress/vitepressYamlConverterAdaptor.ts b/src/adaptors/api/vitepress/vitepressYamlConverterAdaptor.ts index 62cbe65b..d003e7ea 100644 --- a/src/adaptors/api/vitepress/vitepressYamlConverterAdaptor.ts +++ b/src/adaptors/api/vitepress/vitepressYamlConverterAdaptor.ts @@ -26,13 +26,12 @@ import { createAppLogger } from "~/src/utils/appLogger.ts" import { BlogConfig, Post, YamlConvertAdaptor, YamlFormatObj } from "zhi-blog-api" import { DateUtil, StrUtil, YamlUtil } from "zhi-common" -import { CommonGithubConfig } from "~/src/adaptors/api/base/github/commonGithubConfig.ts" import { toRaw } from "vue" /** * Vitepress 平台的YAML解析器 * - * @see {https://vitepress.io/docs/front-matter front-tmatter} + * @see {https://vitepress.dev/guide/frontmatter front-tmatter} * @author terwer * @since 0.8.1 */ @@ -45,65 +44,63 @@ class VitepressYamlConverterAdaptor extends YamlConvertAdaptor { // title yamlFormatObj.yamlObj.title = post.title - // date - yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true) - - // updated - if (!post.dateUpdated) { - post.dateUpdated = new Date() - } - yamlFormatObj.yamlObj.updated = DateUtil.formatIsoToZh(post.dateUpdated.toISOString(), true) + // titleTemplate + // yamlFormatObj.yamlObj.titleTemplate = post.title - // excerpt + // description if (!StrUtil.isEmptyString(post.shortDesc)) { - yamlFormatObj.yamlObj.excerpt = post.shortDesc + yamlFormatObj.yamlObj.description = post.shortDesc } - // tags - if (!StrUtil.isEmptyString(post.mt_keywords)) { - const tags = post.mt_keywords.split(",") - yamlFormatObj.yamlObj.tags = tags - } + // date + yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true) + + // head + yamlFormatObj.yamlObj.head = [ + [ + "meta", + { + name: "description", + content: post?.shortDesc ?? "", + }, + "meta", + { + name: "keywords", + content: post?.mt_keywords?.split(",").join(" "), + }, + ], + ] // categories if (post.categories?.length > 0) { yamlFormatObj.yamlObj.categories = post.categories } - // permalink - if (cfg.yamlLinkEnabled) { - let link = "/post/" + post.wp_slug + ".html" - if (cfg instanceof CommonGithubConfig) { - const githubCfg = cfg as CommonGithubConfig - if (!StrUtil.isEmptyString(cfg.previewPostUrl)) { - link = githubCfg.previewPostUrl.replace("[postid]", post.wp_slug) - const created = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true) - const datearr = created.split(" ")[0] - const numarr = datearr.split("-") - this.logger.debug("created numarr=>", numarr) - const y = numarr[0] - const m = numarr[1] - const d = numarr[2] - link = link.replace(/\[yyyy]/g, y) - link = link.replace(/\[MM]/g, m) - link = link.replace(/\[mm]/g, m) - link = link.replace(/\[dd]/g, d) - - if (yamlFormatObj.yamlObj.categories?.length > 0) { - link = link.replace(/\[cats]/, yamlFormatObj.yamlObj.categories.join("/")) - } else { - link = link.replace(/\/\[cats]/, "") - } - } - } - yamlFormatObj.yamlObj.permalink = link - } + // layout + // https://vitepress.dev/reference/frontmatter-config#layout + // yamlFormatObj.yamlObj.layout = "doc" - // comments - yamlFormatObj.yamlObj.comments = true + // // date + // yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true) - // toc - yamlFormatObj.yamlObj.toc = true + // outline + yamlFormatObj.yamlObj.outline = "deep" + + // navbar + // yamlFormatObj.yamlObj.navbar = true + + // sidebar + yamlFormatObj.yamlObj.sidebar = false + + // prev && next + yamlFormatObj.yamlObj.prev = false + yamlFormatObj.yamlObj.next = false + + // lastUpdated + // yamlFormatObj.yamlObj.lastUpdated= true + + // editLink + // yamlFormatObj.yamlObj.editLink = true // formatter let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj) @@ -130,15 +127,23 @@ class VitepressYamlConverterAdaptor extends YamlConvertAdaptor { if (yamlFormatObj.yamlObj?.date) { post.dateCreated = DateUtil.convertStringToDate(yamlFormatObj.yamlObj?.date) } - if (yamlFormatObj.yamlObj?.updated) { - post.dateUpdated = DateUtil.convertStringToDate(yamlFormatObj.yamlObj?.updated) - } // 摘要 - post.shortDesc = yamlFormatObj.yamlObj?.excerpt + if (yamlFormatObj.yamlObj?.description) { + post.shortDesc = yamlFormatObj.yamlObj.description + } // 标签 - post.mt_keywords = yamlFormatObj.yamlObj?.tags?.join(",") + const head = yamlFormatObj.yamlObj?.head + if (head && head.length == 4) { + for (let i = 0; i < head.length; i++) { + const m = head[i] + if (m?.name === "keywords" && !StrUtil.isEmptyString(m.content)) { + post.mt_keywords = m.content?.split(" ") + break + } + } + } // 分类 post.categories = yamlFormatObj.yamlObj?.categories diff --git a/src/adaptors/api/vuepress/vuepressYamlConverterAdaptor.ts b/src/adaptors/api/vuepress/vuepressYamlConverterAdaptor.ts index 5fdb1529..6f12858a 100644 --- a/src/adaptors/api/vuepress/vuepressYamlConverterAdaptor.ts +++ b/src/adaptors/api/vuepress/vuepressYamlConverterAdaptor.ts @@ -52,11 +52,11 @@ class VuepressYamlConverterAdaptor extends YamlConvertAdaptor { yamlFormatObj.yamlObj.meta = [ { name: "keywords", - content: post.mt_keywords, + content: post.mt_keywords.split(",").join(" "), }, { name: "description", - content: post.shortDesc, + content: post.shortDesc ?? "", }, ]