chrismwilliams / astro-theme-cactus

A simple Astro theme. Use it to create your blog or website.
https://astro-cactus.chriswilliams.dev/
MIT License
866 stars 132 forks source link

How to use two different posts? #264

Closed Hankyu-Kim closed 2 months ago

Hankyu-Kim commented 2 months ago

Hello, thank you for the great theme.

By the way, I tried to organize my posts into two different menus. I attempted to use tags to display only posts related to a 'specific theme,' but things got complicated with files like [...page].astro, [slug].astro, and post.ts.

I also tried duplicating these files and changing CollectionEntry<"post"> to CollectionEntry<"something else">, but that didn’t work either.

Do you have any good references or can you explain how to create an additional post type to differentiate them?

chrismwilliams commented 2 months ago

Thanks @Hankyu-Kim!

So if I understand correctly, I would think about if you require creating another content collection which is similar to 'posts' (it could extend its schema), and would be a separate folder + route(s). One suggestion if you do go down this route would be, if/where possible, to refactor references of CollectionEntry<"post"> to CollectionEntry<"post" | "something else"> where their schemas overlap for re-use.

It sounds like though you've been trying to use a-tag/tags as a filter, which yeah does complicate routes but you could try priority order to filter/catch 'posts' with a specific tag (if that's how you're doing it).

Unfortunately, I don't have any references, but I did have a question a while ago about overriding a route based on tags that may help.

Hankyu-Kim commented 2 months ago

Thank you, the way refactoring references of CollectionEntry<"post"> to CollectionEntry<"post" | "something else"> where their schemas overlap for re-use helped me.

Hankyu-Kim commented 2 months ago

sorry for asking more,

I could divide CollectionEntry<"post"> to CollectionEntry<"post" | "something else"> but couldn't divide the function below:

export async function getAllPosts() { return await getCollection("post", ({ data }) => { return import.meta.env.PROD ? !data.draft : true; }); }

thie unchangable function induce the redundant problem on the file like rss.xml.ts

import rss from "@astrojs/rss"; import { siteConfig } from "@/site-config"; import { getAllPosts } from "@/data/post";

export const GET = async () => { const posts = await getAllPosts();

return rss({ title: siteConfig.title, description: siteConfig.description, site: import.meta.env.SITE, items: posts.map((post) => ({ title: post.data.title, description: post.data.description, pubDate: post.data.publishDate, link: posts/${post.slug}, })), }); };

Do you have any idea for this?

Hankyu-Kim commented 2 months ago

Reference : here is my repo : https://github.com/Hankyu-Kim/hankyukim.com and here is my website : https://hankyukim.com/

the main page posts should be CollectionEntry<"review"> - 404 error and Network menu posts should be CollectionEntry<"post">

chrismwilliams commented 2 months ago

You're probably going to have to create a union return type and query both "post" and "something else" if you want to reuse functions.

Just a quick note though, if your "reviews" are the "something else", they haven't been added to Astro's content collection with your current config, and will error out. As they mention, the 'key should match your collection directory name'