Skip to content

Commit 828cb62

Browse files
ys-zhifutravilyu
authored andcommitted
compiler: custom heading id
1 parent 08afb55 commit 828cb62

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

vdoing/components/RightMenu.vue

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@
1919
</template>
2020

2121
<script>
22+
const getSlug = (title, slug) => {
23+
const titles = title.split(".");
24+
const slugs = slug.split("-");
25+
return titles
26+
.reduce((res, s) => {
27+
if (slugs.includes(s)) {
28+
res = res + s + ".";
29+
} else {
30+
let s1 = s.slice(0);
31+
let index = 0;
32+
let s2;
33+
while (s1) {
34+
s2 = s1.slice(0, index);
35+
if (slugs.includes(s2)) {
36+
if (index === s1.length) {
37+
res = res + s2 + ".";
38+
} else {
39+
res = res + s2 + "_";
40+
}
41+
s1 = s1.slice(index);
42+
index = 0;
43+
} else {
44+
index++;
45+
if (index > s1.length) {
46+
res = res.slice(0, -1) + s2 + ".";
47+
break;
48+
}
49+
}
50+
}
51+
}
52+
return res;
53+
}, "")
54+
.slice(0, -1);
55+
};
2256
const isValid = (content) => {
2357
return /\{#(.+?)\}$/.test(content);
2458
};
@@ -27,7 +61,7 @@ const getNewHeaders = (headers) => {
2761
let title = v.title;
2862
let slug = v.slug;
2963
if (isValid(title)) {
30-
slug = title.match(/\{#(.+?)\}$/)[1];
64+
slug = getSlug(title.match(/\{#(.+?)\}$/)[1], v.slug);
3165
title = title.replace(/\{#(.+?)\}$/, "").trim();
3266
}
3367
return {

vdoing/util/index.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,6 @@ export function isActive(route, path) {
5454
return routePath === pagePath;
5555
}
5656

57-
const isValid = (content) => {
58-
return /\{#(.+?)\}$/.test(content);
59-
};
60-
const getNewHeaders = (headers) => {
61-
return headers?.map((v) => {
62-
let title = v.title;
63-
let slug = v.slug;
64-
if (isValid(title)) {
65-
slug = title.match(/\{#(.+?)\}$/)[1];
66-
title = title.replace(/\{#(.+?)\}$/, "").trim();
67-
}
68-
return {
69-
...v,
70-
slug,
71-
title,
72-
};
73-
});
74-
};
7557

7658
// 直接把pages转为map读取 减少400ms的时间
7759
const pageMap = new Map();
@@ -97,7 +79,7 @@ export function resolvePage(pages, rawPath, base) {
9779
pageMap.set(normalize(regularPath), {
9880
href,
9981
title,
100-
headers: getNewHeaders(headers),
82+
headers,
10183
collapsable,
10284
basePath,
10385
type: "page",

0 commit comments

Comments
 (0)