Skip to content

Commit

Permalink
fix: #309 深度适配Typecho
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jan 8, 2023
1 parent 6e7f76c commit 12c8e44
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
64 changes: 64 additions & 0 deletions utils/import/PRE_DEFINED_PLATFORM_CONSTANTS.ts
@@ -0,0 +1,64 @@
/*
* Copyright (c) 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, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

/**
* Docsy
*/
const PRE_DEFINED_DOCSY_KEY = "githubHugo-zdlc6l"
/**
* Vitepress
*/
const PRE_DEFINED_VITEPRESS_KEY = "githubVitepress-mhxj3"
/**
* Nuxt content
*/
const PRE_DEFINED_NUXT_CONTENT_KEY = "githubNuxt-z1xcb7x"
/**
* Nextra
*/
const PRE_DEFINED_NEXTRA_KEY = "githubNext-ziz9v8"
/**
* Oschina
*/
const PRE_DEFINED_OSCHINA_KEY = "metaweblog-zahrlw"
/**
* Typecho
*/
const PRE_DEFINED_TYPECHO_KEY = "metaweblog-22pamt"

/**
* 预定义平台的 key ,请勿修改,否则将导致此平台无法运行
* 注意:这里的值必须与 pre.json 完全保持一致
* @author terwer
* @since 0.6.1
*/
export const PRE_DEFINED_PLATFORM_KEY_CONSTANTS = {
PRE_DEFINED_DOCSY_KEY,
PRE_DEFINED_VITEPRESS_KEY,
PRE_DEFINED_NUXT_CONTENT_KEY,
PRE_DEFINED_NEXTRA_KEY,
PRE_DEFINED_OSCHINA_KEY,
PRE_DEFINED_TYPECHO_KEY,
}
48 changes: 47 additions & 1 deletion utils/import/importUtil.ts
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Terwer . All rights reserved.
* 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
Expand Down Expand Up @@ -34,16 +34,27 @@ import {
import { checkKeyExists } from "~/utils/configUtil"
import * as pre from "./pre.json"
import { LogFactory } from "~/utils/logUtil"
import { PRE_DEFINED_PLATFORM_KEY_CONSTANTS } from "~/utils/import/PRE_DEFINED_PLATFORM_CONSTANTS"

const logger = LogFactory.getLogger("utils/import/importUtil.ts")

// 需要修正平台配置信息的key(注意:只能修正不影响后续使用的值,例如显示名称等,其他值请勿修复)
// 修复信息请在 pre.json 里面定义
const fixPlatformArray = [
PRE_DEFINED_PLATFORM_KEY_CONSTANTS.PRE_DEFINED_DOCSY_KEY,
PRE_DEFINED_PLATFORM_KEY_CONSTANTS.PRE_DEFINED_TYPECHO_KEY,
]

/**
* 检测唯一性
* @param pkey
* @param dynamicConfigArray
*/
const checkPlatform = (pkey: string, dynamicConfigArray: DynamicConfig[]) => {
if (isDynamicKeyExists(dynamicConfigArray, pkey)) {
if (fixPlatformArray.includes(pkey)) {
doFixPlatformInfo(pkey, dynamicConfigArray)
}
return false
}

Expand Down Expand Up @@ -145,3 +156,38 @@ export const importPreDefinedPlatform = () => {
logger.debug("将要导入预定义平台:", dynamicConfigArray)
setDynamicJsonCfg(dynamicConfigArray)
}

/**
* 修复之前错误预置的信息,例如显示名称等,不可修复 key 等关键信息
* @param pkey 平台key
* @param dynamicConfigArray totalCfg
*/
const doFixPlatformInfo = (
pkey: string,
dynamicConfigArray: DynamicConfig[]
) => {
for (let i = 0; i < dynamicConfigArray.length; i++) {
if (dynamicConfigArray[i].platformKey !== pkey) {
continue
}

// 修复 Typecho 的拼写错误
const pcfg = dynamicConfigArray[i]
logger.warn("pcfg=>", pcfg)
if (PRE_DEFINED_PLATFORM_KEY_CONSTANTS.PRE_DEFINED_TYPECHO_KEY === pkey) {
if (pcfg.platformName === "Typeecho") {
pcfg.platformName = "Typecho"
logger.warn("Typeecho 已经修正为=>", pcfg.platformName)
}
}

// 修复 Docsy 名称
if (PRE_DEFINED_PLATFORM_KEY_CONSTANTS.PRE_DEFINED_DOCSY_KEY == pkey) {
if (pcfg.platformName !== "Docsy") {
pcfg.platformName = "Docsy"

logger.warn("Docsy 已经修正为=>", pcfg.platformName)
}
}
}
}
6 changes: 6 additions & 0 deletions utils/import/pre.json
@@ -1,5 +1,11 @@
{
"githubCfg": [
{
"platformType": "Github",
"subPlatformType": "Github_Hugo",
"platformKey": "githubHugo-zdlc6l",
"platformName": "Docsy"
},
{
"platformType": "Github",
"subPlatformType": "Github_Vitepress",
Expand Down

0 comments on commit 12c8e44

Please sign in to comment.