fix: 🐛 Bug: Starlight-GhostCMS
on line 113 of ghostAPI.ts
#89
|
@ -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`
|
|
@ -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!
|
||||
```
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<StarlightGhostConfig>(),
|
||||
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());
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue