#Adding global imports in Starlight
1 messages · Page 1 of 1 (latest)
Hi @hasty notch. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, you can add global imports to MDX files in Starlight by configuring the MDX integration that Starlight uses under the hood. Starlight is built on top of Astro and includes the @astrojs/mdx integration by default.
You can customize the MDX configuration by using a Starlight plugin. Here's how to do it:
- Create a custom plugin for Starlight that configures the MDX integration:
// starlight-mdx-globals.js
export default {
name: 'starlight-mdx-globals',
hooks: {
'config:setup': ({ updateConfig, config }) => {
// Get the existing MDX configuration if any
const existingMdx = config.markdown?.mdx || {};
updateConfig({
markdown: {
mdx: {
...existingMdx,
// Add your global imports here
globalImports: [
// Example: import a component in all MDX files
'@components/MyComponent',
],
},
},
});
},
},
};
- Then add this plugin to your Starlight configuration in
astro.config.mjs:
import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';
import starlightMdxGlobals from './starlight-mdx-globals.js';
export default defineConfig({
integrations: [
starlight({
title: 'My Docs',
// Add your custom plugin to Starlight
plugins: [starlightMdxGlobals],
}),
],
});
This approach leverages Starlight's plugin system, which allows you to customize the configuration while maintaining compatibility with Starlight's built-in features. The plugin API gives you access to update the configuration that Starlight uses internally.
For more information about Starlight plugins, you can refer to the Plugins Reference and see examples in the plugins showcase.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@hasty notch Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@uncut hearth This didn't seem to work, I am getting messages that my component is not defined.
Let me check the knowledge base for you again. One moment! :books:
I'm sorry to hear that you're experiencing issues with the component not being defined. This is a common problem when working with MDX files in Astro. Let me help you troubleshoot this issue.
When you see "component is not defined" errors in MDX files, there are a few potential causes:
Common MDX Component Issues
-
Import path issues: The global import path might be incorrect or the component might not be accessible from where MDX is trying to import it.
-
Missing component export: The component needs to be properly exported from its source file.
-
MDX configuration: The
globalImportsmight not be correctly applied to the MDX integration.
Troubleshooting Steps
Here are some specific steps to fix the issue:
-
Check for import errors: Make sure the component path in your
globalImportsis correct and accessible. Absolute paths with aliases need to be properly configured in yourtsconfig.jsonorjsconfig.json.// Use full relative paths from the project root globalImports: [ './src/components/MyComponent.astro', ], -
Verify component is properly defined: Ensure your component is correctly exported from its file.
-
Try explicit imports first: Before using global imports, test if you can import the component directly in an MDX file to confirm it works.
--- title: Test Page --- import MyComponent from '../components/MyComponent.astro'; <MyComponent /> -
Check for common errors: As mentioned in the Astro troubleshooting guide, make sure:
- Your import has the correct path
- The import name matches the component name
- You've included the file extension for non-JS/TS files
- Verify MDX integration: If you're still having issues, make sure the MDX integration is properly installed. Starlight includes it by default, but you might need to check for any MDX integration errors.
If you're still experiencing issues, could you share:
- The exact error message you're seeing
- The structure of your component
- The path you're using in the
globalImportsarray
This would help me provide more specific guidance for your situation.
Remember that Starlight is built on Astro, so many of the common Astro troubleshooting steps apply here as well.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@hasty notch Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@uncut hearth The import I've added to globalImports is import { SiBun, SiCloudflareworkers, SiDeno, SiNodedotjs } from '@icons-pack/react-simple-icons'; I have tested this using explicit imports and it works. The error I'm getting is Expected component SiBun to be defined: you likely forgot to import, pass, or provide it. I tried outputting the config object the plugin is modifying and it doesn't have anything about markdown or mdx in it, so I think the config I'm adding is being ignored.
Let me check the knowledge base for you again. One moment! :books:
I see the issue now. It looks like there are two problems: the React component import isn't being recognized in MDX, and the configuration you're adding might not be properly applied to Starlight's MDX setup.
Let's address both issues:
1. React Component in Starlight MDX
First, when using React components (like those from @icons-pack/react-simple-icons) in Starlight, you need to make sure the React integration is properly set up. Starlight uses Astro under the hood, and Astro requires explicit integration for React components.
Here's a revised approach:
// starlight-mdx-globals.js
import react from '@astrojs/react';
export default {
name: 'starlight-mdx-globals',
hooks: {
'config:setup': ({ addIntegration, updateConfig, astroConfig }) => {
// Check if React integration is already added
const isReactLoaded = astroConfig.integrations.find(
({ name }) => name === '@astrojs/react'
);
// Add React integration if not already present
if (!isReactLoaded) {
addIntegration(react());
}
// Update MDX configuration
updateConfig({
markdown: {
mdx: {
// Add your global imports here
globalImports: [
// Import React icons
"import { SiBun, SiCloudflareworkers, SiDeno, SiNodedotjs } from '@icons-pack/react-simple-icons';"
],
},
},
});
},
},
};
2. Debugging Configuration
If you're seeing that the config object doesn't have markdown or MDX properties, it might be because Starlight is handling the MDX configuration differently. Let's try a more direct approach using Astro's configuration:
// starlight-mdx-globals.js
import react from '@astrojs/react';
export default {
name: 'starlight-mdx-globals',
hooks: {
'config:setup': ({ addIntegration, astroConfig }) => {
// Check if React integration is already added
const isReactLoaded = astroConfig.integrations.find(
({ name }) => name === '@astrojs/react'
);
// Add React integration if not already present
if (!isReactLoaded) {
addIntegration(react());
}
// Directly modify Astro's MDX options
// This is a more direct approach that works with Starlight
astroConfig.markdown = astroConfig.markdown || {};
astroConfig.markdown.mdx = astroConfig.markdown.mdx || {};
astroConfig.markdown.mdx.globalImports = [
"import { SiBun, SiCloudflareworkers, SiDeno, SiNodedotjs } from '@icons-pack/react-simple-icons';"
];
},
},
};
## 3. Alternative Approach
If the plugin approach still doesn't work, you can try a more direct method by extending Starlight's configuration in your `astro.config.mjs`:
```javascript
import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';
import react from '@astrojs/react';
export default defineConfig({
integrations: [
react(), // Add React integration explicitly
starlight({
title: 'My Docs',
// Other Starlight config...
}),
],
// Configure MDX directly at the Astro level
markdown: {
mdx: {
globalImports: [
"import { SiBun, SiCloudflareworkers, SiDeno, SiNodedotjs } from '@icons-pack/react-simple-icons';"
]
}
}
});
For more information on advanced Markdown and MDX configuration in Starlight, you can refer to the Authoring Content guide.