fix: 🐛 Bug: `Starlight-GhostCMS` on line 113 of `ghostAPI.ts` (#89)

Co-authored-by: create-issue-branch[bot] <53036503+create-issue-branch[bot]@users.noreply.github.com>
Co-authored-by: Adam Matthiesen <amatthiesen@outlook.com>
This commit is contained in:
create-issue-branch[bot] 2024-03-09 04:11:54 -08:00 committed by GitHub
parent fa93f54b68
commit 205738c288
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 55 additions and 45 deletions

View File

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

View File

@ -49,7 +49,9 @@ export default defineConfig({
integrations: [ integrations: [
starlight({ starlight({
plugins: [ plugins: [
starlightGhostCMS() starlightGhostCMS({
ghostURL: "https://ghostdemo.matthiesen.xyz"
})
], ],
title: 'My Docs', title: 'My Docs',
}), }),
@ -63,7 +65,7 @@ You must also create 2 environment variables in a `.env` file with the following
```env ```env
CONTENT_API_KEY=a33da3965a3a9fb2c6b3f63b48 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! GITHUB_PERSONAL_TOKEN=ghp_ //OPTIONAL - This is for Astro-Gists if you choose to use it!
``` ```

View File

@ -1,18 +1,14 @@
import type { StarlightPlugin, StarlightUserConfig } from "@astrojs/starlight/types"; import type { StarlightPlugin, StarlightUserConfig } from "@astrojs/starlight/types";
import type { AstroIntegrationLogger } from "astro"; import type { AstroIntegrationLogger } from "astro";
import { type StarlightGhostConfig, validateConfig } from "./src/schemas/config"; import { type StarlightGhostConfig, validateConfig } from "./src/schemas/config";
import { facebook, getSettings, invariant, twitter } from "./src/utils/api";
import starlightGhostcms from "./src/integrations/starlight-ghostcms"; import starlightGhostcms from "./src/integrations/starlight-ghostcms";
const settings = await getSettings();
export type { StarlightGhostConfig }; export type { StarlightGhostConfig };
export default function starlightGhostCMS( export default function starlightGhostCMS(
userConfig?: StarlightGhostConfig, userConfig?: StarlightGhostConfig,
): StarlightPlugin { ): StarlightPlugin {
const config: StarlightGhostConfig = validateConfig(userConfig); const config: StarlightGhostConfig = validateConfig(userConfig);
invariant(settings, "Settings not available... check your api key/url");
return { return {
name: "@matthiesenxyz/starlight-ghostcms-plugin", name: "@matthiesenxyz/starlight-ghostcms-plugin",
@ -32,8 +28,6 @@ export default function starlightGhostCMS(
social: { social: {
...starlightConfig.social, ...starlightConfig.social,
...overrideRSS(starlightConfig.social, astroConfig.site), ...overrideRSS(starlightConfig.social, astroConfig.site),
...overrideTwitter(starlightConfig.social),
...overrideFacebook(starlightConfig.social),
}, },
components: { components: {
...starlightConfig.components, ...starlightConfig.components,
@ -59,6 +53,7 @@ export default function starlightGhostCMS(
}; };
} }
function overrideRSS( function overrideRSS(
socials: StarlightUserConfig["social"], socials: StarlightUserConfig["social"],
url: string | undefined url: string | undefined
@ -68,26 +63,6 @@ function overrideRSS(
return { rss: `${url}/rss.xml` }; 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( function overrideStarlightComponent(
components: StarlightUserConfig["components"], components: StarlightUserConfig["components"],
logger: AstroIntegrationLogger, logger: AstroIntegrationLogger,

View File

@ -6,12 +6,17 @@ import { corePlugins } from "astro-integration-kit/plugins";
import { z } from "astro/zod"; import { z } from "astro/zod";
import { type StarlightGhostConfig } from "../schemas/config"; import { type StarlightGhostConfig } from "../schemas/config";
import astroGists from "@matthiesenxyz/astro-gists"; 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({ export default defineIntegration({
name: "@matthiesenxyz/starlight-ghostcms", name: "@matthiesenxyz/starlight-ghostcms",
optionsSchema: z.custom<StarlightGhostConfig>(), optionsSchema: z.custom<StarlightGhostConfig>(),
plugins: [...corePlugins], plugins: [...corePlugins],
setup({ options }) { setup({ options, name }) {
const { resolve } = createResolver(import.meta.url); const { resolve } = createResolver(import.meta.url);
return { return {
@ -24,6 +29,23 @@ export default defineIntegration({
}) => { }) => {
watchIntegration(resolve()); 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 // Add the AstroGist integration if enabled
logger.info("Adding @matthiesenxyz/astro-gists integration ..."); logger.info("Adding @matthiesenxyz/astro-gists integration ...");
addIntegration(astroGists()); addIntegration(astroGists());

View File

@ -3,6 +3,10 @@ import { z } from "astro/zod";
const configSchema = z const configSchema = z
.object({ .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. * 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" * Turn on and off "Powered by Ghost"
*/ */
supportGhost: z.boolean().default(true), supportGhost: z.boolean().default(true),
}) verbose: z.boolean().default(false),
.default({ postCount: 5, recentPostCount: 10, route: "blog", title: "Blog", rssDescription: "My Awesome Starlight-GhostCMS Blog", supportGhost: true}); });
export function validateConfig(userConfig: unknown): StarlightGhostConfig { export function validateConfig(userConfig: unknown): StarlightGhostConfig {
const config = configSchema.safeParse(userConfig); const config = configSchema.safeParse(userConfig);

View File

@ -3,7 +3,7 @@ import type { Page, Post } from "./schemas";
// LOAD ENVIRONMENT VARIABLES // LOAD ENVIRONMENT VARIABLES
import { loadEnv } from "vite"; 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( const { CONTENT_API_KEY, CONTENT_API_URL } = loadEnv(
"all", "all",
@ -11,12 +11,9 @@ const { CONTENT_API_KEY, CONTENT_API_URL } = loadEnv(
"CONTENT_", "CONTENT_",
); );
//const ConfURL = StarlightGhostConfig.ghostURL || "";
// SETUP GHOST API // SETUP GHOST API
const ghostApiKey = CONTENT_API_KEY || ""; const ghostApiKey = CONTENT_API_KEY || "";
const ghostUrl = CONTENT_API_URL || ""; const ghostUrl = config.ghostURL || CONTENT_API_URL || "";
const version = "v5.0"; const version = "v5.0";
const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version); const api = new TSGhostContentAPI(ghostUrl, ghostApiKey, version);
@ -101,6 +98,15 @@ export const getAllPages = async () => {
return pages; 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) => { export const getSluggedPage = async (slug:string) => {
const results = await api.pages const results = await api.pages
.read({slug: slug}) .read({slug: slug})
@ -110,21 +116,16 @@ export const getSluggedPage = async (slug:string) => {
}).fetch() }).fetch()
if (!results.success) { 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 { return {
post: results.data, 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 () => { export const getAllTags = async () => {
const results = await api.tags const results = await api.tags
.browse() .browse()

View File

@ -10,6 +10,7 @@ export default defineConfig({
title: "My Docs", title: "My Docs",
plugins: [ plugins: [
starlightGhostCMS({ starlightGhostCMS({
ghostURL: "https://ghostdemo.matthiesen.xyz",
title: "Demo Blog", title: "Demo Blog",
rssDescription: "Starlight Playground", rssDescription: "Starlight Playground",
route: "blog", route: "blog",