Skip to content

Conversation

@delucis
Copy link
Member

@delucis delucis commented Apr 4, 2025

Description

Where does this apply?

  1. For Markdown and MDX files, anchor links are added using rehype plugins. This can be disabled with markdown: { headingLinks: false } in Starlight config.
  2. For Markdoc files, anchor links are added with a custom renderer for the heading node. This can be disabled by passing { headingLinks: false } to the Starlight Markdoc preset.
  3. In Astro components, anchor links can be added with a new <AnchorHeading> component.

The anchor link approach

The HTML mark-up approach the anchor links follow is based on Amber Wilson’s Are your Anchor Links Accessible? blog post. (Thank you for sharing your research, Amber!)

The output looks a little like this:

<div>
  <h2 id="a-heading">A heading</h2>
  <a href="#a-heading">
    <span class="sr-only">Section titled “A heading”</span>
    <svg>...</svg>
  </a>
</div>
  • The <a> link is placed after the heading element rather than inside it or even wrapping it as some sites do. This ensures that the heading doesn’t contain any additional text (for screenreaders listing headings for example) and also avoids the potentially confusing situation of a link which links to itself.
  • The accessible “Section titled…” label uses Starlight’s internationalization APIs so it can be localized.
  • The heading and link are wrapped in a <div> so that we can position the icon link inline at the end of the heading as desired.
  • There are more notes about the actual styles for the layout in anchor-links.css in this PR.

To-do

  • Write tests for the rehype plugins.
  • Write tests to assert that component and plugin markup matches.
  • Fix the use of rehypeSlug and use Astro’s built-in heading slugger instead.
  • Add changesets

@changeset-bot
Copy link

changeset-bot bot commented Apr 4, 2025

🦋 Changeset detected

Latest commit: 9af5fae

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

This PR includes changesets to release 2 packages
Name Type
@astrojs/starlight Minor
@astrojs/starlight-markdoc Minor

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

@netlify
Copy link

netlify bot commented Apr 4, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 9af5fae
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/67ff79297f4e020008c68bb2
😎 Deploy Preview https://deploy-preview-3033--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: 98 (🔴 down 2 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.

@astrobot-houston
Copy link
Contributor

astrobot-houston commented Apr 4, 2025

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

Locale File Note
en guides/authoring-content.mdx Source changed, localizations will be marked as outdated.
en guides/pages.mdx Source changed, localizations will be marked as outdated.
en reference/configuration.mdx Source changed, localizations will be marked as outdated.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@astrobot-houston
Copy link
Contributor

astrobot-houston commented Apr 4, 2025

size-limit report 📦

Path Size
/index.html 7.2 KB (+3.5% 🔺)
/_astro/*.js 25.76 KB (0%)
/_astro/*.css 14.21 KB (+1.7% 🔺)

@github-actions github-actions bot added 📚 docs Documentation website changes 🌟 core Changes to Starlight’s main package 🌟 markdoc Changes to Starlight’s Markdoc package labels Apr 4, 2025
@Arhell
Copy link

Arhell commented Apr 4, 2025

@delucis thanks for your work, and will it be possible to change the icon to another one, for example this one #
in the config, or already in the component?

…tion ourselves

`rehype-autolink-headings` handles a lot of options and variants we have no need for, so this should be a bit more performant by avoiding a bunch of unnecessary logic and also avoids pulling in several extra dependencies.
@delucis
Copy link
Member Author

delucis commented Apr 4, 2025

will it be possible to change the icon to another one, for example this one # in the config

Probably not in this initial version. I will wait for #3024 first as that will make supporting configurable icons much easier.

The Astro docs already uses the “Section titled…” accessible label format and this commit brings across those where I was confident I could get the quotation style right.
@delucis delucis marked this pull request as ready for review April 10, 2025 20:54
@delucis
Copy link
Member Author

delucis commented Apr 10, 2025

One question I asked myself while playing with the PR is if any special considerations should be made for pages using the splash template? I personally don't think so after thinking about it more.

Ah yeah, I think it should behave the same (fortunately so, as I’m not sure how to do it if they shouldn’t 😅). Not documented currently (maybe it should be?), but anchor links by definition are only added to headings that have an id — either an auto-generated one for Markdown headings or an explicitly added custom one (e.g. someone authors <h2 id="important">This heading is important</h2>). This also means you can opt out of headings being linked like this by authoring, e.g. <h2>This heading has no anchor link</h2>. This is what is unintentionally, but to good effect, happening on the Starlight landing page for example.

@delucis
Copy link
Member Author

delucis commented Apr 15, 2025

Got a failing size limit check when updating branch. Looks like the Expressive Code update in #3109 bumped CSS size by ~600 bytes taking the additions in this PR over the edge, so I bumped our budget up by 250 bytes.

Copy link
Member Author

Choose a reason for hiding this comment

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

@HiDeoo based on your experience in #2322 can you see anything obvious that needs doing to these styles other than wrapping them in @layer starlight.content?

Copy link
Member

Choose a reason for hiding this comment

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

I would not think so. The import 'virtual:starlight/optional-css'; import in Page.astro should still be near the end after import '../style/util.css'; so no change required here.

Altho, we should definitely confirm that before merging the last one of the two.

Copy link
Member

Choose a reason for hiding this comment

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

Just tested locally merging this branch into #2322 for sanity and indeed, I only had to wrap the styles in the starlight.content layer, couldn't spot any issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

Awesome, thank you for testing! Pushed that change up so it’s ready to merge.

@delucis delucis added the ✅ approved Pull requests that have been approved and are ready to merge when next cutting a release label Apr 16, 2025
@delucis
Copy link
Member Author

delucis commented Apr 16, 2025

Merged in the cascade layer changes and tested the deploy preview and all looking good 👍

@delucis delucis merged commit 8c19678 into main Apr 16, 2025
16 checks passed
@delucis delucis deleted the chris/anchor-headings branch April 16, 2025 09:37
@astrobot-houston astrobot-houston mentioned this pull request Apr 16, 2025
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Apr 16, 2025
* main:
  [ci] release (withastro#3124)
  Fix E2E tests broken by formatter
  [ci] format
  Add built-in heading anchor link support (withastro#3033)
  i18n(fr): update `environmental-impact` (withastro#3123)
  Cascade Layers (withastro#2322)
  Fix image metadata validation in StarlightPage schema (withastro#3118)
  Don’t set defaults for `attrs` and `content` in head entries (withastro#3122)
  [ci] format
  i18n(es) Translate `route-data` (withastro#3071)
Yoxnear pushed a commit to Yoxnear/starlight-custom that referenced this pull request Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✅ approved Pull requests that have been approved and are ready to merge when next cutting a release 🌟 core Changes to Starlight’s main package 📚 docs Documentation website changes 🌟 markdoc Changes to Starlight’s Markdoc package 🌟 minor Change that triggers a minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants