From e99553f4139f088fa13427e6da08045e1e2c7379 Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Wed, 21 Feb 2024 16:22:58 -0800 Subject: [PATCH 1/2] add authors page, facebook and twitter autofil from ghost if not set by user, and added an about page that will auto populate if users have a Page on their ghost server called about like in the default configuration. --- packages/starlight-ghostcms/index.ts | 19 ++++- packages/starlight-ghostcms/package.json | 2 + .../src/components/AdvancedAuthorCard.astro | 70 +++++++++++++++++++ .../src/components/Author.astro | 4 +- .../src/overrides/Sidebar.astro | 44 +++++++++++- .../src/overrides/SiteTitle.astro | 3 +- .../src/overrides/sidebartypes.ts | 46 ++++++++++++ .../starlight-ghostcms/src/routes/about.astro | 36 ++++++++++ .../src/routes/authors.astro | 48 +++++++++++++ .../src/utils/api/api-functions.ts | 16 +++++ 10 files changed, 280 insertions(+), 8 deletions(-) create mode 100644 packages/starlight-ghostcms/src/components/AdvancedAuthorCard.astro create mode 100644 packages/starlight-ghostcms/src/overrides/sidebartypes.ts create mode 100644 packages/starlight-ghostcms/src/routes/about.astro create mode 100644 packages/starlight-ghostcms/src/routes/authors.astro diff --git a/packages/starlight-ghostcms/index.ts b/packages/starlight-ghostcms/index.ts index ad9f00ae..0c018270 100644 --- a/packages/starlight-ghostcms/index.ts +++ b/packages/starlight-ghostcms/index.ts @@ -2,12 +2,16 @@ import type { StarlightPlugin, StarlightUserConfig } from '@astrojs/starlight/ty import type { AstroIntegrationLogger } from 'astro' import { type StarlightGhostConfig, validateConfig } from './src/schemas/config' import { vitePluginStarlightGhostConfig } from './src/integrations/vite' +import { facebook, getSettings, invariant, twitter } from './src/utils/api' + +const settings = await getSettings() export type { StarlightGhostConfig } export default function starlightGhostCMS(userConfig?: StarlightGhostConfig): StarlightPlugin { const config: StarlightGhostConfig = validateConfig(userConfig) - + invariant(settings, "Settings not available... check your api key/url") + return { name: '@matthiesenxyz/starlight-ghostcms-plugin', hooks: { @@ -15,7 +19,9 @@ export default function starlightGhostCMS(userConfig?: StarlightGhostConfig): St updateStarlightConfig({ social: { ...starlightConfig.social, - rss: `${astroConfig.site}/rss.xml` + rss: `${astroConfig.site}/rss.xml`, + twitter: twitter(settings.twitter?settings.twitter:""), + facebook: facebook(settings.facebook?settings.facebook:""), }, components: { ...starlightConfig.components, @@ -39,6 +45,15 @@ export default function starlightGhostCMS(userConfig?: StarlightGhostConfig): St entrypoint: '@matthiesenxyz/starlight-ghostcms/routes/[slug].astro', prerender: true, }) + injectRoute({ + pattern: '/blog/about', + entrypoint: '@matthiesenxyz/starlight-ghostcms/routes/about.astro', + prerender: true, + }) + injectRoute({ + pattern: '/blog/authors', + entrypoint: '@matthiesenxyz/starlight-ghostcms/routes/authors.astro', + }) injectRoute({ pattern: '/rss.xml', entrypoint: '@matthiesenxyz/starlight-ghostcms/routes/rss.xml.ts' diff --git a/packages/starlight-ghostcms/package.json b/packages/starlight-ghostcms/package.json index deb081fd..b2a7d8ff 100644 --- a/packages/starlight-ghostcms/package.json +++ b/packages/starlight-ghostcms/package.json @@ -46,6 +46,8 @@ "./overrides/Sidebar.astro": "./src/overrides/Sidebar.astro", "./overrides/SiteTitle.astro": "./src/overrides/SiteTitle.astro", "./routes/index.astro": "./src/routes/index.astro", + "./routes/about.astro": "./src/routes/about.astro", + "./routes/authors.astro": "./src/routes/authors.astro", "./routes/[slug].astro": "./src/routes/[slug].astro", "./routes/rss.xml.ts": "./src/routes/rss.xml.ts", "./schema": "./src/schemas/config.ts" diff --git a/packages/starlight-ghostcms/src/components/AdvancedAuthorCard.astro b/packages/starlight-ghostcms/src/components/AdvancedAuthorCard.astro new file mode 100644 index 00000000..fd16ec71 --- /dev/null +++ b/packages/starlight-ghostcms/src/components/AdvancedAuthorCard.astro @@ -0,0 +1,70 @@ +--- +import type { Author } from '../schemas/authors' +import { facebook } from '../utils/api' + +interface Props { + author: Author +} + +const { author } = Astro.props + +--- + +
+ {author.profile_image && {author.name}} +
+
{author.name}
+ {author.website && + + } + {author.facebook && + + } + {author.twitter && + + } + {author.bio &&
{author.bio}
} + + +
+
+ + diff --git a/packages/starlight-ghostcms/src/components/Author.astro b/packages/starlight-ghostcms/src/components/Author.astro index bbf982a7..bfacf6e3 100644 --- a/packages/starlight-ghostcms/src/components/Author.astro +++ b/packages/starlight-ghostcms/src/components/Author.astro @@ -7,11 +7,11 @@ interface Props { const { author } = Astro.props -const isLink = author.website !== undefined +const isLink = author.slug !== undefined const Element = isLink ? 'a' : 'div' --- - + {author.profile_image && {author.name}}
{author.name}
diff --git a/packages/starlight-ghostcms/src/overrides/Sidebar.astro b/packages/starlight-ghostcms/src/overrides/Sidebar.astro index 9e091e04..a75b80b9 100644 --- a/packages/starlight-ghostcms/src/overrides/Sidebar.astro +++ b/packages/starlight-ghostcms/src/overrides/Sidebar.astro @@ -3,23 +3,63 @@ import StarlightSidebar from '@astrojs/starlight/components/Sidebar.astro' import type { Props } from '@astrojs/starlight/props' import config from 'virtual:starlight-ghost-config' import { isBlogPostPage, isBlogRoot } from '../utils/page' -import { getAllPosts } from '../utils/api' +import { getAllPages, getAllPosts, getSluggedPage } from '../utils/api/api-functions.js' +import type { SidebarEntry } from './sidebartypes' export async function getRecentBlogEntries(){ const entries = await getAllPosts() return entries.slice(0, config.recentPostCount) } +export async function getBlogPageEntries(){ + const entries = await getAllPages() + return entries; +} + export function checkpath(path: string){ if ( path.slice(0, 5) === "/blog" ){ return true } else { return false } } + +export function isAbout(path: string){ + if ( path === "/blog/about" ){ + return true + } else { return false } +} + +export function isAuthors(path: string){ + if ( path === "/blog/authors" ){ + return true + } else { return false } +} const isBlog = checkpath(Astro.url.pathname) const recentEntries = isBlog ? await getRecentBlogEntries() : [] +const aboutPage = await getSluggedPage("about"); +const AboutEntry:SidebarEntry = { + attrs: {}, badge: undefined, + href: '/blog/about', + isCurrent: isAbout(Astro.url.pathname), + type: 'link', + label: aboutPage?.post?.title +} + +const emptyEntry:SidebarEntry = { attrs: {}, badge: undefined, + href: '#', isCurrent: false, type: 'link', label: '', } + +const about = aboutPage?AboutEntry:emptyEntry const blogSidebar: Props['sidebar'] = isBlog ? [ + about, + { + attrs: {}, + badge: undefined, + href: '/blog/authors', + isCurrent: isAuthors(Astro.url.pathname), + label: 'Our Authors', + type: 'link', + }, { attrs: {}, badge: undefined, @@ -33,7 +73,7 @@ const blogSidebar: Props['sidebar'] = isBlog collapsed: false, entries: recentEntries.map((blogEntry) => ({ attrs: {}, - badge: undefined, + badge: blogEntry.featured?({text: "⭐", variant: "note"}):undefined, href: `/blog/${blogEntry.slug}`, isCurrent: isBlogPostPage(Astro.props.slug, `blog/${blogEntry.slug}`), label: blogEntry.title, diff --git a/packages/starlight-ghostcms/src/overrides/SiteTitle.astro b/packages/starlight-ghostcms/src/overrides/SiteTitle.astro index 3f5dd01a..328b4874 100644 --- a/packages/starlight-ghostcms/src/overrides/SiteTitle.astro +++ b/packages/starlight-ghostcms/src/overrides/SiteTitle.astro @@ -1,12 +1,11 @@ --- import type { Props } from "@astrojs/starlight/props"; import AstrolightSiteTitle from "@astrojs/starlight/components/SiteTitle.astro"; -import config from 'virtual:starlight-ghost-config' --- \ No newline at end of file diff --git a/packages/starlight-ghostcms/src/routes/authors.astro b/packages/starlight-ghostcms/src/routes/authors.astro new file mode 100644 index 00000000..0d965af0 --- /dev/null +++ b/packages/starlight-ghostcms/src/routes/authors.astro @@ -0,0 +1,48 @@ +--- +import config from 'virtual:starlight-ghost-config' +import Page from '../components/Page.astro' +//import PrevNextLinks from '../components/PrevNextLinks.astro' +import { getAllAuthors } from '../utils/api/api-functions' +import { getPageProps } from '../utils/page' +import AdvancedAuthorCard from '../components/AdvancedAuthorCard.astro'; + +//const { entries, nextLink, prevLink } = Astro.props + +const { authors } = await getAllAuthors(); + +const pageProps = getPageProps("Our Authors") +--- + + + {config.supportGhost && ( +
Powered by Ghost
+ )} + + +
+
    + {authors.map((author: any) => ( +
  • + +
  • + ))} +
+ +
+ +
+ + + \ No newline at end of file diff --git a/packages/starlight-ghostcms/src/utils/api/api-functions.ts b/packages/starlight-ghostcms/src/utils/api/api-functions.ts index 57fa7b7f..e513b3ce 100644 --- a/packages/starlight-ghostcms/src/utils/api/api-functions.ts +++ b/packages/starlight-ghostcms/src/utils/api/api-functions.ts @@ -106,6 +106,22 @@ export const getAllPages = async () => { return pages; }; +export const getSluggedPage = async (slug:string) => { + const results = await api.pages + .read({slug: slug}) + .include({ + authors: true, + tags: true, + }).fetch() + + if (!results.success) { + throw new Error(results.errors.map((e) => e.message).join(", ")); + } + return { + post: results.data, + }; +}; + export const getSettings = async () => { const res = await api.settings.fetch(); if (res.success) { From cb979d5b0ffba074be33ae5cdb88ec5f897e21ac Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Wed, 21 Feb 2024 16:26:09 -0800 Subject: [PATCH 2/2] add changeset --- .changeset/soft-readers-shout.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/soft-readers-shout.md diff --git a/.changeset/soft-readers-shout.md b/.changeset/soft-readers-shout.md new file mode 100644 index 00000000..8b5dda77 --- /dev/null +++ b/.changeset/soft-readers-shout.md @@ -0,0 +1,5 @@ +--- +"@matthiesenxyz/starlight-ghostcms": patch +--- + +Adds 2 new pages, Authors, and about page(Link will disappear if you dont have the default ghost about page with slug "about"). Also adds auto links from ghost settings for twitter and facebook if not set my the user in starlight.