diff --git a/.changeset/cyan-shirts-care.md b/.changeset/cyan-shirts-care.md new file mode 100644 index 00000000..c95bd41e --- /dev/null +++ b/.changeset/cyan-shirts-care.md @@ -0,0 +1,5 @@ +--- +"@matthiesenxyz/starlight-ghostcms": patch +--- + +fix bug, and resolved config issue that was now will allow users to pass the ghostURL within their `astro.config.mjs` diff --git a/packages/starlight-ghostcms/README.md b/packages/starlight-ghostcms/README.md index 88da96e6..d9cbcd4e 100644 --- a/packages/starlight-ghostcms/README.md +++ b/packages/starlight-ghostcms/README.md @@ -49,7 +49,9 @@ export default defineConfig({ integrations: [ starlight({ plugins: [ - starlightGhostCMS() + starlightGhostCMS({ + ghostURL: "https://ghostdemo.matthiesen.xyz" + }) ], title: 'My Docs', }), @@ -63,7 +65,7 @@ You must also create 2 environment variables in a `.env` file with the following ```env CONTENT_API_KEY=a33da3965a3a9fb2c6b3f63b48 -CONTENT_API_URL=https://ghostdemo.matthiesen.xyz +CONTENT_API_URL=https://ghostdemo.matthiesen.xyz // ghostURL option in `astro.config.mjs` will take priority. (This is fallback option) GITHUB_PERSONAL_TOKEN=ghp_ //OPTIONAL - This is for Astro-Gists if you choose to use it! ``` diff --git a/packages/starlight-ghostcms/index.ts b/packages/starlight-ghostcms/index.ts index 06542310..e9c2b948 100644 --- a/packages/starlight-ghostcms/index.ts +++ b/packages/starlight-ghostcms/index.ts @@ -1,18 +1,14 @@ import type { StarlightPlugin, StarlightUserConfig } from "@astrojs/starlight/types"; import type { AstroIntegrationLogger } from "astro"; 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(); - export type { StarlightGhostConfig }; export default function starlightGhostCMS( userConfig?: StarlightGhostConfig, ): StarlightPlugin { const config: StarlightGhostConfig = validateConfig(userConfig); - invariant(settings, "Settings not available... check your api key/url"); return { name: "@matthiesenxyz/starlight-ghostcms-plugin", @@ -32,8 +28,6 @@ export default function starlightGhostCMS( social: { ...starlightConfig.social, ...overrideRSS(starlightConfig.social, astroConfig.site), - ...overrideTwitter(starlightConfig.social), - ...overrideFacebook(starlightConfig.social), }, components: { ...starlightConfig.components, @@ -59,6 +53,7 @@ export default function starlightGhostCMS( }; } + function overrideRSS( socials: StarlightUserConfig["social"], url: string | undefined @@ -68,26 +63,6 @@ function overrideRSS( 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/src/integrations/starlight-ghostcms.ts b/packages/starlight-ghostcms/src/integrations/starlight-ghostcms.ts index 3fb20786..c4952d6a 100644 --- a/packages/starlight-ghostcms/src/integrations/starlight-ghostcms.ts +++ b/packages/starlight-ghostcms/src/integrations/starlight-ghostcms.ts @@ -6,12 +6,17 @@ import { corePlugins } from "astro-integration-kit/plugins"; import { z } from "astro/zod"; import { type StarlightGhostConfig } from "../schemas/config"; import astroGists from "@matthiesenxyz/astro-gists"; +import { AstroError } from "astro/errors"; +import { loadEnv } from "vite"; + +// Load environment variables +const ENV = loadEnv("all", process.cwd(), "CONTENT_API"); export default defineIntegration({ name: "@matthiesenxyz/starlight-ghostcms", optionsSchema: z.custom(), plugins: [...corePlugins], - setup({ options }) { + setup({ options, name }) { const { resolve } = createResolver(import.meta.url); return { @@ -24,6 +29,23 @@ export default defineIntegration({ }) => { watchIntegration(resolve()); + // Check for GhostCMS API Key + if (ENV.CONTENT_API_KEY === undefined) { + throw new AstroError( + `${name} CONTENT_API_KEY is not set in environment variables`, + ); + } + + // Check for GhostCMS URL + if (options.ghostURL === undefined) { + logger.warn("ghostURL is not set in user configuration falling back to environment variable"); + if (ENV.CONTENT_API_URL === undefined) { + throw new AstroError( + `${name} CONTENT_API_URL is not set in environment variables`, + ); + } + } + // Add the AstroGist integration if enabled logger.info("Adding @matthiesenxyz/astro-gists integration ..."); addIntegration(astroGists()); diff --git a/packages/starlight-ghostcms/src/schemas/config.ts b/packages/starlight-ghostcms/src/schemas/config.ts index 51accf6a..6db57d2c 100644 --- a/packages/starlight-ghostcms/src/schemas/config.ts +++ b/packages/starlight-ghostcms/src/schemas/config.ts @@ -3,6 +3,10 @@ import { z } from "astro/zod"; const configSchema = z .object({ + /** + * The URL of the GhostCMS instance. + */ + ghostURL: z.string().url().optional(), /** * The number of blog posts to display per page in the blog post list. */ @@ -31,8 +35,8 @@ const configSchema = z * Turn on and off "Powered by Ghost" */ supportGhost: z.boolean().default(true), - }) - .default({ postCount: 5, recentPostCount: 10, route: "blog", title: "Blog", rssDescription: "My Awesome Starlight-GhostCMS Blog", supportGhost: true}); + verbose: z.boolean().default(false), + }); export function validateConfig(userConfig: unknown): StarlightGhostConfig { const config = configSchema.safeParse(userConfig); diff --git a/packages/starlight-ghostcms/src/utils/api/ghostAPI.ts b/packages/starlight-ghostcms/src/utils/api/ghostAPI.ts index db629d25..945a0938 100644 --- a/packages/starlight-ghostcms/src/utils/api/ghostAPI.ts +++ b/packages/starlight-ghostcms/src/utils/api/ghostAPI.ts @@ -3,7 +3,7 @@ import type { Page, Post } from "./schemas"; // LOAD ENVIRONMENT VARIABLES import { loadEnv } from "vite"; -//import StarlightGhostConfig from "virtual:starlight-ghostcms/config"; +import config from "virtual:starlight-ghostcms/config"; const { CONTENT_API_KEY, CONTENT_API_URL } = loadEnv( "all", @@ -11,12 +11,9 @@ const { CONTENT_API_KEY, CONTENT_API_URL } = loadEnv( "CONTENT_", ); - -//const ConfURL = StarlightGhostConfig.ghostURL || ""; - // SETUP GHOST API const ghostApiKey = CONTENT_API_KEY || ""; -const ghostUrl = CONTENT_API_URL || ""; +const ghostUrl = config.ghostURL || CONTENT_API_URL || ""; const version = "v5.0"; const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version); @@ -101,6 +98,15 @@ export const getAllPages = async () => { return pages; }; + +export const getSettings = async () => { + const res = await api.settings.fetch(); + if (res.success) { + return res.data; + } + return null; +}; + export const getSluggedPage = async (slug:string) => { const results = await api.pages .read({slug: slug}) @@ -110,21 +116,16 @@ export const getSluggedPage = async (slug:string) => { }).fetch() if (!results.success) { - throw new Error(results.errors.map((e) => e.message).join(", ")); + if (config.verbose === true){ + console.log(`[Starlight-GhostCMS]: ${results.errors.map((e) => e.message).join(", ")}Resource: (${slug})`); + } + return null; } return { post: results.data, }; }; -export const getSettings = async () => { - const res = await api.settings.fetch(); - if (res.success) { - return res.data; - } - return null; -}; - export const getAllTags = async () => { const results = await api.tags .browse() diff --git a/playgrounds/starlight-playground/astro.config.mjs b/playgrounds/starlight-playground/astro.config.mjs index 9eb55a88..ada1a0d6 100644 --- a/playgrounds/starlight-playground/astro.config.mjs +++ b/playgrounds/starlight-playground/astro.config.mjs @@ -10,6 +10,7 @@ export default defineConfig({ title: "My Docs", plugins: [ starlightGhostCMS({ + ghostURL: "https://ghostdemo.matthiesen.xyz", title: "Demo Blog", rssDescription: "Starlight Playground", route: "blog",