Skip to content

Commit 11bd76f

Browse files
Fix API index heading text (#2077)
1 parent 790bc2e commit 11bd76f

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

src/api/ApiIndex.vue

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,6 @@ const filtered = computed(() => {
4646
})
4747
.filter((i) => i) as APIGroup[]
4848
})
49-
50-
// same as vitepress' slugify logic
51-
function slugify(text: string): string {
52-
return (
53-
text
54-
// Replace special characters
55-
.replace(/[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g, '-')
56-
// Remove continuous separators
57-
.replace(/\-{2,}/g, '-')
58-
// Remove prefixing and trailing separators
59-
.replace(/^\-+|\-+$/g, '')
60-
// ensure it doesn't start with a number (#121)
61-
.replace(/^(\d)/, '_$1')
62-
// lowercase
63-
.toLowerCase()
64-
)
65-
}
6649
</script>
6750

6851
<template>
@@ -85,7 +68,7 @@ function slugify(text: string): string {
8568
:key="section.text"
8669
class="api-section"
8770
>
88-
<h2 :id="slugify(section.text)">{{ section.text }}</h2>
71+
<h2 :id="section.anchor">{{ section.text }}</h2>
8972
<div class="api-groups">
9073
<div
9174
v-for="item of section.items"
@@ -95,7 +78,7 @@ function slugify(text: string): string {
9578
<h3>{{ item.text }}</h3>
9679
<ul>
9780
<li v-for="h of item.headers" :key="h.anchor">
98-
<a :href="item.link + '.html#' + slugify(h.anchor)">{{ h.anchor }}</a>
81+
<a :href="item.link + '.html#' + h.anchor">{{ h.text }}</a>
9982
</li>
10083
</ul>
10184
</div>

src/api/api.data.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// a file ending with data.(j|t)s will be evaluated in Node.js
33
import fs from 'fs'
44
import path from 'path'
5+
import type { MultiSidebarConfig } from '@vue/theme/src/vitepress/config'
56
import { sidebar } from '../../.vitepress/config'
67

78
interface APIHeader {
@@ -11,6 +12,7 @@ interface APIHeader {
1112

1213
export interface APIGroup {
1314
text: string
15+
anchor: string
1416
items: {
1517
text: string
1618
link: string
@@ -26,8 +28,9 @@ export default {
2628
watch: './*.md',
2729
// read from fs and generate the data
2830
load(): APIGroup[] {
29-
return sidebar['/api/'].map((group) => ({
31+
return (sidebar as MultiSidebarConfig)['/api/'].map((group) => ({
3032
text: group.text,
33+
anchor: slugify(group.text),
3134
items: group.items.map((item) => ({
3235
...item,
3336
headers: parsePageHeaders(item.link)
@@ -57,22 +60,39 @@ function parsePageHeaders(link: string) {
5760
const h2s = src.match(/^## [^\n]+/gm)
5861
let headers: APIHeader[] = []
5962
if (h2s) {
63+
const anchorRE = /\{#([^}]+)\}/
6064
headers = h2s.map((h) => {
61-
const text = h
62-
.slice(2)
63-
.replace(/<sup class=.*/, '')
64-
.replace(/\\</g, '<')
65-
.replace(/`([^`]+)`/g, '$1')
66-
.replace(/\{#([a-zA-Z0-9-]+)\}/g, '') // hidden anchor tag
67-
.trim()
68-
const anchor = h.match(/\{#([a-zA-Z0-9-]+)\}/)?.[1] ?? text
69-
return { text, anchor }
70-
}
71-
)
65+
const text = h
66+
.slice(2)
67+
.replace(/<sup class=.*/, '')
68+
.replace(/\\</g, '<')
69+
.replace(/`([^`]+)`/g, '$1')
70+
.replace(anchorRE, '') // hidden anchor tag
71+
.trim()
72+
const anchor = h.match(anchorRE)?.[1] ?? slugify(text)
73+
return { text, anchor }
74+
})
7275
}
7376
headersCache.set(fullPath, {
7477
timestamp,
7578
headers
7679
})
7780
return headers
7881
}
82+
83+
// same as vitepress' slugify logic
84+
function slugify(text: string): string {
85+
return (
86+
text
87+
// Replace special characters
88+
.replace(/[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g, '-')
89+
// Remove continuous separators
90+
.replace(/\-{2,}/g, '-')
91+
// Remove prefixing and trailing separators
92+
.replace(/^\-+|\-+$/g, '')
93+
// ensure it doesn't start with a number (#121)
94+
.replace(/^(\d)/, '_$1')
95+
// lowercase
96+
.toLowerCase()
97+
)
98+
}

0 commit comments

Comments
 (0)