some more starlight cleanup... also powered by AIK

This commit is contained in:
Adam Matthiesen 2024-03-05 22:25:43 -08:00
parent f078ebc063
commit edad9da91f
5 changed files with 85 additions and 43 deletions

View File

@ -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,

View File

@ -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"
},

View File

@ -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<StarlightGhostConfig>(),
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");
}
}
}
})

View File

@ -1,11 +1,8 @@
/// <reference types="vitest" />
/// <reference types="vite/client" />
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}"],

View File

@ -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)