Skip to content

Commit

Permalink
Add built-in integrations conditionally (#796)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
  • Loading branch information
HiDeoo and delucis committed Oct 6, 2023
1 parent 9eebaf7 commit 372ec96
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
11 changes: 11 additions & 0 deletions .changeset/happy-tips-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@astrojs/starlight': minor
---

Add the `@astrojs/sitemap` and `@astrojs/mdx` integrations only if they are not detected in the Astro configuration.

鈿狅笍 **BREAKING CHANGE** The minimum supported version of Astro is now v3.2.0. Make sure you update Astro at the same time as updating Starlight:

```sh
npm install astro@latest
```
25 changes: 10 additions & 15 deletions packages/starlight/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { errorMap } from './utils/error-map';
import { StarlightConfigSchema, type StarlightUserConfig } from './utils/user-config';
import { rehypeRtlCodeSupport } from './integrations/code-rtl-support';

export default function StarlightIntegration(opts: StarlightUserConfig): AstroIntegration[] {
export default function StarlightIntegration(opts: StarlightUserConfig): AstroIntegration {
const parsedConfig = StarlightConfigSchema.safeParse(opts, { errorMap });

if (!parsedConfig.success) {
Expand All @@ -34,7 +34,15 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
pattern: '[...slug]',
entryPoint: '@astrojs/starlight/index.astro',
});
const integrations: AstroIntegration[] = [];
if (!config.integrations.find(({ name }) => name === '@astrojs/sitemap')) {
integrations.push(starlightSitemap(userConfig));
}
if (!config.integrations.find(({ name }) => name === '@astrojs/mdx')) {
integrations.push(mdx());
}
const newConfig: AstroUserConfig = {
integrations,
vite: {
plugins: [vitePluginStarlightUserConfig(userConfig, config)],
},
Expand All @@ -50,19 +58,6 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
updateConfig(newConfig);
},

'astro:config:done': ({ config }) => {
const integrations = config.integrations.map(({ name }) => name);
for (const builtin of ['@astrojs/mdx', '@astrojs/sitemap']) {
if (integrations.filter((name) => name === builtin).length > 1) {
throw new Error(
`Found more than one instance of ${builtin}.\n` +
`Starlight includes ${builtin} by default.\n` +
'Please remove it from your integrations array in astro.config.mjs'
);
}
}
},

'astro:build:done': ({ dir }) => {
const targetDir = fileURLToPath(dir);
const cwd = dirname(fileURLToPath(import.meta.url));
Expand All @@ -78,5 +73,5 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
},
};

return [starlightSitemap(userConfig), Starlight, mdx()];
return Starlight;
}
2 changes: 1 addition & 1 deletion packages/starlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"./404.astro": "./404.astro"
},
"peerDependencies": {
"astro": "^3.0.0"
"astro": "^3.2.0"
},
"devDependencies": {
"@types/node": "^18.16.19",
Expand Down

0 comments on commit 372ec96

Please sign in to comment.