From b462c105b56fcb5b47e8ffd94b627cb39ff9fdd5 Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Mon, 19 Feb 2024 21:06:10 -0800 Subject: [PATCH] more progress --- packages/starlight-ghostcms/index.ts | 5 +- .../src/components/PrevNextLinks.astro | 57 -------- .../src/overrides/MarkdownContent.astro | 16 ++- .../starlight-ghostcms/src/routes/index.astro | 14 +- .../src/utils/api/api-functions.ts | 17 +++ .../starlight-ghostcms/src/utils/content.ts | 135 ------------------ 6 files changed, 35 insertions(+), 209 deletions(-) delete mode 100644 packages/starlight-ghostcms/src/components/PrevNextLinks.astro delete mode 100644 packages/starlight-ghostcms/src/utils/content.ts diff --git a/packages/starlight-ghostcms/index.ts b/packages/starlight-ghostcms/index.ts index 6ea9d24f..6df25449 100644 --- a/packages/starlight-ghostcms/index.ts +++ b/packages/starlight-ghostcms/index.ts @@ -26,8 +26,9 @@ export default function starlightBlogPlugin(userConfig?: StarlightGhostConfig): hooks: { 'astro:config:setup': ({ injectRoute, updateConfig }) => { injectRoute({ - pattern: '', - entrypoint: '' + pattern: '@matthiesenxyz/starlight-ghostcms/routes/index.astro', + entrypoint: '/blog', + prerender: true, }) updateConfig({ diff --git a/packages/starlight-ghostcms/src/components/PrevNextLinks.astro b/packages/starlight-ghostcms/src/components/PrevNextLinks.astro deleted file mode 100644 index 0aec5015..00000000 --- a/packages/starlight-ghostcms/src/components/PrevNextLinks.astro +++ /dev/null @@ -1,57 +0,0 @@ ---- -import type { StarlightBlogLink } from '../utils/content' - -interface Props { - next: StarlightBlogLink | undefined - prev: StarlightBlogLink | undefined -} - -const { next, prev } = Astro.props ---- - -{ - prev || next ? ( - - ) : null -} - - diff --git a/packages/starlight-ghostcms/src/overrides/MarkdownContent.astro b/packages/starlight-ghostcms/src/overrides/MarkdownContent.astro index fdf107f9..e643701a 100644 --- a/packages/starlight-ghostcms/src/overrides/MarkdownContent.astro +++ b/packages/starlight-ghostcms/src/overrides/MarkdownContent.astro @@ -2,26 +2,28 @@ import StarlightMarkdownContent from '@astrojs/starlight/components/MarkdownContent.astro' import type { Props } from '@astrojs/starlight/props' -import PrevNextLinks from '../components/PrevNextLinks.astro' -import { getBlogEntry, type StarlightBlogEntryPaginated } from '../utils/content' import { isAnyBlogPostPage } from '../utils/page' import Metadata from '../components/Metadata.astro' +import type { Post } from '../schemas/posts' +import { getSluggedPost } from '../utils/api' const isBlogPost = isAnyBlogPostPage(Astro.props.slug) -let blogEntry: StarlightBlogEntryPaginated | undefined = undefined +let blogEntry: Post | undefined = undefined if (isBlogPost) { - blogEntry = await getBlogEntry(Astro.url.pathname) + blogEntry = await getSluggedPost(Astro.props.slug) } --- - {isBlogPost && blogEntry ? : null} - + {isBlogPost && blogEntry ? : null} + +
PlaceHolder for HTML
+ { isBlogPost && blogEntry ? ( ) : null } diff --git a/packages/starlight-ghostcms/src/routes/index.astro b/packages/starlight-ghostcms/src/routes/index.astro index 07cbaeb6..6d3ab59c 100644 --- a/packages/starlight-ghostcms/src/routes/index.astro +++ b/packages/starlight-ghostcms/src/routes/index.astro @@ -1,20 +1,18 @@ --- -import type { InferGetStaticPropsType } from 'astro' +//import type { InferGetStaticPropsType } from 'astro' import config from 'virtual:starlight-ghost-config' import Page from '../components/Page.astro' import Posts from '../components/Posts.astro' -import PrevNextLinks from '../components/PrevNextLinks.astro' +//import PrevNextLinks from '../components/PrevNextLinks.astro' import { getPageProps } from '../utils/page' +import { getAllPosts } from '../utils/api' export const prerender = true +const entries = await getAllPosts(); -export function getStaticPaths() { -} -type Props = InferGetStaticPropsType - -const { entries, nextLink, prevLink } = Astro.props +//const { entries, nextLink, prevLink } = Astro.props const pageProps = getPageProps(config.title) --- @@ -22,7 +20,7 @@ const pageProps = getPageProps(config.title)
- +
diff --git a/packages/starlight-ghostcms/src/utils/api/api-functions.ts b/packages/starlight-ghostcms/src/utils/api/api-functions.ts index 6e29a020..cdcc1619 100644 --- a/packages/starlight-ghostcms/src/utils/api/api-functions.ts +++ b/packages/starlight-ghostcms/src/utils/api/api-functions.ts @@ -67,6 +67,23 @@ export const getAllPosts = async () => { return posts; }; +export const getSluggedPost = async (slug:string) => { + const result = await api.posts + .read({slug: slug}) + .include({ + authors: true, + tags: true, + }).fetch() + + if (result.success) { + const post: Post = result.data; + return post + } + if (result.errors) { + console.log(result.errors.map((e) => e.message).join("\n")); + } +}; + export const getAllPages = async () => { const pages: Page[] = []; let cursor = await api.pages diff --git a/packages/starlight-ghostcms/src/utils/content.ts b/packages/starlight-ghostcms/src/utils/content.ts deleted file mode 100644 index 9c9a6b0c..00000000 --- a/packages/starlight-ghostcms/src/utils/content.ts +++ /dev/null @@ -1,135 +0,0 @@ -import type { z } from 'astro/zod' -import { getCollection, type AstroCollectionEntry } from 'astro:content' -import starlightConfig from 'virtual:starlight/user-config' -import config from 'virtual:starlight-ghost-config' - -import type { Author } from '../schemas/authors' - -export async function getBlogStaticPaths() { - const entries = await getBlogEntries() - - const entryPages: StarlightBlogEntry[][] = [] - - for (const entry of entries) { - const lastPage = entryPages.at(-1) - - if (!lastPage || lastPage.length === config.postCount) { - entryPages.push([entry]) - } else { - lastPage.push(entry) - } - } - - if (entryPages.length === 0) { - entryPages.push([]) - } - - return entryPages.map((entries, index) => { - const prevPage = index === 0 ? undefined : entryPages.at(index - 1) - const nextPage = entryPages.at(index + 1) - - return { - params: { - page: index === 0 ? undefined : index + 1, - }, - props: { - entries, - nextLink: nextPage ? { href: `/blog/${index + 2}`, label: 'Older posts' } : undefined, - prevLink: prevPage ? { href: index === 1 ? '/blog' : `/blog/${index}`, label: 'Newer posts' } : undefined, - } satisfies StarlightBlogStaticProps, - } - }) -} - -export async function getRecentBlogEntries() { - const entries = await getBlogEntries() - - return entries.slice(0, config.recentPostCount) -} - -export async function getBlogEntry(slug: string): Promise { - const entries = await getBlogEntries() - - const entryIndex = entries.findIndex((entry) => entry.slug === slug.replace(/^\//, '').replace(/\/$/, '')) - const entry = entries[entryIndex] - - if (!entry) { - throw new Error(`Blog post with slug '${slug}' not found.`) - } - - const prevEntry = entries[entryIndex - 1] - const nextEntry = entries[entryIndex + 1] - - return { - entry, - nextLink: nextEntry ? { href: `/${nextEntry.slug}`, label: nextEntry.data.title } : undefined, - prevLink: prevEntry ? { href: `/${prevEntry.slug}`, label: prevEntry.data.title } : undefined, - } -} - -export function getBlogEntryMetadata(entry: StarlightBlogEntry): StarlightBlogEntryMetadata { - const authors: Author[] = [] - - if (!entry.data.authors) { - authors.push(...Object.values(authors)) - } else { - authors.push(entry.data.authors) - } - - return { - authors, - date: entry.data.date.toLocaleDateString(starlightConfig.defaultLocale.lang, { dateStyle: 'medium' }), - } -} - -export async function getBlogEntries() { - const entries = await getCollection('docs', ({ id }) => { - return id.startsWith('blog/') && id !== 'blog/index.mdx' - }) - - return entries.sort((a, b) => { - return b.data.date.getTime() - a.data.date.getTime() - }) -} - -export async function getBlogEntryExcerpt(entry: StarlightBlogEntry) { - if (entry.data.excerpt) { - return entry.data.excerpt - } - - const { Content } = await entry.render() - - return Content -} - -type StarlightEntryData = z.infer> & { title: string } -type StarlightEntry = AstroCollectionEntry - -export type StarlightBlogEntry = StarlightEntry & { - data: { - date: Date - } -} - -export interface StarlightBlogLink { - href: string - label: string -} - -export interface StarlightBlogEntryPaginated { - entry: StarlightBlogEntry - nextLink: StarlightBlogLink | undefined - prevLink: StarlightBlogLink | undefined -} - -interface StarlightBlogEntryMetadata { - authors: Author[] - date: string -} - - -interface StarlightBlogStaticProps { - entries: StarlightBlogEntry[] - nextLink: StarlightBlogLink | undefined - prevLink: StarlightBlogLink | undefined -}