import { isUnifiedProcessor, unified } from "@astrojs/markdown-remark"; import type { StarlightConfig, StarlightPlugin, StarlightUserConfig } from "@astrojs/starlight/types"; import type { AstroConfig } from "astro"; import remarkCustomHeaderId from "remark-custom-header-id"; import { createShikiConfig } from "./shiki-config"; import type { MultiSidebarOptions, ThemeCelestiaOptions } from "./user-options"; import { vitePluginUserConfig } from "./virtual-user-config"; const components = { Header: "starlight-theme-celestia/components/Header.astro", Search: "starlight-theme-celestia/components/Search.astro", ThemeProvider: "starlight-theme-celestia/components/ThemeProvider.astro", ThemeSelect: "starlight-theme-celestia/components/ThemeSelect.astro", SocialIcons: "starlight-theme-celestia/components/SocialIcons.astro", SiteTitle: "starlight-theme-celestia/components/SiteTitleVersioned.astro", PageFrame: "starlight-theme-celestia/components/PageFrame.astro", Pagination: "starlight-theme-celestia/components/Pagination.astro", MobileMenuToggle: "starlight-theme-celestia/components/MobileMenuToggle.astro", TwoColumnContent: "starlight-theme-celestia/components/TwoColumnContent.astro", MarkdownContent: "starlight-theme-celestia/components/MarkdownContent.astro", Hero: "starlight-theme-celestia/components/Hero.astro", MobileTableOfContents: "starlight-theme-celestia/components/MobileTableOfContents.astro", MobileMenuFooter: "starlight-theme-celestia/components/MobileMenuFooter.astro", LanguageSelect: "starlight-theme-celestia/components/LanguageSelect.astro", Head: "starlight-theme-celestia/components/Head.astro", } as const satisfies Partial; const sidebarComponent = "starlight-theme-celestia/components/Sidebar.astro"; export type { MultiSidebarOptions, ThemeCelestiaOptions }; export default function starlightCelestiaTheme(options: ThemeCelestiaOptions = {}): StarlightPlugin { return { name: "starlight-theme-celestia", hooks: { "config:setup": async ({ config, updateConfig, addIntegration, astroConfig }) => { let useTailwind: boolean; if (options.stylingSystem === "css") { useTailwind = false; } else if (options.stylingSystem === "tailwind") { useTailwind = true; } else { const hasTailwindcss = await checkHasPlugin(astroConfig.vite?.plugins, "tailwind"); useTailwind = hasTailwindcss; } const newConfig = { customCss: [ "starlight-theme-celestia/layer.css", useTailwind ? "" : "starlight-theme-celestia/tailwind.gen.css", "starlight-theme-celestia/theme.css", ...(config.customCss || []), "starlight-theme-celestia/styles.css", ].filter(Boolean), components: { ...components, // Only override the sidebar when multi-sidebar is enabled, so Starlight's default keeps // rendering for every other site. ...(options.multiSidebar ? { Sidebar: sidebarComponent } : {}), ...config.components, }, expressiveCode: config.expressiveCode ?? false, } satisfies Partial; updateConfig(newConfig); addIntegration({ name: "starlight-theme-celestia-integration", hooks: { "astro:config:setup": ({ config, updateConfig }) => { // Reuse unified so user options and Astro's legacy-plugin migration tracking survive. // Astro migrates any top-level remark/rehype settings when updateConfig validates this processor. const currentProcessor = config.markdown.processor; const processor = currentProcessor && isUnifiedProcessor(currentProcessor) ? currentProcessor : unified(); processor.options.remarkPlugins.push(remarkCustomHeaderId); updateConfig({ markdown: { shikiConfig: createShikiConfig({ twoslash: true }), processor, }, vite: { plugins: [ vitePluginUserConfig({ nav: options.nav, multiSidebar: options.multiSidebar, rootHref: astroConfig.root.toString(), }), ], }, }); }, }, }); }, }, }; } type ViteUserConfig = AstroConfig["vite"]; type VitePlugin = NonNullable[number]; async function checkHasPlugin(plugin: VitePlugin | Promise, search: string): Promise { const awaited = await plugin; if (!awaited) { return false; } if (Array.isArray(awaited)) { for (const p of awaited) { if (await checkHasPlugin(p, search)) { return true; } } return false; } const name = awaited.name || ""; return name.includes(search); }