-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a090280
commit 75416ed
Showing
10 changed files
with
116 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |