Skip to content

Commit

Permalink
fix: #1495 markdown file name support category and tag
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Dec 2, 2024
1 parent 5f3c47d commit 139e218
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/adaptors/base/baseExtendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
.replace(/\[MM]/g, m)
.replace(/\[mm]/g, m)
.replace(/\[dd]/g, d)
// 分类
if (cfg?.mdFilenameRule?.includes("category")) {
const cat = post?.categories?.[0] ?? ""
filename = filename.replace("[category]", cat)
}
if (cfg?.mdFilenameRule?.includes("cats")) {
const cat = post?.categories?.join("/") ?? ""
filename = filename.replace(/\[cats]/, cat)
}
// 标签
if (cfg?.mdFilenameRule?.includes("tag")) {
const tag = post?.mt_keywords?.split(",")?.[0] ?? ""
filename = filename.replace(/\[tag]/, tag)
}
if (cfg?.mdFilenameRule?.includes("tags")) {
const tag = post?.mt_keywords?.split(",")?.join("/") ?? ""
filename = filename.replace(/\[tags]/, tag)
}
if (cfg.useMdFilename) {
// 使用真实文件名作为MD文件名
filename = filename.replace(/\[filename]/g, post.originalTitle)
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export default {
"MD file preview rules (place occupied: [user] [repo] [Branch] [docpath]), for example:/[user]/[repo]/blob/[branch]/[docpath]",
"setting.blog.mdFilenameRule": "File rule",
"setting.blog.mdFilenameRule.tip":
"Markdown File Name Rules (Plasma: [yyyy] [MM] [dd] [slug] [filename]), for example: [filename].md or [slug].md or [yyyy]-[MM]-[dd]-[slug].md",
"Markdown File Name Rules (Plasma: [yyyy] [MM] [mm] [dd] [category] [cats] [tag] [tags] [slug] [filename]), for example: [filename].md or [slug].md or [yyyy]-[MM]-[dd]-[slug].md",
"setting.blog.username": "Username",
"setting.blog.password": "Password",
"setting.blog.apiurl": "API Url",
Expand Down
2 changes: 1 addition & 1 deletion src/locales/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default {
"如果不明白原理,请勿修改此选项。MD文件预览规则(占位符:[user] [repo] [branch] [docpath]),通常是:/[user]/[repo]/blob/[branch]/[docpath]",
"setting.blog.mdFilenameRule": "文件规则",
"setting.blog.mdFilenameRule.tip":
"Markdown文件名规则(占位符:[yyyy] [MM] [dd] [slug] [filename]),例如:[filename].md、[slug].md、[yyyy]-[mm]-[dd]-[slug].md",
"Markdown文件名规则(占位符:[yyyy] [MM] [mm] [dd] [category] [cats] [tag] [tags] [slug] [filename]),例如:[filename].md、[slug].md、[yyyy]-[mm]-[dd]-[slug].md",
"setting.blog.pageType": "发布格式",
"setting.blog.validate": "验证",
"setting.blog.save": "保存",
Expand Down
12 changes: 9 additions & 3 deletions src/utils/mdUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class MdUtils {
}

/**
* 获取一个字符串的“人类可读”版本
* 获取一个字符串的人类可读版本
*
* @param input
*/
Expand All @@ -155,12 +155,18 @@ class MdUtils {
// 在中文与英文/数字之间添加 -
.replace(/([\u4e00-\u9fa5])([a-zA-Z0-9])/g, "$1-$2")
.replace(/([a-zA-Z0-9])([\u4e00-\u9fa5])/g, "$1-$2")
// 移除非法字符,保留 / - _ . ~
.replace(/[^\w\-_.~\u4e00-\u9fa5\/-_]/g, "")
// 移除非法字符,保留 / - _ . ~
.replace(/[^\w\u4e00-\u9fa5\/\-_.~]/g, "")
// 将空格替换为 -
.replace(/\s+/g, "-")
// 合并连续的 /
.replace(/\/+/g, "/")
// 合并连续的 -
.replace(/-+/g, "-")
// 合并连续的 _
.replace(/_+/g, "_")
// 合并连续的 .
.replace(/\.{2,}/g, ".")
// 去掉开头和结尾的 -
.replace(/^-+|-+$/g, "")
)
Expand Down

0 comments on commit 139e218

Please sign in to comment.