Skip to content

Commit b7d1636

Browse files
florian-lefebvredelucisyanthomasdevsarah11918
authored
[v6] stabilize experimental.headingIdCompat (#12510)
Co-authored-by: Chris Swithinbank <[email protected]> Co-authored-by: Yan <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent 7962307 commit b7d1636

File tree

7 files changed

+128
-372
lines changed

7 files changed

+128
-372
lines changed

astro.sidebar.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ export const sidebar = [
147147
'reference/experimental-flags/live-content-collections',
148148
'reference/experimental-flags/client-prerender',
149149
'reference/experimental-flags/content-intellisense',
150-
'reference/experimental-flags/heading-id-compat',
151150
'reference/experimental-flags/chrome-devtools-workspace',
152151
],
153152
}),

src/content/docs/en/guides/upgrade-to/v6.mdx

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,14 @@ The following experimental flags have been removed and **their corresponding fea
420420

421421
Remove these experimental flags from your Astro config if you were previously using them:
422422

423-
```js del={5-6} title="astro.config.mjs"
423+
```js del={5-7} title="astro.config.mjs"
424424
import { defineConfig } from 'astro/config';
425425

426426
export default defineConfig({
427427
experimental: {
428428
preserveScriptOrder: true,
429429
staticImportMetaEnv: true,
430+
headingIdCompat: true,
430431
},
431432
})
432433
```
@@ -536,7 +537,7 @@ Review your links to your custom endpoints that include a file extension in the
536537

537538
<SourcePR number="14485" title="feat: stabilize static import meta env"/>
538539

539-
In Astro 5.13, the `experimental.staticImportMetaEnv` was introduced to update the behavior when accessing `import.meta.env` directly to align with [Vite's handling of environment variables](https://vite.dev/guide/env-and-mode.html#env-variables) and ensures that `import.meta.env` values are always inlined.
540+
In Astro 5.13, the `experimental.staticImportMetaEnv` flag was introduced to update the behavior when accessing `import.meta.env` directly to align with [Vite's handling of environment variables](https://vite.dev/guide/env-and-mode.html#env-variables) and ensures that `import.meta.env` values are always inlined.
540541

541542
In Astro 5.x, non-public environment variables were replaced by a reference to `process.env`. Additionally, Astro could also convert the value type of your environment variables used through `import.meta.env`, which could prevent access to some values such as the strings `"true"` (which was converted to a boolean value), and `"1"` (which was converted to a number).
542543

@@ -585,6 +586,131 @@ If you need more control over environment variables in Astro, we recommend you u
585586

586587
<ReadMore>Learn more about [environment variables](/en/guides/environment-variables/) in Astro, including `astro:env`.</ReadMore>
587588

589+
### Changed: Markdown heading ID generation
590+
591+
<SourcePR number="14494" title="feat!: stabilize experimental.headingIdCompat"/>
592+
593+
In Astro 5.x, an additional default processing step to Markdown stripped trailing hyphens from the end of IDs for section headings ending in special characters. This provided a cleaner `id` value, but could lead to incompatibilities rendering your Markdown across platforms.
594+
595+
In Astro 5.5, the `experimental.headingIdCompat` flag was introduced to allow you to make the IDs generated by Astro for Markdown headings compatible with common platforms like GitHub and npm, using the popular [`github-slugger`](https://github.com/Flet/github-slugger) package.
596+
597+
Astro 6.0 removes this experimental flag and makes this the new default behavior in Astro: trailing hyphens from the end of IDs for headings ending in special characters are no longer removed.
598+
599+
#### What should I do?
600+
601+
If you have manual links to headings, you may need to update the anchor link value with a new trailing hyphen. For example, the following Markdown heading:
602+
603+
```md
604+
## `<Picture />`
605+
```
606+
607+
will now generate the following HTML with a trailing hyphen in the heading `id`:
608+
609+
```html ins="-"
610+
<h2 id="picture-"><code>&lt;Picture /&gt;</h2>
611+
```
612+
613+
and must now be linked to as:
614+
615+
```markdown ins="-"
616+
See [the Picture component](/en/guides/images/#picture-) for more details.
617+
```
618+
619+
If you were previously using the experimental feature to enforce trailing hyphens, you must [remove this experimental flag from your configuration](#experimental-flags) as it no longer exists.
620+
621+
If you were previously using the `rehypeHeadingIds` plugin directly to enforce compatibility, remove the `headingIdCompat` option as it no longer exists:
622+
623+
```js title="astro.config.mjs" del={8} ins={9}
624+
import { defineConfig } from 'astro/config';
625+
import { rehypeHeadingIds } from '@astrojs/markdown-remark';
626+
import { otherPluginThatReliesOnHeadingIDs } from 'some/plugin/source';
627+
628+
export default defineConfig({
629+
markdown: {
630+
rehypePlugins: [
631+
[rehypeHeadingIds, { headingIdCompat: true }],
632+
[rehypeHeadingIds],
633+
otherPluginThatReliesOnHeadingIDs,
634+
],
635+
},
636+
});
637+
```
638+
639+
If you want to keep the old ID generation for backward compatibility reasons, you can create a custom rehype plugin that will generate headings IDs like Astro 5.x. This will allow you to continue to use your existing anchor links without adding trailing hyphens.
640+
641+
<details>
642+
643+
<summary>Create a custom rehype plugin to strip trailing hyphens</summary>
644+
645+
<Steps>
646+
647+
1. Install required dependencies:
648+
649+
<PackageManagerTabs>
650+
<Fragment slot="npm">
651+
```sh
652+
npm i github-slugger hast-util-heading-rank unist-util-visit hast-util-to-string
653+
```
654+
</Fragment>
655+
<Fragment slot="pnpm">
656+
```sh
657+
pnpm add github-slugger hast-util-heading-rank unist-util-visit hast-util-to-string
658+
```
659+
</Fragment>
660+
<Fragment slot="yarn">
661+
```sh
662+
yarn add github-slugger hast-util-heading-rank unist-util-visit hast-util-to-string
663+
```
664+
</Fragment>
665+
</PackageManagerTabs>
666+
667+
2. Create a custom rehype plugin that will generate headings IDs like Astro v5:
668+
669+
```js title="plugins/rehype-slug.mjs"
670+
import GithubSlugger from 'github-slugger';
671+
import { headingRank } from 'hast-util-heading-rank';
672+
import { visit } from 'unist-util-visit';
673+
import { toString } from 'hast-util-to-string';
674+
675+
const slugs = new GithubSlugger();
676+
677+
export function rehypeSlug() {
678+
/**
679+
* @param {import('hast').Root} tree
680+
*/
681+
return (tree) => {
682+
slugs.reset();
683+
visit(tree, 'element', (node) => {
684+
if (headingRank(node) && !node.properties.id) {
685+
let slug = slugs.slug(toString(node));
686+
// Strip trailing hyphens like in Astro v5 and below:
687+
if (slug.endsWith('-')) slug = slug.slice(0, -1);
688+
node.properties.id = slug;
689+
}
690+
});
691+
};
692+
}
693+
```
694+
695+
3. Add the custom plugin to your Markdown configuration in `astro.config.mjs`:
696+
697+
```js title="astro.config.mjs" ins={2} ins="rehypeSlug"
698+
import { defineConfig } from 'astro/config';
699+
import { rehypeSlug } from './plugins/rehype-slug';
700+
701+
export default defineConfig({
702+
markdown: {
703+
rehypePlugins: [rehypeSlug],
704+
},
705+
});
706+
```
707+
708+
</Steps>
709+
710+
</details>
711+
712+
<ReadMore>Learn more about [Heading IDs](/en/guides/markdown-content/#heading-ids).</ReadMore>
713+
588714
## Community Resources
589715

590716
Know a good resource for Astro v5.0? [Edit this page](https://github.com/withastro/docs/edit/main/src/content/docs/en/guides/upgrade-to/v6.mdx) and add a link below!

src/content/docs/en/reference/experimental-flags/heading-id-compat.mdx

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/content/docs/es/reference/experimental-flags/heading-id-compat.mdx

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/content/docs/fr/reference/experimental-flags/heading-id-compat.mdx

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)