Skip to content

Commit

Permalink
Refactor import paths, update import types, and cleanup deprecated co…
Browse files Browse the repository at this point in the history
…ntent helper functions
  • Loading branch information
Adammatthiesen committed Dec 12, 2024
1 parent 9c4cb81 commit 30f53d6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 56 deletions.
4 changes: 2 additions & 2 deletions packages/studiocms_blog/src/components/PageList.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
import { pages } from '@studiocms/blog:context';
import { FormattedDate } from 'studiocms:components';
import type { pageDataReponse } from 'studiocms:helpers/contentHelper';
import { CustomImage } from 'studiocms:imageHandler/components';
import { pathWithBase } from 'studiocms:lib';
import type { CombinedPageData } from 'studiocms:sdk/types';
const blogRouteFullPath = pages.get('/blog/[...slug]');
Expand All @@ -15,7 +15,7 @@ function getBlogRoute(slug: string) {
}
interface Props {
blogPageList: pageDataReponse[];
blogPageList: CombinedPageData[];
}
const { blogPageList } = Astro.props;
Expand Down
19 changes: 13 additions & 6 deletions packages/studiocms_blog/src/pages/blog/[...slug].astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
import { PostHeader } from '@studiocms:blog/components';
import { BlogLayout } from '@studiocms:blog/layouts';
import { contentHelper } from 'studiocms:helpers/contentHelper';
import { StudioCMSRenderer } from 'studiocms:renderer';
import studioCMS_SDK from 'studiocms:sdk';
import { name } from '../../../package.json';
// Get the slug from the URL
Expand All @@ -14,15 +14,22 @@ if (!slug) {
}
// Fetch the blog post content
const { id, title, description, heroImage, publishedAt, updatedAt, content } = await contentHelper(
slug,
name
);
const post = await studioCMS_SDK.GET.databaseEntry.pages.bySlug(slug, name);
// If no content is found, redirect to 404
if (!id) {
if (!post) {
return new Response(null, { status: 404 });
}
const { title, description, heroImage, publishedAt, updatedAt, defaultContent } = post;
// If no content is found, redirect to 404
if (!defaultContent) {
return new Response(null, { status: 404 });
}
const content = defaultContent.content || '';
---
<BlogLayout {title} {description} {heroImage}>
<main>
Expand Down
9 changes: 2 additions & 7 deletions packages/studiocms_blog/src/pages/blog/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import config from '@studiocms/blog:config';
import { pages } from '@studiocms/blog:context';
import { PageList, RSSIcon } from '@studiocms:blog/components';
import { BlogLayout } from '@studiocms:blog/layouts';
import { getPageList } from 'studiocms:helpers/contentHelper';
import studioCMS_SDK from 'studiocms:sdk';
import { name } from '../../../package.json';
// Set the title and description
Expand All @@ -14,15 +14,10 @@ const description: string = configDescription || 'Blog Index';
const showRSSFeed: boolean = blogIndex?.showRSSFeed || true;
// Get all pages
const pageList = await getPageList();
const blogPageList = await studioCMS_SDK.GET.packagePages(name);
// Get the RSS feed URL
const rssPath = pages.get('/rss.xml');
// Get all blog pages and sort by published date
const blogPageList = pageList
.filter((page) => page.package === name)
.sort((a, b) => Date.parse(b.publishedAt.toString()) - Date.parse(a.publishedAt.toString()));
---
<BlogLayout {title} description={description} >
<main>
Expand Down
51 changes: 16 additions & 35 deletions packages/studiocms_blog/src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import config from '@studiocms/blog:config';
import { pages } from '@studiocms/blog:context';
import { contentHelper, getPageList, getSiteConfig } from 'studiocms:helpers/contentHelper';
import { pathWithBase } from 'studiocms:lib';
import { StudioCMSRenderer } from 'studiocms:renderer';
import rss from '@astrojs/rss';
import studioCMS_SDK from 'studiocms:sdk';
import rss, { type RSSFeedItem } from '@astrojs/rss';
import type { APIContext } from 'astro';
import { name } from '../../package.json';

import { experimental_AstroContainer as AstroContainer } from 'astro/container';

const blogRouteFullPath = pages.get('/blog/[...slug]');

function getBlogRoute(slug: string) {
Expand All @@ -20,41 +17,25 @@ function getBlogRoute(slug: string) {

export async function GET(context: APIContext) {
// Get Config from Studio Database
const studioCMSConfig = await getSiteConfig();
const studioCMSConfig = await studioCMS_SDK.GET.database.config();

// Set Title, Description, and Site
const title = config.title ?? studioCMSConfig.title;
const description = config.description ?? studioCMSConfig.description;
const title = config.title ?? studioCMSConfig?.title ?? 'Blog';
const description = config.description ?? studioCMSConfig?.description ?? 'Blog';
const site = context.site ?? 'https://example.com';

// Get all Posts from Studio Database
const unorderedPosts = await getPageList();

// Order Posts by Published Date
const orderedPosts = unorderedPosts
.filter((page) => page.package === name)
.sort((a, b) => Date.parse(b.publishedAt.toString()) - Date.parse(a.publishedAt.toString()));

// Render Known Posts
const items = await Promise.all(
orderedPosts.map(async ({ slug, title, description, publishedAt: pubDate, package: pkg }) => {
const { content: fetchedContent } = await contentHelper(slug, pkg);
const container = await AstroContainer.create();
const renderedContent = await container.renderToString(StudioCMSRenderer, {
props: { content: fetchedContent },
});

const content = renderedContent
.replace(/<!DOCTYPE html>/, '')
.replace(/<html.*?>/, '')
.replace(/<\/html>/, '')
.trim();

const link = pathWithBase(getBlogRoute(slug));

return { title, description, pubDate, content, link };
})
);
const orderedPosts = await studioCMS_SDK.GET.packagePages(name);

const items: RSSFeedItem[] = orderedPosts.map((post) => {
return {
title: post.title,
description: post.description,
pubDate: post.publishedAt,
link: pathWithBase(getBlogRoute(post.slug)),
categories: post.categories.map(({ name }) => name),
};
});

return rss({ title, description, site, items });
}
14 changes: 11 additions & 3 deletions packages/studiocms_frontend/src/routes/[...slug].astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import { contentHelper } from 'studiocms:helpers/contentHelper';
import { StudioCMSRenderer } from 'studiocms:renderer';
import studioCMS_SDK from 'studiocms:sdk';
import Layout from '../components/Layout.astro';
const { slug } = Astro.params;
Expand All @@ -9,11 +9,19 @@ if (!slug) {
return new Response(null, { status: 404 });
}
const { title, description, heroImage, content, id } = await contentHelper(slug);
const page = await studioCMS_SDK.GET.databaseEntry.pages.bySlug(slug, 'studiocms');
if (!id) {
if (!page) {
return new Response(null, { status: 404 });
}
const { title, description, heroImage, defaultContent } = page;
if (!defaultContent) {
return new Response(null, { status: 404 });
}
const content = defaultContent.content || '';
---

<Layout title={title} description={description} heroImage={heroImage}>
Expand Down
14 changes: 11 additions & 3 deletions packages/studiocms_frontend/src/routes/index.astro
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
---
import { contentHelper } from 'studiocms:helpers/contentHelper';
import { StudioCMSRenderer } from 'studiocms:renderer';
import studioCMS_SDK from 'studiocms:sdk';
import Layout from '../components/Layout.astro';
const { title, description, heroImage, content, id } = await contentHelper('index');
const index = await studioCMS_SDK.GET.databaseEntry.pages.bySlug('index', 'studiocms');
if (!id) {
if (!index) {
return new Response(null, { status: 404 });
}
const { title, description, heroImage, defaultContent } = index;
if (!defaultContent) {
return new Response(null, { status: 404 });
}
const content = defaultContent.content || '';
---

<Layout title={title} description={description} heroImage={heroImage}>
Expand Down

0 comments on commit 30f53d6

Please sign in to comment.