Skip to content

Commit

Permalink
feat: support outline for halo
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Nov 19, 2024
1 parent 86409b6 commit 954be84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/adaptors/api/halo/HaloApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import sypIdUtil from "~/src/utils/sypIdUtil.ts"
import { PostRequest } from "@halo-dev/api-client"
import { HaloPostMeta } from "~/src/adaptors/api/halo/HaloPostMeta.ts"
import FormDataUtils from "~/src/utils/FormDataUtils.ts"
import HaloUtils from "~/src/adaptors/api/halo/haloUtils.ts"

/**
* Halo API 适配器 V2.9.0
Expand Down Expand Up @@ -103,9 +104,9 @@ class HaloApiAdaptor extends BaseBlogApi {
if (this.cfg.pageType === "markdown") {
params.content.raw = post.markdown
} else {
params.content.raw = post.html
params.content.raw = HaloUtils.addIdToH1_H6(post.html)
}
params.content.content = post.html
params.content.content = HaloUtils.addIdToH1_H6(post.html)

if (StrUtil.isEmptyString(post.shortDesc)) {
params.post.spec.excerpt.autoGenerate = true
Expand Down Expand Up @@ -175,9 +176,9 @@ class HaloApiAdaptor extends BaseBlogApi {
if (params.content.rawType === "markdown") {
params.content.raw = post.markdown
} else {
params.content.raw = post.html
params.content.raw = HaloUtils.addIdToH1_H6(post.html)
}
params.content.content = post.html
params.content.content = HaloUtils.addIdToH1_H6(post.html)

if (StrUtil.isEmptyString(post.shortDesc)) {
params.post.spec.excerpt.autoGenerate = true
Expand Down
13 changes: 13 additions & 0 deletions src/adaptors/api/halo/haloUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ class HaloUtils {
public static mergeMatter(content: string, data: object) {
return matter.stringify(content, data, this.options)
}

/**
* 将正文 h1-h6的标签加上 id,例如 <h1 xxx>标题1</h1> 转换成 <h1 id="标题1">标题1</h1>
*
* @param content
*/
public static addIdToH1_H6(content: string): string {
return content.replace(/<h([1-6])>(.*?)<\/h\1>/g, function (match, level, title) {
// 直接使用标题作为 ID
const id = title
return `<h${level} id="${id}">${title}</h${level}>`
})
}
}

export default HaloUtils
9 changes: 5 additions & 4 deletions src/adaptors/web/haloweb/HalowebWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { AliasTranslator, JsonUtil, ObjectUtil, StrUtil } from "zhi-common"
import { HalowebPostMeta } from "~/src/adaptors/web/haloweb/HalowebPostMeta.ts"
import sypIdUtil from "~/src/utils/sypIdUtil"
import FormDataUtils from "~/src/utils/FormDataUtils.ts"
import HaloUtils from "~/src/adaptors/api/halo/haloUtils.ts"

/**
* Halo 网页授权适配器
Expand Down Expand Up @@ -128,9 +129,9 @@ class HalowebWebAdaptor extends BaseWebApi {
if (this.cfg.pageType === "markdown") {
params.content.raw = post.markdown
} else {
params.content.raw = post.html
params.content.raw = HaloUtils.addIdToH1_H6(post.html)
}
params.content.content = post.html
params.content.content = HaloUtils.addIdToH1_H6(post.html)

if (StrUtil.isEmptyString(post.shortDesc)) {
params.post.spec.excerpt.autoGenerate = true
Expand Down Expand Up @@ -200,9 +201,9 @@ class HalowebWebAdaptor extends BaseWebApi {
if (params.content.rawType === "markdown") {
params.content.raw = post.markdown
} else {
params.content.raw = post.html
params.content.raw = HaloUtils.addIdToH1_H6(post.html)
}
params.content.content = post.html
params.content.content = HaloUtils.addIdToH1_H6(post.html)

if (StrUtil.isEmptyString(post.shortDesc)) {
params.post.spec.excerpt.autoGenerate = true
Expand Down

0 comments on commit 954be84

Please sign in to comment.