astro-ghostcms/packages/starlight-ghostcms/index.ts

122 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-03-03 16:49:15 +00:00
import type {
StarlightPlugin,
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";
2024-03-03 16:49:15 +00:00
const settings = await getSettings();
2024-02-19 02:52:08 +00:00
2024-03-03 16:49:15 +00:00
export type { StarlightGhostConfig };
2024-02-19 02:52:08 +00:00
2024-03-03 16:49:15 +00:00
export default function starlightGhostCMS(
userConfig?: StarlightGhostConfig,
): StarlightPlugin {
const config: StarlightGhostConfig = validateConfig(userConfig);
invariant(settings, "Settings not available... check your api key/url");
2024-03-03 16:49:15 +00:00
return {
name: "@matthiesenxyz/starlight-ghostcms-plugin",
hooks: {
setup({
astroConfig,
addIntegration,
config: starlightConfig,
logger,
updateConfig: updateStarlightConfig,
}) {
2024-03-06 04:24:59 +00:00
// Update the Starlight config with the GhostCMS config
2024-03-03 16:49:15 +00:00
updateStarlightConfig({
social: {
...starlightConfig.social,
rss: `${astroConfig.site}/rss.xml`,
twitter: twitter(settings.twitter ? settings.twitter : ""),
facebook: facebook(settings.facebook ? settings.facebook : ""),
},
components: {
...starlightConfig.components,
...overrideStarlightComponent(
starlightConfig.components,
logger,
"MarkdownContent",
),
...overrideStarlightComponent(
starlightConfig.components,
logger,
"Sidebar",
),
...overrideStarlightComponent(
starlightConfig.components,
logger,
"SiteTitle",
),
},
});
2024-02-19 02:52:08 +00:00
2024-03-06 04:24:59 +00:00
// Add the Starlight-GhostCMS integration
2024-03-03 16:49:15 +00:00
addIntegration({
name: "@matthiesenxyz/starlight-ghostcms",
hooks: {
"astro:config:setup": ({ injectRoute, updateConfig }) => {
2024-02-19 02:52:08 +00:00
2024-03-03 16:49:15 +00:00
updateConfig({
vite: {
plugins: [vitePluginStarlightGhostConfig(config)],
},
});
2024-03-06 04:24:59 +00:00
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");
2024-03-06 04:24:59 +00:00
2024-03-03 16:49:15 +00:00
},
},
});
2024-03-06 04:24:59 +00:00
2024-03-03 16:49:15 +00:00
},
},
};
2024-02-19 02:52:08 +00:00
}
function overrideStarlightComponent(
2024-03-03 16:49:15 +00:00
components: StarlightUserConfig["components"],
logger: AstroIntegrationLogger,
component: keyof NonNullable<StarlightUserConfig["components"]>,
) {
if (components?.[component]) {
logger.warn(
`It looks like you already have a \`${component}\` component override in your Starlight configuration.`,
);
logger.warn(
`To use \`starlight-ghostcms\`, remove the override for the \`${component}\` component.\n`,
);
logger.warn("This Warning can be ignored if you know what your doing ;)");
return {};
}
return {
[component]: `@matthiesenxyz/starlight-ghostcms/overrides/${component}.astro`,
};
}