Skip to content

Commit 3491a83

Browse files
authored
supported custom anchors in API index (#1757)
1 parent 7b37147 commit 3491a83

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/api/ApiIndex.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const filtered = computed(() => {
3131
return item
3232
}
3333
// filter headers
34-
const matchedHeaders = item.headers.filter(matches)
34+
const matchedHeaders = item.headers.map(h => h.text).filter(matches)
3535
return matchedHeaders.length
3636
? { text: item.text, link: item.link, headers: matchedHeaders }
3737
: null
@@ -92,8 +92,8 @@ function slugify(text: string): string {
9292
>
9393
<h3>{{ item.text }}</h3>
9494
<ul>
95-
<li v-for="h of item.headers" :key="h">
96-
<a :href="item.link + '.html#' + slugify(h)">{{ h }}</a>
95+
<li v-for="h of item.headers" :key="h.anchor">
96+
<a :href="item.link + '.html#' + slugify(h.anchor)">{{ h.anchor }}</a>
9797
</li>
9898
</ul>
9999
</div>

src/api/api.data.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ export interface APIGroup {
99
items: {
1010
text: string
1111
link: string
12-
headers: string[]
12+
headers: {
13+
anchor: string
14+
text: string
15+
}[]
1316
}[]
1417
}
1518

@@ -34,7 +37,10 @@ export default {
3437
const headersCache = new Map<
3538
string,
3639
{
37-
headers: string[]
40+
headers: {
41+
anchor: string
42+
text: string
43+
}[]
3844
timestamp: number
3945
}
4046
>()
@@ -50,15 +56,22 @@ function parsePageHeaders(link: string) {
5056

5157
const src = fs.readFileSync(fullPath, 'utf-8')
5258
const h2s = src.match(/^## [^\n]+/gm)
53-
let headers: string[] = []
59+
let headers: {
60+
anchor: string
61+
text: string
62+
}[] = []
5463
if (h2s) {
55-
headers = h2s.map((h) =>
56-
h
57-
.slice(2)
58-
.replace(/<sup class=.*/, '')
59-
.replace(/\\</g, '<')
60-
.replace(/`([^`]+)`/g, '$1')
61-
.trim()
64+
headers = h2s.map((h) => {
65+
const text = h
66+
.slice(2)
67+
.replace(/<sup class=.*/, '')
68+
.replace(/\\</g, '<')
69+
.replace(/`([^`]+)`/g, '$1')
70+
.replace(/\{#([a-zA-Z0-9-]+)\}/g, '') // hidden anchor tag
71+
.trim()
72+
const anchor = h.match(/\{#([a-zA-Z0-9-]+)\}/)?.[1] ?? text
73+
return { text, anchor }
74+
}
6275
)
6376
}
6477
headersCache.set(fullPath, {

0 commit comments

Comments
 (0)