Woot Configs!

This commit is contained in:
Adam Matthiesen 2024-01-20 10:38:32 -08:00
parent a090280039
commit 75416edef2
10 changed files with 116 additions and 75 deletions

View File

@ -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';
@ -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,
@ -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) {
@ -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");
}
@ -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
}
},

View File

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

View File

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

View File

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

View File

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

File diff suppressed because one or more lines are too long

View File

@ -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
src/utils/schemas.ts Normal file
View File

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

View File

@ -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
src/utils/virtual.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
declare module 'virtual:@matthiesenxyz/astro-ghostcms/user-config' {
const Config: import('./UserConfigSchema').UserConfig;
export default Config;
}