Skip to content

Commit

Permalink
feat: #133 支持Github系列-预定义平台导入
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Dec 14, 2022
1 parent a52bda9 commit 7eadae1
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 1 deletion.
13 changes: 12 additions & 1 deletion components/publish/tab/DynamicPlatform.vue
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ import {
} from "~/utils/platform/dynamicConfig"
import { LogFactory } from "~/utils/logUtil"
import { checkKeyExists } from "~/utils/configUtil"
import { importPreDefinedPlatform } from "~/utils/import/importUtil"
const logger = LogFactory.getLogger(
"components/publish/tab/DynamicPlatform.vue"
Expand Down Expand Up @@ -301,7 +302,11 @@ const submitForm = async (formEl) => {
formData.dynCfg.platformKey,
formData.dynCfg.platformName
)
newCfg.subPlatformType = formData.subtype
if (formData.ptype === PlatformType.Github) {
newCfg.subPlatformType = formData.subtype
} else {
newCfg.subPlatformType = SubPlatformType.NONE
}
formData.dynamicConfigArray.push(newCfg)
setDynamicJsonCfg(formData.dynamicConfigArray)
Expand Down Expand Up @@ -386,6 +391,12 @@ const initPage = async () => {
}
onMounted(async () => {
// 导入配置
logger.info("开始导入预定义平台...")
importPreDefinedPlatform()
logger.info("导入预定义平台成功.")
// 初始化
await initPage()
})
</script>
Expand Down
147 changes: 147 additions & 0 deletions utils/import/importUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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 {
DynamicConfig,
getDynamicJsonCfg,
isDynamicKeyExists,
PlatformType,
setDynamicJsonCfg,
SubPlatformType,
} from "~/utils/platform/dynamicConfig"
import { checkKeyExists } from "~/utils/configUtil"
import * as pre from "./pre.json"
import { LogFactory } from "~/utils/logUtil"

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

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

// 保证开关变量key不重复
const switchKey = "switch-" + pkey
const postidKey = "custom-" + pkey + "-post-id"
// 保证文章绑定id的key不重复
return !(
checkKeyExists(pkey) ||
checkKeyExists(switchKey) ||
checkKeyExists(postidKey)
)
}

/**
* 生成新平台
* @param ptype
* @param platformKey
* @param platformName
* @param subtype
*/
const genNewPlatform = (
ptype: PlatformType,
platformKey: string,
platformName: string,
subtype?: SubPlatformType
) => {
const newCfg = new DynamicConfig(ptype, platformKey, platformName)
newCfg.subPlatformType = subtype
return newCfg
}

/**
* 导入预定义平台
*/
export const importPreDefinedPlatform = () => {
const dynamicConfigArray = getDynamicJsonCfg().totalCfg || []

// github
if (pre.githubCfg && pre.githubCfg.length > 0) {
pre.githubCfg.forEach((gcfg) => {
// 防止重复导入
if (checkPlatform(gcfg.platformKey, dynamicConfigArray)) {
const newCfg = genNewPlatform(
PlatformType[gcfg.platformType],
gcfg.platformKey,
gcfg.platformName,
SubPlatformType[gcfg.subPlatformType]
)

dynamicConfigArray.push(newCfg)

// const switchKey = "switch-" + gcfg.platformType
// setBooleanConf(switchKey, false)
}
})
}

// metaweblog
if (pre.metaweblogCfg && pre.metaweblogCfg.length > 0) {
pre.metaweblogCfg.forEach((mcfg) => {
// 防止重复导入
if (checkPlatform(mcfg.platformKey, dynamicConfigArray)) {
const newCfg = genNewPlatform(
PlatformType[mcfg.platformType],
mcfg.platformKey,
mcfg.platformName,
SubPlatformType.NONE
)

dynamicConfigArray.push(newCfg)

// const switchKey = "switch-" + gcfg.platformType
// setBooleanConf(switchKey, false)
}
})
}

// WordPress
if (pre.wordpressCfg && pre.wordpressCfg.length > 0) {
pre.wordpressCfg.forEach((wcfg) => {
// 防止重复导入
if (checkPlatform(wcfg.platformKey, dynamicConfigArray)) {
const newCfg = genNewPlatform(
PlatformType[wcfg.platformType],
wcfg.platformKey,
wcfg.platformName,
SubPlatformType.NONE
)

dynamicConfigArray.push(newCfg)

// const switchKey = "switch-" + gcfg.platformType
// setBooleanConf(switchKey, false)
}
})
}

logger.debug("将要导入预定义平台:", dynamicConfigArray)
setDynamicJsonCfg(dynamicConfigArray)
}
37 changes: 37 additions & 0 deletions utils/import/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"githubCfg": [
{
"platformType": "Github",
"subPlatformType": "Github_Vitepress",
"platformKey": "githubVitepress-mhxj3",
"platformName": "Vitepress"
},
{
"platformType": "Github",
"subPlatformType": "Github_Nuxt",
"platformKey": "githubNuxt-z1xcb7x",
"platformName": "Nuxt content"
},
{
"platformType": "Github",
"subPlatformType": "Github_Next",
"platformKey": "githubNext-ziz9v8",
"platformName": "Next.js"
}
],
"metaweblogCfg": [
{
"platformType": "Metaweblog",
"subPlatformType": "none",
"platformKey": "metaweblog-zahrlw",
"platformName": "开源中国"
},
{
"platformType": "Metaweblog",
"subPlatformType": "none",
"platformKey": "metaweblog-22pamt",
"platformName": "Typeecho"
}
],
"wordpressCfg": []
}
5 changes: 5 additions & 0 deletions utils/platform/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ export const getDynYamlConverterAdaptor = (
yamlConverter = new HexoYamlConverterAdaptor()
} else if (ptype.includes(SubPlatformType.Github_Jekyll.toLowerCase())) {
yamlConverter = new JekyllYamlConverterAdaptor()
} else if (
ptype.includes(SubPlatformType.Github_Vitepress.toLowerCase())
) {
} else if (ptype.includes(SubPlatformType.Github_Nuxt.toLowerCase())) {
} else if (ptype.includes(SubPlatformType.Github_Next.toLowerCase())) {
}
}
}
Expand Down

0 comments on commit 7eadae1

Please sign in to comment.