-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
186 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) 2022-2023, 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, [email protected] | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { YamlConvertAdaptor } from "~/src/platforms/yamlConvertAdaptor.ts" | ||
import { CommonGithubConfig } from "~/src/adaptors/api/base/github/CommonGithubConfig.ts" | ||
import { createAppLogger } from "~/src/utils/appLogger.ts" | ||
import { YamlFormatObj } from "~/src/models/yamlFormatObj.ts" | ||
import { DateUtil, StrUtil, YamlUtil } from "zhi-common" | ||
import { PostForm } from "~/src/models/postForm.ts" | ||
|
||
/** | ||
* Hexo平台的YAML解析器 | ||
* | ||
* @author terwer | ||
* @since 0.8.1 | ||
*/ | ||
export class HexoYamlConverterAdaptor extends YamlConvertAdaptor { | ||
private readonly logger = createAppLogger("hexo-yaml-converter-adaptor") | ||
|
||
convertToYaml(postForm: PostForm, githubCfg?: CommonGithubConfig): 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 = DateUtil.formatIsoToZhDate(new Date().toISOString()) | ||
|
||
// 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 && !StrUtil.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 | ||
|
||
// toc | ||
yamlFormatObj.yamlObj.toc = true | ||
|
||
// formatter | ||
let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj) | ||
// 修复yaml的ISO日期格式(js-yaml转换的才需要) | ||
yaml = DateUtil.formatIsoToZhDate(yaml) | ||
// 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?: CommonGithubConfig): PostForm { | ||
return super.convertToAttr(yamlFormatObj, githubCfg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters