Skip to content

Commit

Permalink
fix: 部分侧边栏无法识别标题
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanzisama committed Jan 5, 2025
1 parent 6a47e37 commit 965a5ec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export default withMermaid({
],
sidebar: {
"/": await getSidebar(resolve(import.meta.dirname, "../docs/nitwikit/docs"), ""),
"/Java/": await getSidebar(resolve(import.meta.dirname, "../docs/nitwikit/docs-java"), "Java"),
"/Bedrock/": await getSidebar(resolve(import.meta.dirname, "../docs/nitwikit/docs-bedrock"), "Bedrock"),
"/Java/": await getSidebar(resolve(import.meta.dirname, "../docs/nitwikit/docs-java"), "/Java"),
"/Bedrock/": await getSidebar(resolve(import.meta.dirname, "../docs/nitwikit/docs-bedrock"), "/Bedrock"),
},
...themeConfig,
},
Expand Down
43 changes: 41 additions & 2 deletions .vitepress/utils/vitepress.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import fs from "fs";
import { resolve } from "path";
import glob from "tiny-glob";
import frontMatter from "front-matter";
import { DefaultTheme } from "vitepress";
import { cloneDeep, omit, pull } from "lodash-es";
import { cloneDeep, isUndefined, omit, pull } from "lodash-es";
import markdownit from "markdown-it";
import { Node, parseFromString } from "dom-parser";

const md = markdownit({ html: true, linkify: true, typographer: true });

export async function getSidebar(dir: string, pagePath: string): Promise<DefaultTheme.SidebarItem[]> {
const files = await glob(`**/*.md`, { cwd: dir });
Expand All @@ -12,7 +17,7 @@ export async function getSidebar(dir: string, pagePath: string): Promise<Default
return tree
.map((item) => {
return {
text: item?.attributes?.title ?? item.name ?? "Unknown",
text: getTitle(dir, item),
items: flatmap(item.children ?? []),
link: pagePath + "/" + item.path.replaceAll("\\", "/").replace(/\.md$/, ""),
meta: item,
Expand Down Expand Up @@ -72,6 +77,39 @@ function getMarkdownAttributes(path) {
return fm.attributes;
}

function getTitle(dir: string, item: TreeNode) {
let title = item?.attributes?.title ?? item.attributes?.sidebar_label;

try {
if (isUndefined(title)) {
const path = resolve(dir, item.path);

const content = fs.readFileSync(path, { encoding: "utf-8" });
const html = md.render(content.replace(/^---([\s\S]*)---/, "")).replace(/\<(.*) \/\>/g, "");
const dom = parseFromString(html);

let headingNodes: Node[] = [];

// trying to find heading 1 to 6
for (let i = 1; i <= 6; i++) {
if (headingNodes.length === 0) {
const headingTags = dom.getElementsByTagName(`h${i}`);
if (Array.isArray(headingTags) && headingTags.length !== 0) {
headingNodes = headingTags;
break;
}
}
}

title = headingNodes.reduce((acc, _) => acc + _.textContent, "");
}
} catch (error) {
console.error(error);
}

return title ?? "Unknown";
}

interface TreeNode {
name: string;
path: string;
Expand All @@ -82,5 +120,6 @@ interface TreeNode {
interface NodeAttribute {
title?: string;
slug?: string;
sidebar_label?: string;
sidebar_position?: string;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"devDependencies": {
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.9.0",
"dom-parser": "^1.1.5",
"front-matter": "^4.0.2",
"markdown-it": "^14.1.0",
"mermaid": "^11.4.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 965a5ec

Please sign in to comment.