Skip to content

Prevent heading anchor links on non-Starlight content#3181

Merged
HiDeoo merged 3 commits intowithastro:mainfrom
HiDeoo:hd-fix-heading-links-docs-cc
May 8, 2025
Merged

Prevent heading anchor links on non-Starlight content#3181
HiDeoo merged 3 commits intowithastro:mainfrom
HiDeoo:hd-fix-heading-links-docs-cc

Conversation

@HiDeoo
Copy link
Copy Markdown
Member

@HiDeoo HiDeoo commented May 6, 2025

Description

In Starlight 0.34.0, support for generating clickable anchor links for headings was added. For Markdown and MDX content, a rehype plugin is used to generate the links.

When the feature is not disabled, this plugin runs unconditionally, which means that these links are generated for non-Starlight content like individual Markdown page or content not from the Starlight docs content collection.

This PR prevents this behavior by checking if the virtual file path is part of the Starlight docs content collection first, and bails if it is not.

For testing, I relied on E2E tests as it seems that on Windows, file path separators are inconsistent when the rehype plugin is used on Markdown vs MDX files. I'll investigate and open a separate issue/PR for that on the Astro repo.

Here is a preview of the changes before and after the fix for an individual Markdown page:

Before After
SCR-20250506-pgpd SCR-20250506-pgre

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented May 6, 2025

🦋 Changeset detected

Latest commit: 9d798dd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label May 6, 2025
@netlify
Copy link
Copy Markdown

netlify bot commented May 6, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 9d798dd
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/681a2f1218185c00083687cf
😎 Deploy Preview https://deploy-preview-3181--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@HiDeoo HiDeoo marked this pull request as ready for review May 6, 2025 15:52
Copy link
Copy Markdown
Contributor

@Eveeifyeve Eveeifyeve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I hate function in functions since the functions could be changed in the future, but it looks good.

@HiDeoo HiDeoo merged commit 449c822 into withastro:main May 8, 2025
15 checks passed
@astrobot-houston astrobot-houston mentioned this pull request May 8, 2025
HiDeoo added a commit to hippotastic/starlight that referenced this pull request May 14, 2025
* main: (151 commits)
  i18n(fr): update slot translation and update Astro Docs links (withastro#3196)
  i18n(ko-KR): update `plugins.mdx` (withastro#3197)
  i18n(fr): update `resources/plugins` (withastro#3195)
  chore(deps): update changesets/action action to v1.5.2 (withastro#3191)
  docs(plugins) Add a link to starlight-scroll-to-top plugin (withastro#3194)
  [ci] format
  docs(showcase): add fluid-dnd showcase (withastro#3193)
  [ci] format
  Cookie API website for Showcase (withastro#3188)
  chore(deps): update changesets/action action to v1.5.1 (withastro#3187)
  [ci] format
  add showcase sonar (withastro#3183)
  [ci] release (withastro#3166)
  Prevent heading anchor links on non-Starlight content (withastro#3181)
  i18n(de): update `css-and-tailwind.mdx` (withastro#3174)
  chore(deps): update changesets/action action to v1.5.0 (withastro#3179)
  i18n(ko-KR): update Korean UI strings (withastro#3168)
  i18n(fr): update `guides/css-and-tailwind.mdx` (withastro#3173)
  i18n(ko-KR): update `css-and-tailwind.mdx` (withastro#3172)
  docs: update Tailwind manual installation instructions (withastro#3170)
  ...
Yoxnear pushed a commit to Yoxnear/starlight-custom that referenced this pull request Jul 23, 2025
Co-authored-by: Eveeifyeve <88671402+Eveeifyeve@users.noreply.github.com>
Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>
Co-authored-by: ematipico <602478+ematipico@users.noreply.github.com>
Co-authored-by: MoustaphaDev <81974850+MoustaphaDev@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🌟 core Changes to Starlight’s main package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown Headings not appearing correctly for non Starlight Content

5 participants