Skip to content

Commit

Permalink
fix: #314 HUGO平台支持修改菜单标题和权重
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jan 8, 2023
1 parent 9c6eb87 commit 2fd2274
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 12 deletions.
14 changes: 14 additions & 0 deletions components/publish/tab/main/GithubMain.vue
Expand Up @@ -375,6 +375,19 @@
:placeholder="$t('github.weight.placeholder')"
/>
</el-form-item>
<!-- 是否显示日期字段 -->
<el-form-item :label="$t('github.use.date')">
<el-switch
v-model="githubPagesData.useDate"
@change="githubPagesMethods.showDateOnChange"
/>
<el-alert
v-if="!githubPagesData.useDate"
:closable="false"
:title="$t('github.use.date.no.warn')"
type="warning"
/>
</el-form-item>
</div>

<!-- 快捷操作 -->
Expand Down Expand Up @@ -710,6 +723,7 @@ const { quickData, quickMethods } = useQuick(props, {
slugMethods,
descMethods,
tagMethods,
githubPagesMethods,
})
const { initPublishData, initPublishMethods } = useInitPublish(props, {
pageModeMethods,
Expand Down
35 changes: 33 additions & 2 deletions composables/publish/githubPagesCom.ts
Expand Up @@ -28,11 +28,13 @@ import { LogFactory } from "~/utils/logUtil"
import { getJSONConf } from "~/utils/configUtil"
import { IGithubCfg } from "~/utils/platform/github/githubCfg"
import { GithubApi } from "~/utils/platform/github/githubApi"
import { isEmptyString, pathJoin } from "~/utils/util"
import { isEmptyString, parseBoolean, pathJoin } from "~/utils/util"
import { ElMessage } from "element-plus"
import { getApiParams } from "~/utils/publishUtil"
import { appendStr } from "~/utils/strUtil"
import { CONSTANTS } from "~/utils/constants/constants"
import { SIYUAN_PAGE_ATTR_KEY } from "~/utils/constants/siyuanPageConstants"
import { SiyuanDataObj } from "~/utils/models/siyuanDataObj"

/**
* Github pages组件
Expand Down Expand Up @@ -77,6 +79,10 @@ export const useGithubPages = (props, deps) => {
* 权重(决定显示顺序,越小显示越靠前)
*/
weight: 0,
/**
* 是否显示日期字段
*/
useDate: true,
})

// deps
Expand Down Expand Up @@ -155,6 +161,9 @@ export const useGithubPages = (props, deps) => {
permalinkOnChange: (val: boolean) => {
githubPagesData.usePermalink = val
},
showDateOnChange: (val: boolean) => {
githubPagesData.useDate = val
},

getGithubPagesData: () => {
return githubPagesData
Expand Down Expand Up @@ -225,7 +234,7 @@ export const useGithubPages = (props, deps) => {
return mdTitle
},

initGithubPages: (paths: any) => {
initGithubPages: (paths: any, siyuanData?: SiyuanDataObj) => {
let cpath: string, defpath: string, fname: string
if (paths) {
cpath = paths.cpath
Expand All @@ -248,6 +257,28 @@ export const useGithubPages = (props, deps) => {
"/" + githubPagesData.mdTitle
)
}

// 附加属性
if (siyuanData) {
const menuTitleKey =
SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_MENU_TITLE_KEY
githubPagesData.linkTitle = siyuanData.meta[menuTitleKey] ?? ""

const weightKey =
SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_WEIGHT_KEY
githubPagesData.weight = siyuanData.meta[weightKey] ?? "0"

const usePermalinkKey =
SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_USE_PERMALINK_KEY
githubPagesData.usePermalink = parseBoolean(
siyuanData.meta[usePermalinkKey] ?? "true"
)
const useDateKey =
SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_USE_DATE_KEY
githubPagesData.useDate = parseBoolean(
siyuanData.meta[useDateKey] ?? "false"
)
}
},
}

Expand Down
16 changes: 11 additions & 5 deletions composables/publish/initPublishCom.ts
Expand Up @@ -187,6 +187,9 @@ export const useInitPublish = (props, deps, otherArgs?) => {
githubPagesMethods.getGithubPagesData().linkTitle
// 权重
postForm.formData.weight = githubPagesMethods.getGithubPagesData().weight
// 是否显示日期字段
postForm.formData.useDate =
githubPagesMethods.getGithubPagesData().useDate

return postForm
},
Expand Down Expand Up @@ -335,11 +338,14 @@ export const useInitPublish = (props, deps, otherArgs?) => {
const currentDefaultPath = githubCfg.defaultPath ?? "尚未配置"
const mdTitle = githubPagesMethods.getMdFilename()
// 初始化
githubPagesMethods.initGithubPages({
cpath: docPath,
defpath: currentDefaultPath,
fname: mdTitle,
})
githubPagesMethods.initGithubPages(
{
cpath: docPath,
defpath: currentDefaultPath,
fname: mdTitle,
},
siyuanData
)

// 所有数据初始化完成,生成YAML
initPublishMethods.convertAttrToYAML(true)
Expand Down
24 changes: 23 additions & 1 deletion composables/publish/publishQuickCom.ts
Expand Up @@ -31,6 +31,7 @@ import { SIYUAN_PAGE_ATTR_KEY } from "~/utils/constants/siyuanPageConstants"
import { SiYuanApi } from "~/utils/platform/siyuan/siYuanApi"
import { appendStr } from "~/utils/strUtil"
import { reloadPage } from "~/utils/browserUtil"
import { isEmptyString } from "~/utils/util"

/**
* 快捷操作组件
Expand All @@ -54,6 +55,7 @@ export const useQuick = (props, deps?: any) => {
const slugMethods = deps.slugMethods
const descMethods = deps.descMethods
const tagMethods = deps.tagMethods
const githubPagesMethods = deps.githubPagesMethods

// public methods
const quickMethods = {
Expand Down Expand Up @@ -109,14 +111,34 @@ export const useQuick = (props, deps?: any) => {
const slugData = slugMethods.getSlugData()
const descData = descMethods.getDescData()
const tagData = tagMethods.getTagData()
const customAttr = {
const githubPagesData = githubPagesMethods.getGithubPagesData()

let customAttr = {
[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_SLUG_KEY]:
slugData.customSlug,
[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_DESC_KEY]:
descData.desc,
[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_USE_PERMALINK_KEY]:
githubPagesData.usePermalink.toString(),
[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_USE_DATE_KEY]:
githubPagesData.useDate.toString(),
tags: tagData.join(","),
}

if (!isEmptyString(githubPagesData.linkTitle)) {
customAttr = Object.assign(customAttr, {
[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_MENU_TITLE_KEY]:
githubPagesData.linkTitle,
})
}

const weight = parseInt(githubPagesData.weight)
if (weight > 0) {
customAttr = Object.assign(customAttr, {
[SIYUAN_PAGE_ATTR_KEY.SIYUAN_PAGE_ATTR_CUSTOM_WEIGHT_KEY]: weight,
})
}

// 获取最新属性
const pageId = await siyuanPageMethods.getPageId()
await siyuanApi.setBlockAttrs(pageId, customAttr)
Expand Down
3 changes: 3 additions & 0 deletions locales/en_US.ts
Expand Up @@ -335,4 +335,7 @@ export default {
"github.weight": "Weight",
"github.weight.placeholder":
"Weight (decide to display the order, the smaller the display, the more upward)",
"github.use.date": "Show date",
"github.use.date.no.warn":
"You have closed the date show that it will not generate the date field in Formatter",
}
2 changes: 2 additions & 0 deletions locales/zh_CN.ts
Expand Up @@ -346,4 +346,6 @@ export default {
"菜单栏标题(HUGO平台专用,为空则不显示在菜单)",
"github.weight": "显示权重",
"github.weight.placeholder": "权重(决定显示顺序,越小显示越靠前)",
"github.use.date": "显示日期",
"github.use.date.no.warn": "您已关闭日期显示,将不会在formatter生成date字段",
}
28 changes: 27 additions & 1 deletion utils/constants/siyuanPageConstants.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 All @@ -23,10 +23,36 @@
* questions.
*/

/**
* 别名
*/
const SIYUAN_PAGE_ATTR_CUSTOM_SLUG_KEY = "custom-slug"
/**
* 摘要
*/
const SIYUAN_PAGE_ATTR_CUSTOM_DESC_KEY = "custom-desc"
/**
* 菜单标题
*/
const SIYUAN_PAGE_ATTR_CUSTOM_MENU_TITLE_KEY = "custom-menu-title"
/**
* 权重
*/
const SIYUAN_PAGE_ATTR_CUSTOM_WEIGHT_KEY = "custom-weight"
/**
* 是否显示永久链接
*/
const SIYUAN_PAGE_ATTR_CUSTOM_USE_PERMALINK_KEY = "custom-use-permalink"
/**
* 是否使用日期
*/
const SIYUAN_PAGE_ATTR_CUSTOM_USE_DATE_KEY = "custom-use-date"

export const SIYUAN_PAGE_ATTR_KEY = {
SIYUAN_PAGE_ATTR_CUSTOM_SLUG_KEY,
SIYUAN_PAGE_ATTR_CUSTOM_DESC_KEY,
SIYUAN_PAGE_ATTR_CUSTOM_MENU_TITLE_KEY,
SIYUAN_PAGE_ATTR_CUSTOM_WEIGHT_KEY,
SIYUAN_PAGE_ATTR_CUSTOM_USE_PERMALINK_KEY,
SIYUAN_PAGE_ATTR_CUSTOM_USE_DATE_KEY,
}
2 changes: 1 addition & 1 deletion utils/import/importUtil.ts
Expand Up @@ -173,7 +173,7 @@ const doFixPlatformInfo = (

// 修复 Typecho 的拼写错误
const pcfg = dynamicConfigArray[i]
logger.warn("pcfg=>", pcfg)
logger.debug("pcfg=>", pcfg)
if (PRE_DEFINED_PLATFORM_KEY_CONSTANTS.PRE_DEFINED_TYPECHO_KEY === pkey) {
if (pcfg.platformName === "Typeecho") {
pcfg.platformName = "Typecho"
Expand Down
1 change: 1 addition & 0 deletions utils/models/postForm.ts
Expand Up @@ -44,6 +44,7 @@ export class PostForm {
mdContent: "",
htmlContent: "",
usePermalink: true,
useDate: true,
linkTitle: "",
weight: 0,
}
Expand Down
7 changes: 5 additions & 2 deletions utils/platform/github/hugo/HugoYamlConverterAdaptor.ts
Expand Up @@ -64,8 +64,11 @@ export class HugoYamlConverterAdaptor
"/post/" + postForm.formData.customSlug + ".html"
}

// date
yamlFormatObj.yamlObj.date = postForm.formData.created
// 日期开关
if (postForm.formData.useDate) {
// date
yamlFormatObj.yamlObj.date = postForm.formData.created
}

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

0 comments on commit 2fd2274

Please sign in to comment.