Skip to content

Commit 269499a

Browse files
committed
compiler: custom heading id
1 parent cd92ae2 commit 269499a

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

vdoing/components/RightMenu.vue

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:class="[
77
'right-menu-item',
88
'level' + item.level,
9-
{ active: item.slug === hashText }
9+
{ active: item.slug === hashText },
1010
]"
1111
v-for="(item, i) in headers"
1212
:key="i"
@@ -19,38 +19,56 @@
1919
</template>
2020

2121
<script>
22+
const isValid = (content) => {
23+
return /\{#(.+?)\}$/.test(content);
24+
};
25+
const getNewHeaders = (headers) => {
26+
return headers?.map((v) => {
27+
let title = v.title;
28+
let slug = v.slug;
29+
if (isValid(title)) {
30+
slug = title.match(/\{#(.+?)\}$/)[1];
31+
title = title.replace(/\{#(.+?)\}$/, "").trim();
32+
}
33+
return {
34+
...v,
35+
slug,
36+
title,
37+
};
38+
});
39+
};
2240
export default {
2341
data() {
2442
return {
2543
headers: [],
26-
hashText: ''
27-
}
44+
hashText: "",
45+
};
2846
},
2947
mounted() {
30-
this.getHeadersData()
31-
this.getHashText()
48+
this.getHeadersData();
49+
this.getHashText();
3250
},
3351
watch: {
3452
$route() {
35-
this.headers = this.$page.headers
36-
this.getHashText()
37-
}
53+
this.headers = getNewHeaders(this.$page.headers);
54+
this.getHashText();
55+
},
3856
},
3957
methods: {
4058
getHeadersData() {
41-
this.headers = this.$page.headers
59+
this.headers = getNewHeaders(this.$page.headers);
4260
},
4361
getHashText() {
44-
this.hashText = decodeURIComponent(window.location.hash.slice(1))
45-
if(!this.hashText && this.headers){
46-
this.hashText = this.headers[0]?.slug
62+
this.hashText = decodeURIComponent(window.location.hash.slice(1));
63+
if (!this.hashText && this.headers) {
64+
this.hashText = this.headers[0]?.slug;
4765
}
48-
}
49-
}
50-
}
66+
},
67+
},
68+
};
5169
</script>
5270
53-
<style lang='stylus'>
71+
<style lang="stylus">
5472
.right-menu-wrapper
5573
max-height calc(100vh - 3.6rem - 36px)
5674
width $rightMenuWidth
@@ -151,7 +169,7 @@ export default {
151169
// padding 0.3rem 0
152170
// background var(--sidebarBg)
153171
// border-radius 5px
154-
172+
155173
.right-menu-item
156174
// border-color transparent
157175
// &.active

0 commit comments

Comments
 (0)