Skip to content

Auto‑insert <a.header‑anchor> for headings rendered by Vue components #4867

@alexchexes

Description

@alexchexes

Is your feature request related to a problem? Please describe.

When a heading comes from a Vue component, it gets an id and shows up in the "On this page" outline, but the invisible <a class="header-anchor">, which appears on mouse hover, is missing.

Minimal repro:

docs/index.md

<script setup lang="ts">
import AComponent from './AComponent.vue'
</script>

## foo {#foo}

<AComponent />

docs/AComponent.vue

<template>
  <h2 id="bar">bar</h2>
  <h2 id="baz">baz</h2>
</template>

Result: ## bar and ## baz appear in the outline and are linkable, but they have no header‑anchor element, so users cannot easily copy a permalink, and the UI feels a bit inconsistent when hovering the mouse over headings added by the component and the ones written in Markdown.

Describe the solution you'd like

During the same client‑side pass that builds the outline, scan for any <h1‑h6> with an id but lacking .header-anchor, and inject an anchor with the same markup VitePress generates for Markdown headings.

Describe alternatives you've considered

Manually adding the anchor works...

<h2 id="qux">
  qux
  <a class="header-anchor" href="#qux" aria-label='Permalink to "qux {#qux}"'>&ZeroWidthSpace;</a>
</h2>

...but is somewhat boilerplate and is brittle. It must be manually added to each place where <h1‑h6> is used and will break if the theme changes.

Additional context

Screencast of how the repro example looks:

chrome_2025-07-30--19-07-12--488.mp4

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions