Skip to content

Prevent heading anchor links on non-Starlight content #3181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 8, 2025

Conversation

HiDeoo
Copy link
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

Copy link

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
Copy link

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
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
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