new starlight feature.... prep for expansion of astro-ghostcms

This commit is contained in:
Adam Matthiesen 2024-03-05 21:24:01 -08:00
parent 70b6fd2d16
commit f078ebc063
9 changed files with 47 additions and 27 deletions

View File

@ -3,3 +3,6 @@
--- ---
Bumb GhostCMS API, No user facing breaking changes. Bumb GhostCMS API, No user facing breaking changes.
NEW:
- You can now set a `route: "blog"` in your `astro.config.mjs` to change the default `/<route>` to your blog/posts

View File

@ -78,11 +78,16 @@ export default function starlightGhostCMS(
}); });
}; };
makeRoute("blog", "index.astro"); makeRoute(`${config.route}`,
makeRoute("blog/[slug]", "[slug].astro"); "index.astro");
makeRoute("blog/about", "about.astro"); makeRoute(`${config.route}/[slug]`,
makeRoute("blog/authors", "authors.astro"); "[slug].astro");
makeRoute("rss.xml", "rss.xml.ts"); makeRoute(`${config.route}/about`,
"about.astro");
makeRoute(`${config.route}/authors`,
"authors.astro");
makeRoute("rss.xml",
"rss.xml.ts");
}, },
}, },

View File

@ -1,22 +1,27 @@
--- ---
import StarlightMarkdownContent from '@astrojs/starlight/components/MarkdownContent.astro' import StarlightMarkdownContent from '@astrojs/starlight/components/MarkdownContent.astro'
import type { Props } from '@astrojs/starlight/props' import type { Props } from '@astrojs/starlight/props'
import config from 'virtual:starlight-ghostcms/config'
import { isAnyBlogPostPage } from '../utils/page' import { isAnyBlogPostPage } from '../utils/page'
import Metadata from '../components/Metadata.astro' import Metadata from '../components/Metadata.astro'
import type { Post } from '../schemas/posts' import type { Post } from '../schemas/posts'
export function checkpath(path: string){
return path.split('/').includes(config.route) ? true : false
}
const isBlog = checkpath(Astro.url.pathname)
const isBlogPost = isAnyBlogPostPage(Astro.props.slug) const isBlogPost = isAnyBlogPostPage(Astro.props.slug)
let blogEntry: Post | undefined = undefined let blogEntry: Post | undefined = undefined
--- ---
<StarlightMarkdownContent {...Astro.props}> <StarlightMarkdownContent {...Astro.props}>
{isBlogPost && blogEntry ? <Metadata entry={blogEntry} /> : null} {isBlog && blogEntry ? <Metadata entry={blogEntry} /> : null}
<slot /> <slot />
{ {
isBlogPost && blogEntry ? ( isBlog && blogEntry ? (
<div class="post-footer"> <div class="post-footer">
</div> </div>
) : null ) : null

View File

@ -17,19 +17,17 @@ export async function getBlogPageEntries(){
} }
export function checkpath(path: string){ export function checkpath(path: string){
if ( path.slice(0, 5) === "/blog" ){ return path.split('/').includes(config.route) ? true : false
return true
} else { return false }
} }
export function isAbout(path: string){ export function isAbout(path: string){
if ( path === "/blog/about" ){ if ( path === `/${config.route}/about` ){
return true return true
} else { return false } } else { return false }
} }
export function isAuthors(path: string){ export function isAuthors(path: string){
if ( path === "/blog/authors" ){ if ( path === `/${config.route}/authors` ){
return true return true
} else { return false } } else { return false }
} }
@ -38,7 +36,7 @@ const recentEntries = isBlog ? await getRecentBlogEntries() : []
const aboutPage = await getSluggedPage("about"); const aboutPage = await getSluggedPage("about");
const AboutEntry:SidebarEntry = { const AboutEntry:SidebarEntry = {
attrs: {}, badge: undefined, attrs: {}, badge: undefined,
href: '/blog/about', href: `/${config.route}/about`,
isCurrent: isAbout(Astro.url.pathname), isCurrent: isAbout(Astro.url.pathname),
type: 'link', type: 'link',
label: aboutPage?.post?.title label: aboutPage?.post?.title
@ -55,7 +53,7 @@ const blogSidebar: Props['sidebar'] = isBlog
{ {
attrs: {}, attrs: {},
badge: undefined, badge: undefined,
href: '/blog/authors', href: `/${config.route}/authors`,
isCurrent: isAuthors(Astro.url.pathname), isCurrent: isAuthors(Astro.url.pathname),
label: 'Our Authors', label: 'Our Authors',
type: 'link', type: 'link',
@ -63,7 +61,7 @@ const blogSidebar: Props['sidebar'] = isBlog
{ {
attrs: {}, attrs: {},
badge: undefined, badge: undefined,
href: '/blog', href: `/${config.route}`,
isCurrent: isBlogRoot(Astro.props.slug), isCurrent: isBlogRoot(Astro.props.slug),
label: 'All posts', label: 'All posts',
type: 'link', type: 'link',
@ -74,8 +72,8 @@ const blogSidebar: Props['sidebar'] = isBlog
entries: recentEntries.map((blogEntry) => ({ entries: recentEntries.map((blogEntry) => ({
attrs: {}, attrs: {},
badge: blogEntry.featured?({text: "⭐", variant: "note"}):undefined, badge: blogEntry.featured?({text: "⭐", variant: "note"}):undefined,
href: `/blog/${blogEntry.slug}`, href: `/${config.route}/${blogEntry.slug}`,
isCurrent: isBlogPostPage(Astro.props.slug, `blog/${blogEntry.slug}`), isCurrent: isBlogPostPage(Astro.props.slug, `${config.route}/${blogEntry.slug}`),
label: blogEntry.title, label: blogEntry.title,
type: 'link', type: 'link',
})), })),
@ -89,7 +87,7 @@ const blogSidebar: Props['sidebar'] = isBlog
{ {
!isBlog && ( !isBlog && (
<div class="md:sl-hidden"> <div class="md:sl-hidden">
<a href="/blog">Blog</a> <a href={`/${config.route}`}>Blog</a>
</div> </div>
) )
} }

View File

@ -1,11 +1,12 @@
--- ---
import type { Props } from "@astrojs/starlight/props"; import type { Props } from "@astrojs/starlight/props";
import AstrolightSiteTitle from "@astrojs/starlight/components/SiteTitle.astro"; import AstrolightSiteTitle from "@astrojs/starlight/components/SiteTitle.astro";
import config from 'virtual:starlight-ghostcms/config'
--- ---
<AstrolightSiteTitle {...Astro.props} /> <AstrolightSiteTitle {...Astro.props} />
<div> <div>
<a href="/blog">Blog</a> <a href={`/${config.route}`}>{config.linkName}</a>
</div> </div>
<style> <style>

View File

@ -22,7 +22,7 @@ export async function GET({ site }: APIContext) {
post.published_at ? post.published_at : post.created_at, post.published_at ? post.published_at : post.created_at,
), ),
description: post.excerpt, description: post.excerpt,
link: `/blog/${post.slug}/`, link: `/${config.route}/${post.slug}/`,
author: post.primary_author?.name, author: post.primary_author?.name,
})), })),
}); });

