Skip to content

Commit

Permalink
Woot Configs!
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Jan 20, 2024
1 parent a090280 commit 75416ed
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 75 deletions.
29 changes: 15 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { UserConfigSchema, type UserConfig } from "./src/utils/UserConfigSchema"
import { ghostSitemap, ghostRobots } from "./src/integrations";
import { loadEnv } from 'vite';
import { fromZodError } from "zod-validation-error";
import sitemap from '@astrojs/sitemap';
import robotsTxt from "astro-robots-txt";
import { viteGhostCMS } from "./src/utils/virtual-imports";

// LOAD ENVIRONMENT VARIABLES
const mode = 'all';
Expand All @@ -16,13 +15,10 @@ const env = loadEnv(mode, process.cwd(), prefixes);
const pkg = '@matthiesenxyz/astro-ghostcms';

export default function GhostCMS(options: UserConfig): AstroIntegration {
let UserConfig:UserConfig
return {
name: pkg,
hooks: {
'astro:config:setup': async ({
command,
isRestart,
injectRoute,
config,
updateConfig,
Expand All @@ -49,6 +45,7 @@ export default function GhostCMS(options: UserConfig): AstroIntegration {
throw validationError;
}
const entry = o.data.theme;
const uconf = o.data;

// THEME SELECTOR
if (entry === pkg) {
Expand Down Expand Up @@ -109,7 +106,7 @@ export default function GhostCMS(options: UserConfig): AstroIntegration {
logger.info("Checking for @astrojs/sitemap");
if (!int.find(({ name }) => name === '@astrojs/sitemap')) {
logger.info("Injecting Integration: @astrojs/sitemap");
int.push(sitemap());
int.push(ghostSitemap(uconf));
} else {
logger.info("Already Imported by User: @astrojs/sitemap");
}
Expand All @@ -118,18 +115,22 @@ export default function GhostCMS(options: UserConfig): AstroIntegration {
logger.info("Checking for astro-robots-txt");
if (!int.find(({ name }) => name === 'astro-robots-txt')) {
logger.info("Injecting Integration: astro-robots-txt");
int.push(robotsTxt());
int.push(ghostRobots(uconf));
} else {
logger.info("Already Imported by User: astro-robots-txt");
}

try {
updateConfig({
integrations: [sitemap(),robotsTxt()]
})
} catch (e) {
try { updateConfig({
integrations: [
ghostSitemap(uconf),
ghostRobots(uconf)
],
vite: {
plugins: [
viteGhostCMS(uconf,config)
]
} }) } catch (e) {
logger.error(e as string)
throw e
throw e
}

},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@matthiesenxyz/astro-ghostcms",
"description": "Astro GhostCMS integration to allow easier importing of GhostCMS Content",
"version": "2.1.1",
"version": "2.1.2",
"author": "MatthiesenXYZ (https://matthiesen.xyz)",
"type": "module",
"license": "MIT",
Expand Down
20 changes: 17 additions & 3 deletions src/integrations/robots-txt.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import robotsTxt, { type RobotsTxtOptions } from "astro-robots-txt";
import { UserConfig } from "../utils/UserConfigSchema";

export function getRobotsTxtConfig(): RobotsTxtOptions {
export function getRobotsTxtConfig(opts: UserConfig): RobotsTxtOptions {
const { robotstxt } = opts;
const robotsConfig: RobotsTxtOptions = {};
if (robotstxt?.host) {
robotsConfig.host = robotstxt.host;
}
if (robotstxt?.policy) {
robotsConfig.policy = robotstxt.policy;
}
if (robotstxt?.sitemap) {
robotsConfig.sitemap = robotstxt.sitemap;
}
if (robotstxt?.sitemapBaseFileName) {
robotsConfig.sitemapBaseFileName = robotstxt.sitemapBaseFileName;
}
return robotsConfig;
}

/**
* A wrapped version of the `astro-robots-txt` integration for GhostCMS.
*/
export default function ghostRobots() {
return robotsTxt(getRobotsTxtConfig());
export default function ghostRobots(opts: UserConfig) {
return robotsTxt(getRobotsTxtConfig(opts));
}
14 changes: 11 additions & 3 deletions src/integrations/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import sitemap, { type SitemapOptions } from '@astrojs/sitemap';
import { UserConfig } from '../utils/UserConfigSchema';

export function getSitemapConfig(): SitemapOptions {
export function getSitemapConfig(opts: UserConfig): SitemapOptions {
const { sitemap } = opts
const sitemapConfig: SitemapOptions = {};
if (sitemap?.entryLimit){
sitemapConfig.entryLimit = sitemap.entryLimit;
}
if (sitemap?.customPages){
sitemapConfig.customPages = sitemap.customPages;
}
return sitemapConfig;
}

/**
* A wrapped version of the `@astrojs/sitemap` integration for GhostCMS.
*/
export default function ghostSitemap() {
return sitemap(getSitemapConfig());
export default function ghostSitemap(opts: UserConfig) {
return sitemap(getSitemapConfig(opts));
}
1 change: 0 additions & 1 deletion src/routes/rss.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export async function GET(context) {
title: title,
description: description,
site: context.site,
//stylesheet: '/rss/styles.xsl',
items: posts.map((post) => ({
title: post.title,
pubDate: post.published_at,
Expand Down
52 changes: 0 additions & 52 deletions src/styles/rss.xsl

This file was deleted.

4 changes: 3 additions & 1 deletion src/utils/UserConfigSchema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { z } from 'astro/zod';
import * as S from './schemas';

export const UserConfigSchema = z.object({
theme: z.string().default('@matthiesenxyz/astro-ghostcms'),
rssStyle: z.string().optional()
sitemap: S.SitemapSchema.optional(),
robotstxt: S.RobotsTxtSchema.optional(),
});

export type UserConfig = z.infer<typeof UserConfigSchema>
21 changes: 21 additions & 0 deletions src/utils/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from 'astro/zod';

export const SitemapSchema = z.object({
customPages: z.string().array().optional(),
entryLimit: z.number().optional()
})

const RobotsPolicySchema = z.object({
userAgent: z.string(),
allow: z.string().optional(),
disallow: z.string().optional(),
cleanParam: z.string().optional(),
crawlDelay: z.number()
})

export const RobotsTxtSchema = z.object({
host: z.string().optional(),
sitemap: z.string().optional(),
sitemapBaseFileName: z.string().optional(),
policy: RobotsPolicySchema.array().optional(),
})
44 changes: 44 additions & 0 deletions src/utils/virtual-imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { AstroConfig, ViteUserConfig } from 'astro'
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

import { UserConfig } from './UserConfigSchema'

function resolveVirtualModuleId<T extends string>(id: T): `\0${T}` {
return `\0${id}`
}

export function viteGhostCMS(
opts: UserConfig,
{
root,
}: Pick<AstroConfig, 'root' | 'srcDir' | 'trailingSlash'> & {
build: Pick<AstroConfig['build'], 'format'>
}
): NonNullable<ViteUserConfig['plugins']>[number] {
const resolveId = (id: string) =>
JSON.stringify(id.startsWith('.') ? resolve(fileURLToPath(root), id) : id);

const modules = {
'virtual:@matthiesenxyz/astro-ghostcms/user-config': `export default ${ JSON.stringify(opts) }`
} satisfies Record<string, string>

/** Mapping names prefixed with `\0` to their original form. */
const resolutionMap = Object.fromEntries(
(Object.keys(modules) as (keyof typeof modules)[]).map((key) => [
resolveVirtualModuleId(key),
key,
])
)

return {
name: 'vite-plugin-matthiesenxyz-astro-ghostcms-user-config',
resolveId(id): string | void {
if (id in modules) return resolveVirtualModuleId(id)
},
load(id): string | void {
const resolution = resolutionMap[id]
if (resolution) return modules[resolution]
},
}
}
4 changes: 4 additions & 0 deletions src/utils/virtual.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'virtual:@matthiesenxyz/astro-ghostcms/user-config' {
const Config: import('./UserConfigSchema').UserConfig;
export default Config;
}

0 comments on commit 75416ed

Please sign in to comment.