@verbb/vitepress-theme 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/package.json +32 -0
- package/src/Layout.vue +1077 -0
- package/src/components/DocsIcon.vue +100 -0
- package/src/components/DocsMobileMenu.vue +68 -0
- package/src/components/DocsMobileMenuNode.vue +114 -0
- package/src/components/DocsNotFound.vue +41 -0
- package/src/components/DocsOutline.vue +284 -0
- package/src/components/DocsOutlineItem.vue +65 -0
- package/src/components/DocsSearchProvider.vue +51 -0
- package/src/components/DocsSidebar.vue +41 -0
- package/src/components/DocsSidebarNode.vue +114 -0
- package/src/custom.css +547 -0
- package/src/env.d.ts +8 -0
- package/src/index.ts +22 -0
- package/src/search.ts +17 -0
- package/src/sidebar.ts +218 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed } from 'vue';
|
|
3
|
+
|
|
4
|
+
const props = withDefaults(defineProps<{
|
|
5
|
+
name?: string;
|
|
6
|
+
class?: string;
|
|
7
|
+
}>(), {
|
|
8
|
+
name: '',
|
|
9
|
+
class: 'size-4',
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
type IconDefinition = {
|
|
13
|
+
paths: Array<{
|
|
14
|
+
d: string;
|
|
15
|
+
fill?: string;
|
|
16
|
+
}>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const icons: Record<string, IconDefinition> = {
|
|
20
|
+
'play-circle': {
|
|
21
|
+
paths: [
|
|
22
|
+
{ d: 'M8 14.25A6.25 6.25 0 1 0 8 1.75a6.25 6.25 0 0 0 0 12.5' },
|
|
23
|
+
{ d: 'M6.25 5.75 10.25 8l-4 2.25V5.75', fill: 'currentColor' },
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
'app-window': {
|
|
27
|
+
paths: [
|
|
28
|
+
{ d: 'M2.75 4.25A1.5 1.5 0 0 1 4.25 2.75h7.5a1.5 1.5 0 0 1 1.5 1.5v7.5a1.5 1.5 0 0 1-1.5 1.5h-7.5a1.5 1.5 0 0 1-1.5-1.5v-7.5Z' },
|
|
29
|
+
{ d: 'M2.75 5.5h10.5' },
|
|
30
|
+
{ d: 'M5 4.125h.01M7 4.125h.01M9 4.125h.01' },
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
blocks: {
|
|
34
|
+
paths: [
|
|
35
|
+
{ d: 'M2.75 3.25h4.5v4.5h-4.5z' },
|
|
36
|
+
{ d: 'M8.75 3.25h4.5v4.5h-4.5z' },
|
|
37
|
+
{ d: 'M5.75 8.75h4.5v4.5h-4.5z' },
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
'clipboard-list': {
|
|
41
|
+
paths: [
|
|
42
|
+
{ d: 'M5.25 3.25h5.5a1.5 1.5 0 0 1 1.5 1.5v7a1.5 1.5 0 0 1-1.5 1.5h-5.5a1.5 1.5 0 0 1-1.5-1.5v-7a1.5 1.5 0 0 1 1.5-1.5Z' },
|
|
43
|
+
{ d: 'M6.25 2.75h3.5v1.5h-3.5z' },
|
|
44
|
+
{ d: 'M6 6.5h3.75M6 8.5h3.75M6 10.5h3.75' },
|
|
45
|
+
{ d: 'M5 6.5h.01M5 8.5h.01M5 10.5h.01' },
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
'layout-template': {
|
|
49
|
+
paths: [
|
|
50
|
+
{ d: 'M2.75 3.25h10.5v9.5H2.75z' },
|
|
51
|
+
{ d: 'M6.25 3.25v9.5' },
|
|
52
|
+
{ d: 'M6.25 6.75h7' },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
'rows-3': {
|
|
56
|
+
paths: [
|
|
57
|
+
{ d: 'M3 4.5h1.5M6 4.5h7' },
|
|
58
|
+
{ d: 'M3 8h1.5M6 8h7' },
|
|
59
|
+
{ d: 'M3 11.5h1.5M6 11.5h7' },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
'square-terminal': {
|
|
63
|
+
paths: [
|
|
64
|
+
{ d: 'M3.25 3.25h9.5v9.5h-9.5z' },
|
|
65
|
+
{ d: 'M5.25 6.25 7 8l-1.75 1.75' },
|
|
66
|
+
{ d: 'M8.75 9.75h2.25' },
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
'flask-conical': {
|
|
70
|
+
paths: [
|
|
71
|
+
{ d: 'M6 2.75h4' },
|
|
72
|
+
{ d: 'M7 2.75v2.5l-3 5.25a1.5 1.5 0 0 0 1.3 2.25h5.4A1.5 1.5 0 0 0 12 10.5L9 5.25v-2.5' },
|
|
73
|
+
{ d: 'M5.5 9h5' },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const icon = computed(() => icons[props.name] ?? null);
|
|
79
|
+
</script>
|
|
80
|
+
|
|
81
|
+
<template>
|
|
82
|
+
<svg
|
|
83
|
+
v-if="icon"
|
|
84
|
+
viewBox="0 0 16 16"
|
|
85
|
+
fill="none"
|
|
86
|
+
stroke="currentColor"
|
|
87
|
+
stroke-width="1.5"
|
|
88
|
+
stroke-linecap="round"
|
|
89
|
+
stroke-linejoin="round"
|
|
90
|
+
:class="props.class"
|
|
91
|
+
aria-hidden="true"
|
|
92
|
+
>
|
|
93
|
+
<path
|
|
94
|
+
v-for="path in icon.paths"
|
|
95
|
+
:key="path.d"
|
|
96
|
+
:d="path.d"
|
|
97
|
+
:fill="path.fill ?? 'none'"
|
|
98
|
+
/>
|
|
99
|
+
</svg>
|
|
100
|
+
</template>
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed } from 'vue';
|
|
3
|
+
import { useDocsSidebar } from '../sidebar';
|
|
4
|
+
import DocsIcon from './DocsIcon.vue';
|
|
5
|
+
import DocsMobileMenuNode from './DocsMobileMenuNode.vue';
|
|
6
|
+
|
|
7
|
+
defineProps<{
|
|
8
|
+
logoSrc: string | null;
|
|
9
|
+
siteTitle: string;
|
|
10
|
+
}>();
|
|
11
|
+
|
|
12
|
+
defineEmits<{
|
|
13
|
+
navigate: [];
|
|
14
|
+
}>();
|
|
15
|
+
|
|
16
|
+
const { sidebarGroups } = useDocsSidebar();
|
|
17
|
+
|
|
18
|
+
const groups = computed(() => sidebarGroups.value.filter((group) => group.items?.length));
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<template>
|
|
22
|
+
<div class="min-h-full bg-white">
|
|
23
|
+
<div class="border-b border-slate-200/80 px-4 pb-4 pt-5">
|
|
24
|
+
<div class="flex min-w-0 items-center gap-3">
|
|
25
|
+
<img
|
|
26
|
+
v-if="logoSrc"
|
|
27
|
+
:src="logoSrc"
|
|
28
|
+
alt=""
|
|
29
|
+
class="block h-7 w-auto max-w-[156px] shrink-0 object-contain"
|
|
30
|
+
>
|
|
31
|
+
<div v-else class="min-w-0 truncate text-base font-semibold tracking-[-0.01em] text-slate-900">
|
|
32
|
+
{{ siteTitle }}
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
|
|
37
|
+
<div class="px-4 pb-6 pt-6">
|
|
38
|
+
<nav aria-label="Sidebar navigation" class="text-sm">
|
|
39
|
+
<section
|
|
40
|
+
v-for="group in groups"
|
|
41
|
+
:key="group.text ?? group.items?.[0]?.link"
|
|
42
|
+
class="mt-6 first:mt-0"
|
|
43
|
+
>
|
|
44
|
+
<h2
|
|
45
|
+
v-if="group.text"
|
|
46
|
+
class="mb-3 flex items-center gap-2.5 text-sm font-medium text-slate-900"
|
|
47
|
+
>
|
|
48
|
+
<DocsIcon
|
|
49
|
+
v-if="group.icon"
|
|
50
|
+
:name="group.icon"
|
|
51
|
+
class="size-4 text-slate-600"
|
|
52
|
+
/>
|
|
53
|
+
{{ group.text }}
|
|
54
|
+
</h2>
|
|
55
|
+
|
|
56
|
+
<ul class="space-y-px">
|
|
57
|
+
<DocsMobileMenuNode
|
|
58
|
+
v-for="item in group.items"
|
|
59
|
+
:key="item.link ?? `${item.text}-${item.icon ?? ''}`"
|
|
60
|
+
:item="item"
|
|
61
|
+
@navigate="$emit('navigate')"
|
|
62
|
+
/>
|
|
63
|
+
</ul>
|
|
64
|
+
</section>
|
|
65
|
+
</nav>
|
|
66
|
+
</div>
|
|
67
|
+
</div>
|
|
68
|
+
</template>
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref, watch } from 'vue';
|
|
3
|
+
import { useData, useRouter, withBase } from 'vitepress';
|
|
4
|
+
import type { SidebarItem } from '../sidebar';
|
|
5
|
+
import { hasActiveSidebarItem, isActivePath } from '../sidebar';
|
|
6
|
+
import DocsIcon from './DocsIcon.vue';
|
|
7
|
+
|
|
8
|
+
const props = withDefaults(defineProps<{
|
|
9
|
+
item: SidebarItem;
|
|
10
|
+
depth?: number;
|
|
11
|
+
}>(), {
|
|
12
|
+
depth: 0,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const emit = defineEmits<{
|
|
16
|
+
navigate: [];
|
|
17
|
+
}>();
|
|
18
|
+
|
|
19
|
+
const { page } = useData();
|
|
20
|
+
const router = useRouter();
|
|
21
|
+
|
|
22
|
+
const hasChildren = computed(() => Boolean(props.item.items?.length));
|
|
23
|
+
const isCurrent = computed(() => isActivePath(page.value.relativePath, props.item.link));
|
|
24
|
+
const hasActiveDescendant = computed(() => {
|
|
25
|
+
return props.item.items?.some((child) => hasActiveSidebarItem(page.value.relativePath, child)) ?? false;
|
|
26
|
+
});
|
|
27
|
+
const isOpen = ref(hasChildren.value ? (!props.item.collapsed || hasActiveDescendant.value) : false);
|
|
28
|
+
|
|
29
|
+
watch(hasActiveDescendant, (value) => {
|
|
30
|
+
if (value) {
|
|
31
|
+
isOpen.value = true;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
function itemHref(link?: string) {
|
|
36
|
+
return link ? withBase(link) : '#';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function navigate(event: MouseEvent, link?: string) {
|
|
40
|
+
if (!link) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
event.preventDefault();
|
|
45
|
+
await router.go(itemHref(link));
|
|
46
|
+
emit('navigate');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function toggleGroup() {
|
|
50
|
+
if (!hasChildren.value) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
isOpen.value = !isOpen.value;
|
|
55
|
+
}
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<template>
|
|
59
|
+
<li class="relative">
|
|
60
|
+
<button
|
|
61
|
+
v-if="hasChildren"
|
|
62
|
+
type="button"
|
|
63
|
+
class="group flex w-full cursor-pointer items-center py-0.5 pr-2 text-left text-sm leading-6 outline-offset-[-1px] transition hover:text-docs-primary"
|
|
64
|
+
:class="hasActiveDescendant
|
|
65
|
+
? 'text-docs-primary'
|
|
66
|
+
: 'text-slate-700'"
|
|
67
|
+
@click="toggleGroup"
|
|
68
|
+
>
|
|
69
|
+
<span class="min-w-0 flex-1 break-words">{{ item.text }}</span>
|
|
70
|
+
<svg
|
|
71
|
+
viewBox="0 0 640 640"
|
|
72
|
+
class="size-3 shrink-0 transition-transform"
|
|
73
|
+
:class="isOpen ? 'rotate-90' : 'rotate-0'"
|
|
74
|
+
aria-hidden="true"
|
|
75
|
+
>
|
|
76
|
+
<path d="M471.1 297.4C483.6 309.9 483.6 330.2 471.1 342.7L279.1 534.7C266.6 547.2 246.3 547.2 233.8 534.7C221.3 522.2 221.3 501.9 233.8 489.4L403.2 320L233.9 150.6C221.4 138.1 221.4 117.8 233.9 105.3C246.4 92.8 266.7 92.8 279.2 105.3L471.2 297.3z" />
|
|
77
|
+
</svg>
|
|
78
|
+
</button>
|
|
79
|
+
|
|
80
|
+
<a
|
|
81
|
+
v-else
|
|
82
|
+
:href="itemHref(item.link)"
|
|
83
|
+
class="group flex w-full cursor-pointer items-center py-0.5 text-left text-sm leading-6 outline-offset-[-1px] transition hover:text-docs-primary"
|
|
84
|
+
:class="isCurrent
|
|
85
|
+
? 'text-docs-primary'
|
|
86
|
+
: 'text-slate-700'"
|
|
87
|
+
@click="navigate($event, item.link)"
|
|
88
|
+
>
|
|
89
|
+
<div class="flex min-w-0 flex-1 items-start gap-x-2.5">
|
|
90
|
+
<DocsIcon
|
|
91
|
+
v-if="item.icon"
|
|
92
|
+
:name="item.icon"
|
|
93
|
+
class="mt-1 size-4 shrink-0 text-slate-500 group-hover:text-slate-700"
|
|
94
|
+
/>
|
|
95
|
+
<div class="flex min-w-0 flex-1 flex-wrap items-center gap-1.5 [word-break:break-word]">
|
|
96
|
+
<span class="min-w-0 max-w-full break-words">{{ item.text }}</span>
|
|
97
|
+
</div>
|
|
98
|
+
</div>
|
|
99
|
+
</a>
|
|
100
|
+
|
|
101
|
+
<ul
|
|
102
|
+
v-if="hasChildren && isOpen"
|
|
103
|
+
:style="{ marginLeft: depth === 0 ? '1rem' : '1.25rem' }"
|
|
104
|
+
>
|
|
105
|
+
<DocsMobileMenuNode
|
|
106
|
+
v-for="child in item.items"
|
|
107
|
+
:key="child.link ?? `${child.text}-${child.icon ?? ''}`"
|
|
108
|
+
:item="child"
|
|
109
|
+
:depth="depth + 1"
|
|
110
|
+
@navigate="emit('navigate')"
|
|
111
|
+
/>
|
|
112
|
+
</ul>
|
|
113
|
+
</li>
|
|
114
|
+
</template>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed } from 'vue';
|
|
3
|
+
import { useData, withBase } from 'vitepress';
|
|
4
|
+
|
|
5
|
+
const { theme } = useData();
|
|
6
|
+
|
|
7
|
+
const code = computed(() => theme.value.notFound?.code ?? '404');
|
|
8
|
+
const title = computed(() => theme.value.notFound?.title ?? 'Page not found');
|
|
9
|
+
const quote = computed(() => {
|
|
10
|
+
return theme.value.notFound?.quote
|
|
11
|
+
?? 'The page you requested does not exist or may have moved.';
|
|
12
|
+
});
|
|
13
|
+
const linkLabel = computed(() => theme.value.notFound?.linkLabel ?? 'Go to home');
|
|
14
|
+
const linkText = computed(() => theme.value.notFound?.linkText ?? 'Take me home');
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<template>
|
|
18
|
+
<section class="flex min-h-[calc(100dvh-14rem)] w-full flex-col items-center justify-center px-6 py-16 text-center sm:px-8 sm:py-24 lg:min-h-[calc(100dvh-10rem)]">
|
|
19
|
+
<p class="text-6xl font-semibold tracking-[-0.04em] text-slate-900 sm:text-7xl">
|
|
20
|
+
{{ code }}
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
<h1 class="mt-3 text-2xl font-semibold tracking-[-0.03em] text-slate-900 sm:text-3xl">
|
|
24
|
+
{{ title }}
|
|
25
|
+
</h1>
|
|
26
|
+
|
|
27
|
+
<div class="mt-6 h-px w-16 bg-slate-200" />
|
|
28
|
+
|
|
29
|
+
<p class="mt-5 max-w-sm text-sm leading-6 text-slate-600">
|
|
30
|
+
{{ quote }}
|
|
31
|
+
</p>
|
|
32
|
+
|
|
33
|
+
<a
|
|
34
|
+
:href="withBase('/')"
|
|
35
|
+
:aria-label="linkLabel"
|
|
36
|
+
class="mt-7 inline-flex items-center rounded-xl border border-docs-primary-border bg-docs-primary-soft px-4 py-2 text-sm font-medium text-docs-primary transition hover:border-docs-primary-border-strong hover:bg-docs-primary-soft-hover"
|
|
37
|
+
>
|
|
38
|
+
{{ linkText }}
|
|
39
|
+
</a>
|
|
40
|
+
</section>
|
|
41
|
+
</template>
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { onContentUpdated, useData } from 'vitepress';
|
|
3
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
|
|
4
|
+
import DocsOutlineItem from './DocsOutlineItem.vue';
|
|
5
|
+
|
|
6
|
+
type OutlineItem = {
|
|
7
|
+
title: string;
|
|
8
|
+
slug: string;
|
|
9
|
+
link: string;
|
|
10
|
+
level: number;
|
|
11
|
+
children: OutlineItem[];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type FlatOutlineItem = OutlineItem & {
|
|
15
|
+
depth: number;
|
|
16
|
+
active: boolean;
|
|
17
|
+
activeDeepest: boolean;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const { frontmatter, theme } = useData();
|
|
21
|
+
|
|
22
|
+
const title = computed(() => {
|
|
23
|
+
const outline = theme.value.outline;
|
|
24
|
+
|
|
25
|
+
return (typeof outline === 'object' && !Array.isArray(outline) && outline?.label)
|
|
26
|
+
|| theme.value.outlineTitle
|
|
27
|
+
|| 'On this page';
|
|
28
|
+
});
|
|
29
|
+
const activeHash = ref<string | null>(null);
|
|
30
|
+
const headers = ref<OutlineItem[]>([]);
|
|
31
|
+
const flattenedHeaders = computed<FlatOutlineItem[]>(() => {
|
|
32
|
+
const activeTrail = findActiveTrail(headers.value, activeHash.value);
|
|
33
|
+
const activeLinks = new Set(activeTrail.map((item) => item.link));
|
|
34
|
+
const activeDeepestLink = activeTrail.at(-1)?.link ?? null;
|
|
35
|
+
|
|
36
|
+
return flattenHeaders(headers.value).map((item) => ({
|
|
37
|
+
...item,
|
|
38
|
+
depth: Math.max(item.level - 2, 0),
|
|
39
|
+
active: activeLinks.has(item.link),
|
|
40
|
+
activeDeepest: item.link === activeDeepestLink,
|
|
41
|
+
}));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function getScrollContainer() {
|
|
45
|
+
return document.getElementById('docs-scroll-container')
|
|
46
|
+
?? document.getElementById('content-container');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getActivationOffset(scrollContainer: HTMLElement) {
|
|
50
|
+
const scrollMarginTop = Number.parseFloat(
|
|
51
|
+
getComputedStyle(document.documentElement).getPropertyValue('--scroll-mt'),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
if (Number.isFinite(scrollMarginTop)) {
|
|
55
|
+
return scrollMarginTop;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return Math.min(Math.max(scrollContainer.clientHeight * 0.18, 56), 120);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function resolveOutlineRange(range: unknown): [number, number] | null {
|
|
62
|
+
if (range === false) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const levelsRange = (typeof range === 'object' && !Array.isArray(range) && range && 'level' in range
|
|
67
|
+
? (range as { level?: number | [number, number] | 'deep'; }).level
|
|
68
|
+
: range) ?? 2;
|
|
69
|
+
|
|
70
|
+
if (levelsRange === 'deep') {
|
|
71
|
+
return [2, 6];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (Array.isArray(levelsRange)) {
|
|
75
|
+
return [levelsRange[0], levelsRange[1]];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return [levelsRange as number, levelsRange as number];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function getVisibleLevels() {
|
|
82
|
+
const range = resolveOutlineRange(frontmatter.value.outline ?? theme.value.outline);
|
|
83
|
+
|
|
84
|
+
if (!range) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return range;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function serializeHeader(element: Element) {
|
|
92
|
+
const ignoredClassNames = /\b(?:VPBadge|header-anchor|footnote-ref|ignore-header)\b/;
|
|
93
|
+
let content = '';
|
|
94
|
+
|
|
95
|
+
for (const node of element.childNodes) {
|
|
96
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
97
|
+
const child = node as HTMLElement;
|
|
98
|
+
|
|
99
|
+
if (ignoredClassNames.test(child.className)) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
content += child.textContent ?? '';
|
|
104
|
+
} else if (node.nodeType === Node.TEXT_NODE) {
|
|
105
|
+
content += node.textContent ?? '';
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return content.trim();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function rebuildHeaders() {
|
|
113
|
+
const levels = getVisibleLevels();
|
|
114
|
+
|
|
115
|
+
if (!levels) {
|
|
116
|
+
headers.value = [];
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const [minLevel, maxLevel] = levels;
|
|
121
|
+
const nodes = Array.from(document.querySelectorAll('.vp-doc :where(h1,h2,h3,h4,h5,h6)'))
|
|
122
|
+
.filter((element) => element instanceof HTMLElement && !!element.id)
|
|
123
|
+
.map((element) => {
|
|
124
|
+
const level = Number(element.tagName.slice(1));
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
title: serializeHeader(element),
|
|
128
|
+
slug: element.id,
|
|
129
|
+
link: `#${element.id}`,
|
|
130
|
+
level,
|
|
131
|
+
children: [],
|
|
132
|
+
} satisfies OutlineItem;
|
|
133
|
+
})
|
|
134
|
+
.filter((item) => item.title && item.level >= minLevel && item.level <= maxLevel);
|
|
135
|
+
|
|
136
|
+
const tree: OutlineItem[] = [];
|
|
137
|
+
const stack: OutlineItem[] = [];
|
|
138
|
+
|
|
139
|
+
for (const node of nodes) {
|
|
140
|
+
while (stack.length && stack[stack.length - 1].level >= node.level) {
|
|
141
|
+
stack.pop();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (stack.length) {
|
|
145
|
+
stack[stack.length - 1].children.push(node);
|
|
146
|
+
} else {
|
|
147
|
+
tree.push(node);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
stack.push(node);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
headers.value = tree;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function flattenHeaders(items: OutlineItem[]): OutlineItem[] {
|
|
157
|
+
return items.flatMap((item) => {
|
|
158
|
+
return [item, ...flattenHeaders(item.children ?? [])];
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function findActiveTrail(items: OutlineItem[], link: string | null): OutlineItem[] {
|
|
163
|
+
if (!link) {
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
for (const item of items) {
|
|
168
|
+
if (item.link === link) {
|
|
169
|
+
return [item];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (item.children?.length) {
|
|
173
|
+
const childTrail = findActiveTrail(item.children, link);
|
|
174
|
+
|
|
175
|
+
if (childTrail.length) {
|
|
176
|
+
return [item, ...childTrail];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return [];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function updateActiveHash() {
|
|
185
|
+
const flattenedHeaders = flattenHeaders(headers.value);
|
|
186
|
+
const scrollContainer = getScrollContainer();
|
|
187
|
+
|
|
188
|
+
if (!flattenedHeaders.length || !scrollContainer) {
|
|
189
|
+
activeHash.value = null;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const scrollTop = scrollContainer.scrollTop;
|
|
194
|
+
const innerHeight = scrollContainer.clientHeight;
|
|
195
|
+
const scrollHeight = scrollContainer.scrollHeight;
|
|
196
|
+
const activationOffset = getActivationOffset(scrollContainer);
|
|
197
|
+
const isBottom = Math.abs(scrollTop + innerHeight - scrollHeight) < 1;
|
|
198
|
+
|
|
199
|
+
if (scrollTop < 1) {
|
|
200
|
+
const locationHash = window.location.hash;
|
|
201
|
+
const matchingHash = flattenedHeaders.some((header) => header.link === locationHash)
|
|
202
|
+
? locationHash
|
|
203
|
+
: null;
|
|
204
|
+
|
|
205
|
+
activeHash.value = matchingHash ?? flattenedHeaders[0]?.link ?? null;
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (isBottom) {
|
|
210
|
+
activeHash.value = flattenedHeaders[flattenedHeaders.length - 1]?.link ?? null;
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const locationHash = window.location.hash;
|
|
215
|
+
const matchingHash = flattenedHeaders.some((header) => header.link === locationHash)
|
|
216
|
+
? locationHash
|
|
217
|
+
: null;
|
|
218
|
+
let currentHash: string | null = null;
|
|
219
|
+
|
|
220
|
+
for (const header of flattenedHeaders) {
|
|
221
|
+
const element = document.getElementById(header.slug);
|
|
222
|
+
|
|
223
|
+
if (!element) {
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const containerTop = scrollContainer.getBoundingClientRect().top;
|
|
228
|
+
const top = scrollTop + element.getBoundingClientRect().top - containerTop;
|
|
229
|
+
|
|
230
|
+
if (top > scrollTop + activationOffset) {
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
currentHash = header.link;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
activeHash.value = currentHash ?? matchingHash ?? flattenedHeaders[0]?.link ?? null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const onScroll = () => {
|
|
241
|
+
updateActiveHash();
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
onMounted(() => {
|
|
245
|
+
const scrollContainer = getScrollContainer();
|
|
246
|
+
|
|
247
|
+
requestAnimationFrame(() => {
|
|
248
|
+
rebuildHeaders();
|
|
249
|
+
updateActiveHash();
|
|
250
|
+
});
|
|
251
|
+
scrollContainer?.addEventListener('scroll', onScroll, { passive: true });
|
|
252
|
+
window.addEventListener('hashchange', onScroll, { passive: true });
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
onBeforeUnmount(() => {
|
|
256
|
+
const scrollContainer = getScrollContainer();
|
|
257
|
+
|
|
258
|
+
scrollContainer?.removeEventListener('scroll', onScroll);
|
|
259
|
+
window.removeEventListener('hashchange', onScroll);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
onContentUpdated(async () => {
|
|
263
|
+
await nextTick();
|
|
264
|
+
rebuildHeaders();
|
|
265
|
+
updateActiveHash();
|
|
266
|
+
});
|
|
267
|
+
</script>
|
|
268
|
+
|
|
269
|
+
<template>
|
|
270
|
+
<nav
|
|
271
|
+
v-if="headers.length"
|
|
272
|
+
id="table-of-contents"
|
|
273
|
+
aria-label="On this page"
|
|
274
|
+
class="space-y-2"
|
|
275
|
+
>
|
|
276
|
+
<button type="button" class="flex cursor-pointer items-center space-x-2 text-sm font-medium text-slate-700 transition-colors hover:text-slate-900">
|
|
277
|
+
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" class="h-3 w-3" aria-hidden="true">
|
|
278
|
+
<path d="M2.5 3.5h11M2.5 8h7M2.5 12.5h11" stroke-linecap="round" />
|
|
279
|
+
</svg>
|
|
280
|
+
<span>{{ title }}</span>
|
|
281
|
+
</button>
|
|
282
|
+
<DocsOutlineItem :items="flattenedHeaders" />
|
|
283
|
+
</nav>
|
|
284
|
+
</template>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
type OutlineItem = {
|
|
3
|
+
title: string;
|
|
4
|
+
link: string;
|
|
5
|
+
depth: number;
|
|
6
|
+
active: boolean;
|
|
7
|
+
activeDeepest: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const props = defineProps<{
|
|
11
|
+
items: OutlineItem[];
|
|
12
|
+
}>();
|
|
13
|
+
|
|
14
|
+
function navigateToHash(event: MouseEvent, link: string) {
|
|
15
|
+
const targetId = link.replace(/^#/, '');
|
|
16
|
+
const target = document.getElementById(targetId);
|
|
17
|
+
|
|
18
|
+
if (!target) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
event.preventDefault();
|
|
23
|
+
target.scrollIntoView({ block: 'start', behavior: 'smooth' });
|
|
24
|
+
window.location.hash = link;
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<template>
|
|
29
|
+
<ul id="table-of-contents-content" class="toc">
|
|
30
|
+
<li
|
|
31
|
+
v-for="item in props.items"
|
|
32
|
+
:key="item.link"
|
|
33
|
+
class="toc-item relative"
|
|
34
|
+
:data-depth="item.depth"
|
|
35
|
+
:data-active="item.active || undefined"
|
|
36
|
+
:data-active-deepest="item.activeDeepest || undefined"
|
|
37
|
+
:class="item.depth > 0
|
|
38
|
+
? item.active
|
|
39
|
+
? 'border-l pl-4 border-docs-primary hover:border-docs-primary'
|
|
40
|
+
: 'border-l pl-4 border-slate-950/5 hover:border-slate-950/20'
|
|
41
|
+
: ''"
|
|
42
|
+
>
|
|
43
|
+
<a
|
|
44
|
+
:href="item.link"
|
|
45
|
+
:style="item.depth > 0 ? 'padding-left:1rem' : undefined"
|
|
46
|
+
class="break-words py-1"
|
|
47
|
+
:class="[
|
|
48
|
+
item.depth > 0
|
|
49
|
+
? 'group flex items-start whitespace-pre-wrap'
|
|
50
|
+
: 'block border-l pl-4 font-medium',
|
|
51
|
+
item.active
|
|
52
|
+
? item.depth > 0
|
|
53
|
+
? 'text-docs-primary'
|
|
54
|
+
: 'text-docs-primary border-docs-primary hover:border-docs-primary'
|
|
55
|
+
: item.depth > 0
|
|
56
|
+
? 'text-gray-500 hover:text-gray-900'
|
|
57
|
+
: 'border-slate-950/5 hover:border-slate-950/20 hover:text-gray-900',
|
|
58
|
+
]"
|
|
59
|
+
@click="navigateToHash($event, item.link)"
|
|
60
|
+
>
|
|
61
|
+
{{ item.title }}
|
|
62
|
+
</a>
|
|
63
|
+
</li>
|
|
64
|
+
</ul>
|
|
65
|
+
</template>
|