Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion astro.sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export const sidebar = [
'reference/experimental-flags/live-content-collections',
'reference/experimental-flags/client-prerender',
'reference/experimental-flags/content-intellisense',
'reference/experimental-flags/heading-id-compat',
'reference/experimental-flags/chrome-devtools-workspace',
],
}),
Expand Down
130 changes: 128 additions & 2 deletions src/content/docs/en/guides/upgrade-to/v6.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,14 @@ The following experimental flags have been removed and **their corresponding fea

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

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

export default defineConfig({
experimental: {
preserveScriptOrder: true,
staticImportMetaEnv: true,
headingIdCompat: true,
},
})
```
Expand Down Expand Up @@ -536,7 +537,7 @@ Review your links to your custom endpoints that include a file extension in the

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

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

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

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

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

### Changed: Markdown heading ID generation

<SourcePR number="14494" title="feat!: stabilize experimental.headingIdCompat"/>

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.

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.

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.

#### What should I do?

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:

```md
## `<Picture />`
```

will now generate the following HTML with a trailing hyphen in the heading `id`:

```html ins="-"
<h2 id="picture-"><code>&lt;Picture /&gt;</h2>
```

and must now be linked to as:

```markdown ins="-"
See [the Picture component](/en/guides/images/#picture-) for more details.
```

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.

If you were previously using the `rehypeHeadingIds` plugin directly to enforce compatibility, remove the `headingIdCompat` option as it no longer exists:

```js title="astro.config.mjs" del={8} ins={9}
import { defineConfig } from 'astro/config';
import { rehypeHeadingIds } from '@astrojs/markdown-remark';
import { otherPluginThatReliesOnHeadingIDs } from 'some/plugin/source';

export default defineConfig({
markdown: {
rehypePlugins: [
[rehypeHeadingIds, { headingIdCompat: true }],
[rehypeHeadingIds],
otherPluginThatReliesOnHeadingIDs,
],
},
});
```

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.

<details>

<summary>Create a custom rehype plugin to strip trailing hyphens</summary>

<Steps>

1. Install required dependencies:

<PackageManagerTabs>
<Fragment slot="npm">
```sh
npm i github-slugger hast-util-heading-rank unist-util-visit hast-util-to-string
```
</Fragment>
<Fragment slot="pnpm">
```sh
pnpm add github-slugger hast-util-heading-rank unist-util-visit hast-util-to-string
```
</Fragment>
<Fragment slot="yarn">
```sh
yarn add github-slugger hast-util-heading-rank unist-util-visit hast-util-to-string
```
</Fragment>
</PackageManagerTabs>

2. Create a custom rehype plugin that will generate headings IDs like Astro v5:

```js title="plugins/rehype-slug.mjs"
import GithubSlugger from 'github-slugger';
import { headingRank } from 'hast-util-heading-rank';
import { visit } from 'unist-util-visit';
import { toString } from 'hast-util-to-string';

const slugs = new GithubSlugger();

export function rehypeSlug() {
/**
* @param {import('hast').Root} tree
*/
return (tree) => {
slugs.reset();
visit(tree, 'element', (node) => {
if (headingRank(node) && !node.properties.id) {
let slug = slugs.slug(toString(node));
// Strip trailing hyphens like in Astro v5 and below:
if (slug.endsWith('-')) slug = slug.slice(0, -1);
node.properties.id = slug;
}
});
};
}
```

3. Add the custom plugin to your Markdown configuration in `astro.config.mjs`:

```js title="astro.config.mjs" ins={2} ins="rehypeSlug"
import { defineConfig } from 'astro/config';
import { rehypeSlug } from './plugins/rehype-slug';

export default defineConfig({
markdown: {
rehypePlugins: [rehypeSlug],
},
});
```

</Steps>

</details>

<ReadMore>Learn more about [Heading IDs](/en/guides/markdown-content/#heading-ids).</ReadMore>

## Community Resources

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!
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading