Skip to content

Commit

Permalink
Merge pull request #1484 from terwer/dev
Browse files Browse the repository at this point in the history
feat: #1231 support filename.md as hexo md filename
  • Loading branch information
terwer authored Nov 27, 2024
2 parents 28d50c9 + 2795949 commit 50fadc0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Publish articles from siyuan-note to platforms like Yuque etc. `Free` and `open
**Encountered other issues or have suggestions? Group 1 is full, please join QQ Group 2 for discussion.**

[![](https://img.shields.io/badge/QQ%20Group-722632752-blue)](<a target="_blank" href="https://qm.qq.com/cgi-bin/qm/qr?k=fYrA79XDvtr4JuEgez-dmj1h3tOef8pg&jump_from=webapi&authKey=DC+XcjkoTH762jxvkSgpt7V97QFETnaLVTZIWhd8PdZoX+MNSr+LsprWFYYELXu8"><img border="0" src="//pub.idqqimg.com/wpa/images/group.png" alt="发布工具2群" title="发布工具2群"></a>)
[![](https://img.shields.io/badge/QQ-722632752-blue)](https://qm.qq.com/cgi-bin/qm/qr?k=fYrA79XDvtr4JuEgez-dmj1h3tOef8pg&jump_from=webapi&authKey=DC+XcjkoTH762jxvkSgpt7V97QFETnaLVTZIWhd8PdZoX+MNSr+LsprWFYYELXu8)

The paid plugin [Share Pro](https://siyuan.wiki/s/20241115132719-3ivxe1y), also written by me, is now available. Share your notes with one click, welcome to follow.
## Update history
Expand Down
4 changes: 3 additions & 1 deletion src/adaptors/api/gitlab-hexo/useGitlabhexoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const useGitlabhexoApi = async (key: string, newCfg?: GitlabhexoConfig) => {
const githubBranch = Utils.emptyOrDefault(process.env.VITE_GITLAB_BRANCH, "main")
const middlewareUrl = Utils.emptyOrDefault(process.env.VITE_MIDDLEWARE_URL, LEGENCY_SHARED_PROXT_MIDDLEWARE)
cfg = new GitlabhexoConfig(githubUsername, githubAuthToken, githubRepo, githubBranch, middlewareUrl)
cfg.mdFilenameRule = "[slug].md"
logger.info("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
Expand All @@ -76,7 +77,8 @@ const useGitlabhexoApi = async (key: string, newCfg?: GitlabhexoConfig) => {
}

// 文件规则
cfg.mdFilenameRule = "[slug].md"
// 推荐别名。但是不强制使用
// cfg.mdFilenameRule = "[slug].md"
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
4 changes: 3 additions & 1 deletion src/adaptors/api/hexo/useHexoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const useHexoApi = async (key: string, newCfg?: HexoConfig) => {
const githubBranch = Utils.emptyOrDefault(process.env.VITE_GITHUB_BRANCH, "main")
const middlewareUrl = Utils.emptyOrDefault(process.env.VITE_MIDDLEWARE_URL, LEGENCY_SHARED_PROXT_MIDDLEWARE)
cfg = new HexoConfig(githubUsername, githubAuthToken, githubRepo, githubBranch, middlewareUrl)
cfg.mdFilenameRule = "[slug].md"
logger.info("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
Expand All @@ -81,7 +82,8 @@ const useHexoApi = async (key: string, newCfg?: HexoConfig) => {
// [yyyy] 年
// [mm] 月
// [dd] 日
cfg.mdFilenameRule = "[slug].md"
// 推荐别名。但是不强制使用
// cfg.mdFilenameRule = "[slug].md"
// 标签
cfg.tagEnabled = true
// 分类
Expand Down
2 changes: 2 additions & 0 deletions src/adaptors/base/baseExtendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
if (cfg.useMdFilename) {
// 使用真实文件名作为MD文件名
filename = filename.replace(/\[filename]/g, post.originalTitle)
// 这里需要去除空格等 url 里面参数不允许的非法字符
filename = MdUtils.getHumanFilename(filename)
} else {
// 使用别名作为MD文件名
filename = filename.replace(/\[slug]/g, post.wp_slug)
Expand Down
2 changes: 0 additions & 2 deletions src/adaptors/web/zhihu/zhihuWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ class ZhihuWebAdaptor extends BaseWebApi {
// this.logger.debug("get column initJson=>", initJson)
// const columns = initJson?.initialState?.entities?.columns ?? {}
// if (ObjectUtil.isEmptyObject(columns) || columns.length == 0) {
// debugger
// $(".List-item a.ColumnLink").each((index, element) => {
// // 在这里处理每个匹配的 a 标签
// const $a = $(element)
Expand All @@ -258,7 +257,6 @@ class ZhihuWebAdaptor extends BaseWebApi {
// const href = $a.attr("href")
// // 获取 div 内的文本
// const text = $div.text()
// debugger
// const cat = new CategoryInfo()
// cat.categoryId = href.split("/").pop()
// cat.categoryName = text
Expand Down
1 change: 0 additions & 1 deletion src/composables/usePublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ const usePublish = () => {
const post = _.cloneDeep(doc) as Post
// 别名
const slug = ObjectUtil.getProperty(postMeta, SiyuanAttr.Custom_slug, post.wp_slug)
debugger
if (!StrUtil.isEmptyString(slug)) {
post.wp_slug = slug
logger.info("Using existing siyuan note slug")
Expand Down
15 changes: 15 additions & 0 deletions src/utils/mdUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,19 @@ $$
const processedText = MdUtils.replaceSignToAnother(text, "**", " **", "** ")
console.log(processedText)
})

it("test getHumanFilename", () => {
const result = MdUtils.getHumanFilename("zh二哥有问题 123 !@#$%^&*()")
console.log(result)
})

it("test getHumanFilename2", () => {
const result = MdUtils.getHumanFilename("文件名字2024测试---!")
console.log(result)
})

it("test getHumanFilename3", () => {
const result = MdUtils.getHumanFilename("测试123abc中文")
console.log(result)
})
})
22 changes: 22 additions & 0 deletions src/utils/mdUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,28 @@ class MdUtils {

return finalText
}

/**
* 获取一个字符串的“人类可读”版本
*
* @param input
*/
public static getHumanFilename(input: string): string {
return (
input
// 在中文与英文/数字之间添加 -
.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(/\s+/g, "-")
// 合并连续的 -
.replace(/-+/g, "-")
// 去掉开头和结尾的 -
.replace(/^-+|-+$/g, "")
)
}
}

export { MdUtils }

0 comments on commit 50fadc0

Please sign in to comment.