diff --git a/packages/starlight-ghostcms/index.ts b/packages/starlight-ghostcms/index.ts index 8e1238dd..5a1c7143 100644 --- a/packages/starlight-ghostcms/index.ts +++ b/packages/starlight-ghostcms/index.ts @@ -3,12 +3,12 @@ import type { StarlightUserConfig, } from "@astrojs/starlight/types"; import type { AstroIntegrationLogger } from "astro"; -import { vitePluginStarlightGhostConfig } from "./src/integrations/vite"; import { type StarlightGhostConfig, validateConfig, } from "./src/schemas/config"; import { facebook, getSettings, invariant, twitter } from "./src/utils/api"; +import starlightGhostcms from "./src/integrations/starlight-ghostcms"; const settings = await getSettings(); @@ -30,13 +30,16 @@ export default function starlightGhostCMS( logger, updateConfig: updateStarlightConfig, }) { + // Add the Starlight-GhostCMS integration + addIntegration(starlightGhostcms(config)); + // Update the Starlight config with the GhostCMS config updateStarlightConfig({ social: { ...starlightConfig.social, - rss: `${astroConfig.site}/rss.xml`, - twitter: twitter(settings.twitter ? settings.twitter : ""), - facebook: facebook(settings.facebook ? settings.facebook : ""), + ...overrideRSS(starlightConfig.social, astroConfig.site), + ...overrideTwitter(starlightConfig.social), + ...overrideFacebook(starlightConfig.social), }, components: { ...starlightConfig.components, @@ -57,47 +60,37 @@ export default function starlightGhostCMS( ), }, }); - - // Add the Starlight-GhostCMS integration - addIntegration({ - name: "@matthiesenxyz/starlight-ghostcms", - hooks: { - "astro:config:setup": ({ injectRoute, updateConfig }) => { - - updateConfig({ - vite: { - plugins: [vitePluginStarlightGhostConfig(config)], - }, - }); - - const makeRoute = (endpoint: string, entrypoint: string) => { - injectRoute({ - pattern: `/${endpoint}`, - entrypoint: `@matthiesenxyz/starlight-ghostcms/routes/${entrypoint}`, - prerender: true, - }); - }; - - makeRoute(`${config.route}`, - "index.astro"); - makeRoute(`${config.route}/[slug]`, - "[slug].astro"); - makeRoute(`${config.route}/about`, - "about.astro"); - makeRoute(`${config.route}/authors`, - "authors.astro"); - makeRoute("rss.xml", - "rss.xml.ts"); - - }, - }, - }); - }, }, }; } +function overrideRSS( + socials: StarlightUserConfig["social"], + url: string | undefined + ) { if (socials?.rss) { return {}; } + if (url === undefined) { return undefined; } + return { rss: `${url}/rss.xml` }; +} + +function overrideTwitter( + socials: StarlightUserConfig["social"], + ) { if (socials?.twitter) { return {}; } + if (settings?.twitter) { + return { twitter: twitter(settings.twitter), } + } + return undefined; +} + +function overrideFacebook( + socials: StarlightUserConfig["social"], + ) { if (socials?.facebook) { return {}; } + if (settings?.facebook) { + return { facebook: facebook(settings.facebook), } + } + return undefined; +} + function overrideStarlightComponent( components: StarlightUserConfig["components"], logger: AstroIntegrationLogger, diff --git a/packages/starlight-ghostcms/package.json b/packages/starlight-ghostcms/package.json index b72f5d63..328808e3 100644 --- a/packages/starlight-ghostcms/package.json +++ b/packages/starlight-ghostcms/package.json @@ -68,6 +68,7 @@ "@astrojs/rss": "^4.0.5", "@ts-ghost/core-api": "^6.0.0", "@ts-ghost/content-api": "^4.0.12", + "astro-integration-kit": "^0.5.1", "vite": "^5.1.5", "vite-tsconfig-paths": "^4.3.1" }, diff --git a/packages/starlight-ghostcms/src/integrations/starlight-ghostcms.ts b/packages/starlight-ghostcms/src/integrations/starlight-ghostcms.ts new file mode 100644 index 00000000..fb585ea6 --- /dev/null +++ b/packages/starlight-ghostcms/src/integrations/starlight-ghostcms.ts @@ -0,0 +1,48 @@ +import { + createResolver, + defineIntegration, +} from "astro-integration-kit"; +import { corePlugins } from "astro-integration-kit/plugins"; +import { z } from "astro/zod"; +import { vitePluginStarlightGhostConfig } from "./vite"; +import { type StarlightGhostConfig } from "../schemas/config"; + +export default defineIntegration({ + name: "@matthiesenxyz/starlight-ghostcms", + optionsSchema: z.custom(), + plugins: [...corePlugins], + setup({ options }) { + const { resolve } = createResolver(import.meta.url); + + return { + "astro:config:setup": ({ watchIntegration, updateConfig, injectRoute }) => { + watchIntegration(resolve()); + + updateConfig({ + vite: { + plugins: [vitePluginStarlightGhostConfig(options)], + }, + }); + + const makeRoute = (endpoint: string, entrypoint: string) => { + injectRoute({ + pattern: `/${endpoint}`, + entrypoint: resolve(`../routes/${entrypoint}`), + prerender: true, + }); + }; + + makeRoute(`${options.route}`, + "index.astro"); + makeRoute(`${options.route}/[slug]`, + "[slug].astro"); + makeRoute(`${options.route}/about`, + "about.astro"); + makeRoute(`${options.route}/authors`, + "authors.astro"); + makeRoute("rss.xml", + "rss.xml.ts"); + } + } + } +}) \ No newline at end of file diff --git a/packages/starlight-ghostcms/vitest.config.ts b/packages/starlight-ghostcms/vitest.config.ts index deec7256..6a723a68 100644 --- a/packages/starlight-ghostcms/vitest.config.ts +++ b/packages/starlight-ghostcms/vitest.config.ts @@ -1,11 +1,8 @@ /// /// - -import tsconfigPaths from "vite-tsconfig-paths"; import { defineProject } from "vitest/config"; export default defineProject({ - plugins: [tsconfigPaths()], test: { globals: true, include: ["./**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca719988..0495882d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -253,6 +253,9 @@ importers: '@ts-ghost/core-api': specifier: ^6.0.0 version: 6.0.0 + astro-integration-kit: + specifier: ^0.5.1 + version: 0.5.1(astro@4.4.9) vite: specifier: ^5.1.5 version: 5.1.5(@types/node@20.11.24)