Skip to content
Open
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
123 changes: 121 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,124 @@ 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.5, the `experimental.headingIdCompat` flag was introduced 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.

In Astro 5.x, Astro had an additional default processing step that stripped trailing hyphens from the end of IDs for headings ending in special characters.

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 aren't stripped anymore.

#### What should I do?

If you were previously using this experimental feature, 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, 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 have manual links to headings, review them to make sure they're still correct. For example, the following Markdown heading:

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

will generate the following HTML:

```html del={1} ins={2}
<h2 id="picture"><code>&lt;Picture /&gt;</h2>
<h2 id="picture-"><code>&lt;Picture /&gt;</h2>
```

<details>

<summary>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. Click to expand.</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"
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a reason why you used js instead of ts?

Copy link
Member

Choose a reason for hiding this comment

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

Mostly because that's what most of our examples do. But also because it means you can inline it in your config file if you like even if that is .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