View File

@ -11,6 +11,14 @@ const configSchema = z
* The number of recent blog posts to display in the sidebar. * The number of recent blog posts to display in the sidebar.
*/ */
recentPostCount: z.number().min(1).default(10), recentPostCount: z.number().min(1).default(10),
/**
* Allows you to change the default route for the blog.
*/
route: z.string().default("blog"),
/**
* The name of the blog link in the navigation.
*/
linkName: z.string().default("Blog"),
/** /**
* The title of the blog. * The title of the blog.
*/ */
@ -24,7 +32,7 @@ const configSchema = z
*/ */
supportGhost: z.boolean().default(true), supportGhost: z.boolean().default(true),
}) })
.default({}); .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

@ -1,13 +1,13 @@
export function isAnyBlogPage(slug: string) { import config from 'virtual:starlight-ghostcms/config'
return slug.match(/^blog(\/?$|\/.+\/?$)/) !== null;
}
export function isBlogRoot(slug: string) { export function isBlogRoot(slug: string) {
return slug === "blog"; return slug === "blog";
} }
export function isAnyBlogPostPage(slug: string) { export function isAnyBlogPostPage(slug: string) {
return slug.match(/^blog\/(?!(\d+\/?|tags\/.+)$).+$/) !== null; const group = slug.split("/").pop();
const currentslug = group?.[0];
return currentslug;
} }
export function isBlogPostPage(slug: string, postSlug: string) { export function isBlogPostPage(slug: string, postSlug: string) {

View File

@ -10,9 +10,9 @@ 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",
}), }),
], ],
social: { social: {