Skip to content

Commit 01f5578

Browse files
authored
docs: expand details for global CLI anchor links (#2715)
The Global CLI page lists environment-variable headings in its outline even when the enclosing details block is collapsed. Clicking those links leaves the content hidden and scrolls to the wrong position. Add a page-local script that opens enclosing details blocks and scrolls to the heading with VitePress's configured offset. It handles direct links, hash changes, and clicking the current outline link again after collapsing the block, and removes its listeners when leaving the page. This adapts [VitePress maintainer brc-dd's suggested script](vuejs/vitepress#4252 (comment)) to headings inside details blocks. 🤖 Generated with Codex
1 parent c556257 commit 01f5578

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

docs/guide/global-cli.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
<script setup lang="ts">
2+
import { getScrollOffset } from 'vitepress';
3+
import { nextTick, onMounted, onUnmounted } from 'vue';
4+
5+
function openTarget() {
6+
let target: HTMLElement | null;
7+
try {
8+
target = document.getElementById(decodeURIComponent(location.hash.slice(1)));
9+
} catch {
10+
return;
11+
}
12+
if (!target?.closest('details')) return;
13+
14+
for (let details = target.closest('details'); details; details = details.parentElement?.closest('details') ?? null) {
15+
details.open = true;
16+
}
17+
18+
// VitePress cannot measure a heading inside closed details. Correct the scroll after revealing it.
19+
requestAnimationFrame(() => {
20+
if (!target.isConnected) return;
21+
const top = window.scrollY + target.getBoundingClientRect().top - getScrollOffset()
22+
+ Number.parseInt(window.getComputedStyle(target).paddingTop, 10);
23+
window.scrollTo(0, top);
24+
});
25+
}
26+
27+
function onAnchorClick(event: MouseEvent) {
28+
if (event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
29+
const link = event.target instanceof Element ? event.target.closest('a') : null;
30+
// Clicking the current hash again does not emit hashchange.
31+
if (link?.href === location.href) openTarget();
32+
}
33+
34+
onMounted(async () => {
35+
window.addEventListener('hashchange', openTarget);
36+
document.addEventListener('click', onAnchorClick);
37+
await nextTick();
38+
openTarget();
39+
});
40+
41+
onUnmounted(() => {
42+
window.removeEventListener('hashchange', openTarget);
43+
document.removeEventListener('click', onAnchorClick);
44+
});
45+
</script>
46+
147
# Global CLI
248

349
The global CLI is a standalone `vp` binary for machine-level runtime and package management. It includes a Vite+ toolchain, does not require Node.js to be installed first, and can be used without adding `vite-plus` to a project.

0 commit comments

Comments
 (0)