Skip to content

Commit

Permalink
feat: #133 支持Github系列-支持Hexo平台
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Dec 14, 2022
1 parent bdfe99f commit a52bda9
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 9 deletions.
4 changes: 4 additions & 0 deletions components/publish/tab/main/github/HexoMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
:is-reload="props.isReload"
:is-main-reload="props.isMainReload"
:page-id="props.pageId"
:yaml-converter="yamlConverter"
/>
</template>
<script lang="ts" setup>
import GithubMain from "~/components/publish/tab/main/GithubMain.vue"
import { API_TYPE_CONSTANTS } from "~/utils/constants/apiTypeConstants"
import { ref } from "vue"
import { HexoYamlConverterAdaptor } from "~/utils/platform/github/hexo/hexoYamlConverterAdaptor"
const apiType = ref(API_TYPE_CONSTANTS.API_TYPE_HEXO)
const hexoYamlConverterAdaptor = new HexoYamlConverterAdaptor()
const yamlConverter = ref(hexoYamlConverterAdaptor)
const props = defineProps({
isReload: {
Expand Down
14 changes: 7 additions & 7 deletions composables/publish/publishActionCom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export const usePublish = (props, deps?: any) => {
const api = new GithubApi(githubCfg)

// 先删除
// if (initPublishMethods.getInitPublishData().isPublished) {
// try {
// await publishMethods.doCancel(false)
// } catch (e) {
// logger.error("强制删除异常,不影响发布=>", e)
// }
// }
if (initPublishMethods.getInitPublishData().isPublished) {
try {
await publishMethods.doCancel(false)
} catch (e) {
logger.error("强制删除异常,不影响发布=>", e)
}
}

// 校验标题
const mdTitle = githubPagesMethods.getGithubPagesData().mdTitle
Expand Down
2 changes: 2 additions & 0 deletions utils/platform/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { LogFactory } from "~/utils/logUtil"
import { VuepressYamlConvertAdaptor } from "~/utils/platform/github/vuepress/VuepressYamlConvertAdaptor"
import { JekyllYamlConverterAdaptor } from "~/utils/platform/github/jekyll/JekyllYamlConverterAdaptor"
import { HugoYamlConverterAdaptor } from "~/utils/platform/github/hugo/HugoYamlConverterAdaptor"
import { HexoYamlConverterAdaptor } from "~/utils/platform/github/hexo/hexoYamlConverterAdaptor"

const logger = LogFactory.getLogger("utils/platform/dynamicConfig.ts")

Expand Down Expand Up @@ -347,6 +348,7 @@ export const getDynYamlConverterAdaptor = (
} else if (ptype.includes(SubPlatformType.Github_Hugo.toLowerCase())) {
yamlConverter = new HugoYamlConverterAdaptor()
} else if (ptype.includes(SubPlatformType.Github_Hexo.toLowerCase())) {
yamlConverter = new HexoYamlConverterAdaptor()
} else if (ptype.includes(SubPlatformType.Github_Jekyll.toLowerCase())) {
yamlConverter = new JekyllYamlConverterAdaptor()
}
Expand Down
3 changes: 2 additions & 1 deletion utils/platform/github/hexo/hexoCfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export class HexoCfg extends GithubCfg {
this.defaultPath = "source/_posts"
this.posidKey = POSTID_KEY_CONSTANTS.HEXO_POSTID_KEY
this.previewMdUrl = "/[user]/[repo]/blob/[branch]/[docpath]"
this.previewUrl = "/[date]/[postid]/"
// this.previewUrl = "[yyyy]/[mm]/[dd]/[postid]/"
this.previewUrl = "/post/[postid].html"
this.mdFilenameRule = "[slug].md"
}
}
129 changes: 129 additions & 0 deletions utils/platform/github/hexo/hexoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2022, 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, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import {
IYamlConvertAdaptor,
YamlConvertAdaptor,
} from "~/utils/platform/yamlConvertAdaptor"
import { PostForm } from "~/utils/models/postForm"
import { IGithubCfg } from "~/utils/platform/github/githubCfg"
import { YamlFormatObj } from "~/utils/models/yamlFormatObj"
import { LogFactory } from "~/utils/logUtil"
import { obj2Yaml } from "~/utils/yamlUtil"
import { covertStringToDate, formatIsoToZhDate } from "~/utils/dateUtil"
import { isEmptyString } from "~/utils/util"

/**
* Hexo平台的YAML解析器
*/
export class HexoYamlConverterAdaptor
extends YamlConvertAdaptor
implements IYamlConvertAdaptor
{
private readonly logger = LogFactory.getLogger(
"utils/platform/github/hexo/hexoYamlConverterAdaptor.ts"
)

convertToYaml(postForm: PostForm, githubCfg?: IGithubCfg): YamlFormatObj {
let yamlFormatObj: YamlFormatObj = new YamlFormatObj()
this.logger.debug("您正在使用 Hexo Yaml Converter", postForm)

// title
yamlFormatObj.yamlObj.title = postForm.formData.title

// date
yamlFormatObj.yamlObj.date = postForm.formData.created

// updated
yamlFormatObj.yamlObj.updated = formatIsoToZhDate(
new Date().toISOString(),
true
)

// excerpt
yamlFormatObj.yamlObj.excerpt = postForm.formData.desc

// tags
yamlFormatObj.yamlObj.tags = postForm.formData.tag.dynamicTags

// categories
yamlFormatObj.yamlObj.categories = postForm.formData.categories

// permalink
let link = "/post/" + postForm.formData.customSlug + ".html"
if (githubCfg && !isEmptyString(githubCfg.previewUrl)) {
link = githubCfg.previewUrl.replace(
"[postid]",
postForm.formData.customSlug
)

const created = postForm.formData.created
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

// comments
yamlFormatObj.yamlObj.comments = true

// formatter
let yaml = obj2Yaml(yamlFormatObj.yamlObj)
// 修复yaml的ISO日期格式(js-yaml转换的才需要)
yaml = formatIsoToZhDate(yaml, true)
// this.logger.debug("yaml=>", yaml)

yamlFormatObj.formatter = yaml
yamlFormatObj.mdContent = postForm.formData.mdContent
yamlFormatObj.mdFullContent =
yamlFormatObj.formatter + "\n\n" + yamlFormatObj.mdContent
yamlFormatObj.htmlContent = postForm.formData.htmlContent

return yamlFormatObj
}

convertToAttr(
yamlFormatObj: YamlFormatObj,
githubCfg?: IGithubCfg
): PostForm {
return super.convertToAttr(yamlFormatObj, githubCfg)
}
}
3 changes: 2 additions & 1 deletion utils/platform/github/jekyll/jekyllCfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export class JekyllCfg extends GithubCfg {
this.defaultPath = "_posts"
this.posidKey = POSTID_KEY_CONSTANTS.JEKYLL_POSTID_KEY
this.previewMdUrl = "/[user]/[repo]/blob/[branch]/[docpath]"
this.previewUrl = "/[cats]/[yyyy]/[mm]/[dd]/[postid].html"
// this.previewUrl = "/[cats]/[yyyy]/[mm]/[dd]/[postid].html"
this.previewUrl = "/post/[postid].html"
this.mdFilenameRule = "[yyyy]-[mm]-[dd]-[slug].md"
}
}

0 comments on commit a52bda9

Please sign in to comment.