From 6ab25041794a4005ef31eaa0b83e5db78d728c8d Mon Sep 17 00:00:00 2001 From: terwer Date: Fri, 9 Dec 2022 00:55:14 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=BB=84=E4=BB=B6=E5=8C=96=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.json | 2 +- components/detail/PostDetail.vue | 6 +- components/publish/tab/ChangeLocale.vue | 12 - components/publish/tab/PlatformMain.vue | 7 +- components/publish/tab/ServiceSwitch.vue | 1 - components/publish/tab/main/GithubMain.vue | 1157 +---------------- composables/makeSlugCom.ts | 149 +++ composables/tabCountCom.ts | 5 +- layouts/default/DefaultFooter.vue | 4 +- locales/zh_CN.ts | 2 +- plugins/vue-hljs/vue-hljs.js | 2 +- .../publish/tab/main/githubMain.test.ts | 40 + utils/common/publishPreference.ts | 55 + utils/configUtil.ts | 4 +- utils/constants/constants.ts | 6 + utils/htmlUtil.ts | 18 +- utils/logUtil.ts | 11 +- utils/parser/imageParser.ts | 4 +- utils/platform/commonblog/kms/kmsApi.ts | 6 +- utils/platform/commonblog/yuque/yuqueApi.ts | 4 +- utils/platform/dynamicConfig.ts | 6 +- .../metaweblog/CustomMetaweblogApi.ts | 4 +- utils/platform/siyuan/siYuanApi.ts | 4 +- utils/platform/siyuan/siYuanApiAdaptor.ts | 10 +- utils/publishUtil.ts | 35 + utils/strUtil.ts | 16 +- utils/util.ts | 1 + utils/yamlUtil.ts | 4 +- 28 files changed, 403 insertions(+), 1172 deletions(-) create mode 100644 composables/makeSlugCom.ts create mode 100644 test/components/publish/tab/main/githubMain.test.ts create mode 100644 utils/common/publishPreference.ts diff --git a/.eslintrc.json b/.eslintrc.json index 5aea7ba3..fa1b3c0f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -27,7 +27,7 @@ "@typescript-eslint/no-var-requires": 1, "no-undef": 0, "no-undef-init": 1, - "no-undefined": 1, + "no-undefined": 0, "no-prototype-builtins": 1, "no-unused-expressions": 1 } diff --git a/components/detail/PostDetail.vue b/components/detail/PostDetail.vue index 7860471f..f8705fea 100644 --- a/components/detail/PostDetail.vue +++ b/components/detail/PostDetail.vue @@ -40,7 +40,7 @@ import { onMounted, ref } from "vue" import { getPageId, getWidgetId } from "~/utils/platform/siyuan/siyuanUtil" import { goToPage } from "~/utils/otherlib/ChromeUtil" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" import PostDetailService from "~/components/detail/PostDetailService.vue" import { LogFactory } from "~/utils/logUtil" @@ -58,9 +58,9 @@ const pid = ref("") const handlePublish = async () => { const widgetResult = getWidgetId() if (widgetResult.isInSiyuan) { - goToPage(appandStr("/index.html?id=", pid.value)) + goToPage(appendStr("/index.html?id=", pid.value)) } else { - goToPage(appandStr("/publish/index.html?id=", pid.value)) + goToPage(appendStr("/publish/index.html?id=", pid.value)) } } diff --git a/components/publish/tab/ChangeLocale.vue b/components/publish/tab/ChangeLocale.vue index 7b248c06..9e6c1c3f 100644 --- a/components/publish/tab/ChangeLocale.vue +++ b/components/publish/tab/ChangeLocale.vue @@ -41,13 +41,6 @@ /> - - - - - {{ isDark ? $t("theme.mode.light") : $t("theme.mode.dark") }} - - @@ -55,7 +48,6 @@ @@ -1197,28 +152,4 @@ onBeforeMount(async () => { .github-main { padding: 0; } - -.source-opt { - margin-bottom: 10px; -} - -.source-opt a { - padding: 4px 2px; - cursor: pointer; - /*border: solid 1px #409eff;*/ -} - -.source-opt a.middle { - margin: 0 12px; -} - -.source-opt a.current { - background-color: #409eff; - color: #ffffff; -} - -.top-yaml-tip { - padding: 2px 4px; - margin: 0 10px 0 0; -} diff --git a/composables/makeSlugCom.ts b/composables/makeSlugCom.ts new file mode 100644 index 00000000..0857a1f6 --- /dev/null +++ b/composables/makeSlugCom.ts @@ -0,0 +1,149 @@ +/* + * 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 { reactive } from "vue" +import { ElMessage } from "element-plus" +import { appendStr, mdFileToTitle } from "~/utils/strUtil" +import { isEmptyString, pingyinSlugify, zhSlugify } from "~/utils/util" +import shortHash from "shorthash2" +import { useI18n } from "vue-i18n" +import { SiYuanApi } from "~/utils/platform/siyuan/siYuanApi" +import { LogFactory } from "~/utils/logUtil" +import { getPublishCfg } from "~/utils/publishUtil" +import { getPageId } from "~/utils/platform/siyuan/siyuanUtil" +import { SIYUAN_PAGE_ATTR_KEY } from "~/utils/constants/siyuanPageConstants" + +/** + * 文章别名组件 + * @param defaultPageId 默认页面ID + * @param siyuanApi 思源Api + */ +export const useSlug = (defaultPageId: string, siyuanApi: SiYuanApi) => { + const logger = LogFactory.getLogger() + const { t } = useI18n() + const slugData = reactive({ + isSlugLoading: false, + forceRefresh: false, + customSlug: "", + slugHashEnabled: true, + }) + + function checkForce() { + // 空值跳过 + if (isEmptyString(slugData.customSlug)) { + return true + } + + // 别名不为空,根据用户操作判断 + return slugData.forceRefresh + } + + async function makeSlug(hideTip?: any) { + if (!checkForce()) { + if (hideTip !== true) { + ElMessage.warning(t("main.force.refresh.tip")) + } + return + } + + slugData.isSlugLoading = true + + try { + const publishCfg = getPublishCfg() + // 获取最新属性 + const pageId = await getPageId(true, defaultPageId) + if (!pageId || pageId === "") { + return + } + const page = await siyuanApi.getBlockByID(pageId) + + // 标题处理 + let fmtTitle = page.content + if (publishCfg.fixTitle) { + fmtTitle = mdFileToTitle(fmtTitle) + } + + // 生成别名 + if (publishCfg.useGoogleTranslate) { + // 先调用谷歌API,如果失败就用拼音代替 + try { + // 调用Google翻译API + const result = await zhSlugify(fmtTitle) + if (result) { + slugData.customSlug = result + } else { + slugData.customSlug = await pingyinSlugify(fmtTitle) + ElMessage.success(t("main.opt.failure")) + } + } catch (e) { + slugData.customSlug = await pingyinSlugify(fmtTitle) + } + } else { + slugData.customSlug = await pingyinSlugify(fmtTitle) + } + + // 添加hash + if (slugData.slugHashEnabled) { + const newstr = page.content + new Date().toISOString() + const hashstr = appendStr("-", shortHash(newstr).toLowerCase()) + slugData.customSlug += hashstr + } + + // 保存别名属性到思源 + const customAttr = { + [SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_SLUG_KEY]: + slugData.customSlug, + } + await siyuanApi.setBlockAttrs(pageId, customAttr) + + if (hideTip !== true) { + ElMessage.success(t("main.opt.success")) + } + } catch (e) { + if (hideTip !== true) { + const errmsg = appendStr(t("main.opt.failure"), "=>", e) + logger.error(errmsg) + ElMessage.error(errmsg) + } + } + + // 结束 + slugData.isSlugLoading = false + } + + /** + * 初始化 + * @param slug + */ + function initSlug(slug: string) { + slugData.customSlug = slug + } + + return { + slugData, + makeSlug, + initSlug, + } +} diff --git a/composables/tabCountCom.ts b/composables/tabCountCom.ts index 62da2b60..e786fb1e 100644 --- a/composables/tabCountCom.ts +++ b/composables/tabCountCom.ts @@ -34,7 +34,10 @@ import { import { getBooleanConf } from "~/utils/configUtil" import SWITCH_CONSTANTS from "~/utils/constants/switchConstants" -export function useTabCount(): any { +/** + * 计算使用的平台数 + */ +export const useTabCount = (): any => { const tabCountStore = useTabCountStore() // Github diff --git a/layouts/default/DefaultFooter.vue b/layouts/default/DefaultFooter.vue index cbe50162..b5448f61 100644 --- a/layouts/default/DefaultFooter.vue +++ b/layouts/default/DefaultFooter.vue @@ -279,9 +279,9 @@ const initConf = () => { onMounted(() => { if (inSiyuan()) { - logger.warn("恭喜你,正在以挂件模式运行") + logger.info("恭喜你,正在以挂件模式运行") } else { - logger.warn( + logger.info( "正在以非挂件模式运行,部分功能将通过请求代理的方式进行模拟,请知悉" ) } diff --git a/locales/zh_CN.ts b/locales/zh_CN.ts index 87469b19..3453b623 100644 --- a/locales/zh_CN.ts +++ b/locales/zh_CN.ts @@ -67,7 +67,7 @@ export default { "不使用hash会展示更有意义的url,但是可能导致文章别名重复(后果是Vuepress会Build失败除非手动解决别名唯一问题),请知悉。", "main.force.refresh": "强制刷新", "main.force.refresh.tip": - "为了防止误操作更新别名,别名不为空的时候默认不会更新别名,如需更新,请勾选。", + "为了防止误操作更新别名,别名不为空的时候默认不会更新别名,如需更新,请勾选强制刷新。", "main.desc": "文章摘要", "main.auto.fetch.desc": "自动提取摘要", "main.create.time": "发布时间", diff --git a/plugins/vue-hljs/vue-hljs.js b/plugins/vue-hljs/vue-hljs.js index 9c764ef0..1dc2c59a 100644 --- a/plugins/vue-hljs/vue-hljs.js +++ b/plugins/vue-hljs/vue-hljs.js @@ -29,4 +29,4 @@ * @version 1.0.0 * 2019/3/21 12:02 */ -console.log("Register vue-hljs success") +// console.log("Register vue-hljs success") diff --git a/test/components/publish/tab/main/githubMain.test.ts b/test/components/publish/tab/main/githubMain.test.ts new file mode 100644 index 00000000..991fd132 --- /dev/null +++ b/test/components/publish/tab/main/githubMain.test.ts @@ -0,0 +1,40 @@ +/* + * 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 { describe } from "vitest" +import { shallowMount } from "@vue/test-utils" +import githubMain from "~/components/publish/tab/main/GithubMain.vue" +import { LogFactory } from "~/utils/logUtil" + +describe("GithubMain test", () => { + const logger = LogFactory.getLogger() + + it("mount component test", async () => { + const wrapper = await shallowMount(githubMain, {}) + const result = wrapper.html() + logger.info("GithubMain HTML=>", result) + expect(result).toContain("div") + }) +}) diff --git a/utils/common/publishPreference.ts b/utils/common/publishPreference.ts new file mode 100644 index 00000000..78c0143a --- /dev/null +++ b/utils/common/publishPreference.ts @@ -0,0 +1,55 @@ +/* + * 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 { PageEditMode } from "~/utils/common/pageEditMode" +import { SourceContentShowType } from "~/utils/common/sourceContentShowType" + +/** + * 发布偏好设置 + */ +export class PublishPreference { + /** + * 精简模式、源码模式 + */ + editMode: PageEditMode + + /** + * 默认展示形式,HTML、MD、YAML等 + */ + contentShowType: SourceContentShowType + + /** + * 是否处理标题(包括去除.md等后缀,去除数字编号) + */ + fixTitle: boolean + /** + * 是否使用谷歌翻译,不使用将直接用拼音代替 + */ + useGoogleTranslate: boolean + /** + * 是否删除H1标签 + */ + removeH1: boolean +} diff --git a/utils/configUtil.ts b/utils/configUtil.ts index 987a221b..4af8ffb8 100644 --- a/utils/configUtil.ts +++ b/utils/configUtil.ts @@ -26,7 +26,7 @@ import { LogFactory } from "~/utils/logUtil" import { importJSONToLocalStorage } from "~/utils/otherlib/ChromeUtil" import { ElMessage } from "element-plus" -import { appandStr } from "./strUtil" +import { appendStr } from "./strUtil" import { isBrowser, reloadPage } from "~/utils/browserUtil" const logger = LogFactory.getLogger() @@ -222,7 +222,7 @@ export const importConf = async (): Promise => { logger.warn("您取消了导入=>", e) } else { logger.error("配置文件解析错误=>", e) - ElMessage.error(appandStr("配置文件解析错误=>", e)) + ElMessage.error(appendStr("配置文件解析错误=>", e)) } } } diff --git a/utils/constants/constants.ts b/utils/constants/constants.ts index b06f5b9f..a5aed77f 100644 --- a/utils/constants/constants.ts +++ b/utils/constants/constants.ts @@ -38,6 +38,11 @@ const MAX_PREVIEW_LENGTH = 255 */ const DYNAMIC_CONFIG_KEY = "dynamic-config" +/** + * 发布偏好设置,全系统唯一,请勿更改 + */ +const PUBLISH_PREFERENCE_CONFIG_KEY = "publish-preference" + /** * 分词最大数目 */ @@ -48,4 +53,5 @@ export const CONSTANTS = { MAX_PREVIEW_LENGTH, DYNAMIC_CONFIG_KEY, DEFAULT_JIEBA_WORD_LENGTH, + PUBLISH_PREFERENCE_CONFIG_KEY, } diff --git a/utils/htmlUtil.ts b/utils/htmlUtil.ts index fe154b47..2de927bf 100644 --- a/utils/htmlUtil.ts +++ b/utils/htmlUtil.ts @@ -46,7 +46,6 @@ export const removeTitleNumber = (str: string): string => { */ export const removeWidgetTag = (str: string): string => { let newstr = str.toString() - // logUtil.logWarn("newstr=>", newstr) // 旧版发布挂件 const publisherRegex = //g @@ -61,9 +60,6 @@ export const removeWidgetTag = (str: string): string => { const noteAttrRegex = //g newstr = newstr.replace(noteAttrRegex, "") - const h1Regex = //g - newstr = newstr.replace(h1Regex, "") - return newstr } @@ -166,6 +162,18 @@ export const mdToHtml = (md: string): string => { */ export const mdToPlainText = (md: string): string => { let html = mdToHtml(md) - html = removeWidgetTag(html) return filterHtml(html) } + +/** + * 移除H1标签 + * @param html + */ +export const removeH1 = (html: string): string => { + let newstr = html + + const h1Regex = //g + newstr = newstr.replace(h1Regex, "") + + return newstr +} diff --git a/utils/logUtil.ts b/utils/logUtil.ts index d7ae4ffa..5c01cbcb 100644 --- a/utils/logUtil.ts +++ b/utils/logUtil.ts @@ -28,7 +28,6 @@ import { getBooleanEnv } from "./envUtil" import loglevel, { Logger } from "loglevel" import prefix from "loglevel-plugin-prefix" -import { isBrowser } from "~/utils/browserUtil" // import chalk from "chalk" // polyfill due to https://github.com/vitejs/vite/issues/7385 @@ -47,11 +46,11 @@ const chalk = { }, } -if (isBrowser()) { - console.log("loglevel运行在浏览器环境中") -} else { - console.log("loglevel运行在node环境中") -} +// if (isBrowser()) { +// console.log("loglevel运行在浏览器环境中") +// } else { +// console.log("loglevel运行在node环境中") +// } const LOG_INFO_ENABLED = getBooleanEnv("VITE_LOG_INFO_ENABLED") ?? false const LOG_LEVEL_INFO = "INFO" diff --git a/utils/parser/imageParser.ts b/utils/parser/imageParser.ts index d5ddb2bb..335bbef8 100644 --- a/utils/parser/imageParser.ts +++ b/utils/parser/imageParser.ts @@ -27,7 +27,7 @@ import { LogFactory } from "~/utils/logUtil" import { inSiyuan } from "~/utils/platform/siyuan/siyuanUtil" import { imageToBase64 } from "~/utils/parser/imageToBase64" import { getEnvOrDefault } from "~/utils/envUtil" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" /** * 图片解析器 @@ -124,7 +124,7 @@ export class ImageParser { newImg = resJson?.body?.base64str ?? "parse error" } - newImg = appandStr( + newImg = appendStr( 'base64Image' diff --git a/utils/platform/commonblog/kms/kmsApi.ts b/utils/platform/commonblog/kms/kmsApi.ts index 0f57d7fb..ea88e0dd 100644 --- a/utils/platform/commonblog/kms/kmsApi.ts +++ b/utils/platform/commonblog/kms/kmsApi.ts @@ -24,7 +24,7 @@ */ import { CommonblogApi } from "../commonblogApi" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" /** * KMS的API @@ -187,7 +187,7 @@ export class KmsApi extends CommonblogApi { if (statusCode !== 200) { if (statusCode === 401) { - throw new Error(appandStr("因权限不足操作已被禁止:", msg)) + throw new Error(appendStr("因权限不足操作已被禁止:", msg)) } else { throw new Error("请求错误") } @@ -225,7 +225,7 @@ export class KmsApi extends CommonblogApi { if (statusCode !== 200) { if (statusCode === 401) { - throw new Error(appandStr("因权限不足操作已被禁止:", msg)) + throw new Error(appendStr("因权限不足操作已被禁止:", msg)) } else { throw new Error("请求错误") } diff --git a/utils/platform/commonblog/yuque/yuqueApi.ts b/utils/platform/commonblog/yuque/yuqueApi.ts index 1cc3f58a..f0c44a5c 100644 --- a/utils/platform/commonblog/yuque/yuqueApi.ts +++ b/utils/platform/commonblog/yuque/yuqueApi.ts @@ -25,7 +25,7 @@ import { CommonblogApi } from "~/utils/platform/commonblog/commonblogApi" import { LogFactory } from "~/utils/logUtil" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" import { isEmptyObject } from "~/utils/util" /** @@ -94,7 +94,7 @@ export class YuqueApi extends CommonblogApi { throw new Error("请求语雀API异常") } - return appandStr(result.id, "") + return appendStr(result.id, "") } /** diff --git a/utils/platform/dynamicConfig.ts b/utils/platform/dynamicConfig.ts index a7d3ab36..8a3c5a97 100644 --- a/utils/platform/dynamicConfig.ts +++ b/utils/platform/dynamicConfig.ts @@ -27,7 +27,7 @@ import { CONSTANTS } from "~/utils/constants/constants" import { getJSONConf, setJSONConf } from "~/utils/configUtil" import { isEmptyString } from "~/utils/util" import { newID } from "~/utils/idUtil" -import { appandStr, upperFirst } from "~/utils/strUtil" +import { appendStr, upperFirst } from "~/utils/strUtil" export class DynamicConfig { /** @@ -205,9 +205,9 @@ export function getNewPlatformKey( ret = ptype.toLowerCase() if (!isEmptyString(subtype) && SubPlatformType.NONE !== subtype) { - ret = appandStr(ret, upperFirst(subtype)) + ret = appendStr(ret, upperFirst(subtype)) } - return appandStr(ret, "-", newId) + return appendStr(ret, "-", newId) } /** diff --git a/utils/platform/metaweblog/CustomMetaweblogApi.ts b/utils/platform/metaweblog/CustomMetaweblogApi.ts index a8f3ad3a..49980763 100644 --- a/utils/platform/metaweblog/CustomMetaweblogApi.ts +++ b/utils/platform/metaweblog/CustomMetaweblogApi.ts @@ -24,7 +24,7 @@ */ import { XmlrpcClient } from "~/utils/platform/metaweblog/xmlrpc" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" import { UserBlog } from "~/utils/common/userBlog" import { METAWEBLOG_METHOD_CONSTANTS } from "~/utils/constants/metaweblogMethodConstants" import { Logger } from "loglevel" @@ -78,7 +78,7 @@ export class CustomMetaWeblogApi { const faultCode = this.parseFieldValue(fault, "faultCode") const faultString = this.parseFieldValue(fault, "faultString") throw new Error( - appandStr("发生异常,错误码=>", faultCode, ",错误信息=>", faultString) + appendStr("发生异常,错误码=>", faultCode, ",错误信息=>", faultString) ) } diff --git a/utils/platform/siyuan/siYuanApi.ts b/utils/platform/siyuan/siYuanApi.ts index 889421db..5ae34926 100644 --- a/utils/platform/siyuan/siYuanApi.ts +++ b/utils/platform/siyuan/siYuanApi.ts @@ -25,7 +25,7 @@ import { getSiyuanCfg } from "~/utils/platform/siyuan/siYuanConfig" import { LogFactory } from "~/utils/logUtil" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" /** * 思源API v2.5.0 @@ -263,7 +263,7 @@ export class SiYuanApi { const siyuanCfg = getSiyuanCfg() if (siyuanCfg.baseUrl !== "") { - url = appandStr(siyuanCfg.baseUrl, url) + url = appendStr(siyuanCfg.baseUrl, url) } let m = "POST" diff --git a/utils/platform/siyuan/siYuanApiAdaptor.ts b/utils/platform/siyuan/siYuanApiAdaptor.ts index a9eaf307..8e93bb5c 100644 --- a/utils/platform/siyuan/siYuanApiAdaptor.ts +++ b/utils/platform/siyuan/siYuanApiAdaptor.ts @@ -30,7 +30,7 @@ import { API_TYPE_CONSTANTS } from "~/utils/constants/apiTypeConstants" import { Post } from "~/utils/common/post" import { POST_STATUS_CONSTANTS } from "~/utils/constants/postStatusConstants" import { CategoryInfo } from "~/utils/common/categoryInfo" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" import { renderHTML } from "~/utils/markdownUtil" import { removeWidgetTag } from "~/utils/htmlUtil" import { LogFactory } from "~/utils/logUtil" @@ -105,8 +105,8 @@ export class SiYuanApiAdaptor implements IApi { commonPost.title = siyuanPost.content commonPost.permalink = customSlug === "" - ? appandStr("/post/", siyuanPost.root_id) - : appandStr("/post/", customSlug, ".html") + ? appendStr("/post/", siyuanPost.root_id) + : appendStr("/post/", customSlug, ".html") // commonPost.isPublished = isPublished commonPost.mt_keywords = page.mt_keywords commonPost.description = page.description @@ -245,8 +245,8 @@ export class SiYuanApiAdaptor implements IApi { commonPost.title = siyuanPost.content commonPost.permalink = customSlug === "" - ? appandStr("/post/", siyuanPost.root_id) - : appandStr("/post/", customSlug, ".html") + ? appendStr("/post/", siyuanPost.root_id) + : appendStr("/post/", customSlug, ".html") // commonPost.isPublished = isPublished commonPost.mt_keywords = page.mt_keywords commonPost.description = page.description diff --git a/utils/publishUtil.ts b/utils/publishUtil.ts index 453c1607..b720223a 100644 --- a/utils/publishUtil.ts +++ b/utils/publishUtil.ts @@ -33,6 +33,11 @@ import { IGithubCfg } from "~/utils/platform/github/githubCfg" import { IMetaweblogCfg } from "~/utils/platform/metaweblog/IMetaweblogCfg" import { ICommonblogCfg } from "~/utils/platform/commonblog/commonblogCfg" import { LogFactory } from "~/utils/logUtil" +import { PublishPreference } from "~/utils/common/publishPreference" +import { CONSTANTS } from "~/utils/constants/constants" +import { isEmptyString } from "~/utils/util" +import { PageEditMode } from "~/utils/common/pageEditMode" +import { SourceContentShowType } from "~/utils/common/sourceContentShowType" const logger = LogFactory.getLogger("utils/publishUtil.ts") @@ -134,3 +139,33 @@ export const getPublishStatus = (apiType: string, meta: any): boolean => { return false } + +/** + * 获取发布偏好设置 + */ +export const getPublishCfg = (): PublishPreference => { + let publishCfg = getJSONConf( + CONSTANTS.PUBLISH_PREFERENCE_CONFIG_KEY + ) + if (isEmptyString(publishCfg)) { + publishCfg = new PublishPreference() + publishCfg.fixTitle = false + publishCfg.useGoogleTranslate = true + publishCfg.editMode = PageEditMode.EditMode_simple + publishCfg.contentShowType = SourceContentShowType.YAML_CONTENT + } + + // ================ + // 下面是我自己的配置 + // ================ + publishCfg.fixTitle = true + publishCfg.useGoogleTranslate = true + publishCfg.editMode = PageEditMode.EditMode_complex + publishCfg.contentShowType = SourceContentShowType.YAML + publishCfg.removeH1 = true + // ================ + // 下面是我自己的配置 + // ================ + + return publishCfg +} diff --git a/utils/strUtil.ts b/utils/strUtil.ts index 82478967..1b4b4a19 100644 --- a/utils/strUtil.ts +++ b/utils/strUtil.ts @@ -66,6 +66,20 @@ export const upperFirst = (name: string): string => { * 字符串拼接 * @param str */ -export const appandStr = (...str: string[]): string => { +export const appendStr = (...str: string[]): string => { return str.join("") } + +/** + * 文件名转title + * @param fmtTitle + */ +export const mdFileToTitle = (fmtTitle: string): string => { + if (fmtTitle.indexOf(".md") > -1) { + fmtTitle = fmtTitle.replace(/\.md/g, "") + } + if (fmtTitle.indexOf(".") > -1) { + fmtTitle = fmtTitle.replace(/\d*\./g, "") + } + return fmtTitle +} diff --git a/utils/util.ts b/utils/util.ts index b0968489..aa6c866d 100644 --- a/utils/util.ts +++ b/utils/util.ts @@ -41,6 +41,7 @@ export const zhSlugify = async (q: string): Promise => { const json = await v.json() let res = json[0][0] res = res.replaceAll(/-/g, "") + res = res.replaceAll(/\./g, "") res = res.replaceAll(/~/g, "") res = slugify(res) diff --git a/utils/yamlUtil.ts b/utils/yamlUtil.ts index af541001..34df64f6 100644 --- a/utils/yamlUtil.ts +++ b/utils/yamlUtil.ts @@ -24,7 +24,7 @@ */ import jsYaml from "js-yaml" -import { appandStr } from "~/utils/strUtil" +import { appendStr } from "~/utils/strUtil" /** * yaml转对象 @@ -43,6 +43,6 @@ export const yaml2Obj = (yaml: string): any => { export const obj2Yaml = (obj: any): string => { // https://npmmirror.com/package/js-yaml let res = jsYaml.dump(obj, {}) - res = appandStr("---\n", res, "---") + res = appendStr("---\n", res, "---") return res }