Change posts.js to blog.js and improve default values
This commit is contained in:
@ -2,7 +2,7 @@
|
|||||||
import Grid from '~/components/blog/Grid.astro';
|
import Grid from '~/components/blog/Grid.astro';
|
||||||
|
|
||||||
import { getBlogPermalink } from '~/utils/permalinks';
|
import { getBlogPermalink } from '~/utils/permalinks';
|
||||||
import { findPostsByIds } from '~/utils/posts';
|
import { findPostsByIds } from '~/utils/blog';
|
||||||
|
|
||||||
const ids = [
|
const ids = [
|
||||||
'get-started-website-with-astro-tailwind-css.md',
|
'get-started-website-with-astro-tailwind-css.md',
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
import Grid from '~/components/blog/Grid.astro';
|
import Grid from '~/components/blog/Grid.astro';
|
||||||
|
|
||||||
import { getBlogPermalink } from '~/utils/permalinks';
|
import { getBlogPermalink } from '~/utils/permalinks';
|
||||||
import { findLatestPosts } from '~/utils/posts';
|
import { findLatestPosts } from '~/utils/blog';
|
||||||
|
|
||||||
const count = 4;
|
const count = 4;
|
||||||
const posts = await findLatestPosts({ count });
|
const posts = await findLatestPosts({ count });
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { z, defineCollection } from 'astro:content';
|
import { z, defineCollection } from 'astro:content';
|
||||||
|
import { cleanSlug } from '~/utils/permalinks';
|
||||||
|
|
||||||
const blog = defineCollection({
|
const blog = defineCollection({
|
||||||
schema: {
|
schema: {
|
||||||
@ -9,16 +10,16 @@ const blog = defineCollection({
|
|||||||
canonical: z.string().url().optional(),
|
canonical: z.string().url().optional(),
|
||||||
permalink: z.string().optional(),
|
permalink: z.string().optional(),
|
||||||
|
|
||||||
publishDate: z.string().transform((str) => new Date(str)),
|
publishDate: z.date().optional(),
|
||||||
draft: z.boolean().optional(),
|
draft: z.boolean().optional(),
|
||||||
|
|
||||||
excerpt: z.string().optional(),
|
excerpt: z.string().optional(),
|
||||||
category: z.string().optional(),
|
category: z.string().optional(),
|
||||||
tags: z.array(z.string()).optional(),
|
tags: z.array(z.string()).optional(),
|
||||||
authors: z.array(z.string()).optional(),
|
author: z.string().optional(),
|
||||||
},
|
},
|
||||||
slug: ({ defaultSlug, data }) => {
|
slug: ({ defaultSlug, data }) => {
|
||||||
return data.permalink || defaultSlug;
|
return cleanSlug(data.permalink || defaultSlug);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -5,14 +5,13 @@ import Layout from '~/layouts/PageLayout.astro';
|
|||||||
import BlogList from '~/components/blog/List.astro';
|
import BlogList from '~/components/blog/List.astro';
|
||||||
import Pagination from '~/components/common/Pagination.astro';
|
import Pagination from '~/components/common/Pagination.astro';
|
||||||
|
|
||||||
import { fetchPosts } from '~/utils/posts';
|
import { fetchPosts } from '~/utils/blog';
|
||||||
import { getCanonical, getPermalink, BLOG_BASE } from '~/utils/permalinks';
|
import { BLOG_BASE } from '~/utils/permalinks';
|
||||||
import Title from '~/components/blog/Title.astro';
|
import Title from '~/components/blog/Title.astro';
|
||||||
|
|
||||||
export async function getStaticPaths({ paginate }) {
|
export async function getStaticPaths({ paginate }) {
|
||||||
if (BLOG?.disabled || BLOG?.blog?.disabled) return [];
|
if (BLOG?.disabled || BLOG?.blog?.disabled) return [];
|
||||||
const posts = await fetchPosts();
|
return paginate(await fetchPosts(), {
|
||||||
return paginate(posts, {
|
|
||||||
params: { blog: BLOG_BASE || undefined },
|
params: { blog: BLOG_BASE || undefined },
|
||||||
pageSize: BLOG.postsPerPage,
|
pageSize: BLOG.postsPerPage,
|
||||||
});
|
});
|
||||||
|
@ -5,8 +5,8 @@ import Layout from '~/layouts/PageLayout.astro';
|
|||||||
import BlogList from '~/components/blog/List.astro';
|
import BlogList from '~/components/blog/List.astro';
|
||||||
import Pagination from '~/components/common/Pagination.astro';
|
import Pagination from '~/components/common/Pagination.astro';
|
||||||
|
|
||||||
import { fetchPosts } from '~/utils/posts';
|
import { fetchPosts } from '~/utils/blog';
|
||||||
import { cleanSlug, CATEGORY_BASE } from '~/utils/permalinks';
|
import { CATEGORY_BASE } from '~/utils/permalinks';
|
||||||
import Title from '~/components/blog/Title.astro';
|
import Title from '~/components/blog/Title.astro';
|
||||||
|
|
||||||
export async function getStaticPaths({ paginate }) {
|
export async function getStaticPaths({ paginate }) {
|
||||||
@ -22,7 +22,7 @@ export async function getStaticPaths({ paginate }) {
|
|||||||
paginate(
|
paginate(
|
||||||
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
|
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
|
||||||
{
|
{
|
||||||
params: { category: cleanSlug(category), blog: CATEGORY_BASE || undefined },
|
params: { category: category, blog: CATEGORY_BASE || undefined },
|
||||||
pageSize: BLOG.postsPerPage,
|
pageSize: BLOG.postsPerPage,
|
||||||
props: { category },
|
props: { category },
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,15 @@ import Layout from '~/layouts/PageLayout.astro';
|
|||||||
import SinglePost from '~/components/blog/SinglePost.astro';
|
import SinglePost from '~/components/blog/SinglePost.astro';
|
||||||
import ToBlogLink from '~/components/blog/ToBlogLink.astro';
|
import ToBlogLink from '~/components/blog/ToBlogLink.astro';
|
||||||
|
|
||||||
import { getCanonical, getPermalink, cleanSlug, POST_BASE } from '~/utils/permalinks';
|
import { getCanonical, getPermalink, POST_BASE } from '~/utils/permalinks';
|
||||||
import { fetchPosts } from '~/utils/posts';
|
import { fetchPosts } from '~/utils/blog';
|
||||||
import { findImage } from '~/utils/images';
|
import { findImage } from '~/utils/images';
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
if (BLOG?.disabled || BLOG?.post?.disabled) return [];
|
if (BLOG?.disabled || BLOG?.post?.disabled) return [];
|
||||||
const posts = await fetchPosts();
|
return (await fetchPosts()).map((post) => ({
|
||||||
return posts.map((post) => ({
|
|
||||||
params: {
|
params: {
|
||||||
slug: cleanSlug(post.slug),
|
slug: post.slug,
|
||||||
blog: POST_BASE || undefined,
|
blog: POST_BASE || undefined,
|
||||||
},
|
},
|
||||||
props: { post },
|
props: { post },
|
||||||
|
@ -5,8 +5,8 @@ import Layout from '~/layouts/PageLayout.astro';
|
|||||||
import BlogList from '~/components/blog/List.astro';
|
import BlogList from '~/components/blog/List.astro';
|
||||||
import Pagination from '~/components/common/Pagination.astro';
|
import Pagination from '~/components/common/Pagination.astro';
|
||||||
|
|
||||||
import { fetchPosts } from '~/utils/posts';
|
import { fetchPosts } from '~/utils/blog';
|
||||||
import { getCanonical, getPermalink, cleanSlug, TAG_BASE } from '~/utils/permalinks';
|
import { TAG_BASE } from '~/utils/permalinks';
|
||||||
import Title from '~/components/blog/Title.astro';
|
import Title from '~/components/blog/Title.astro';
|
||||||
|
|
||||||
export async function getStaticPaths({ paginate }) {
|
export async function getStaticPaths({ paginate }) {
|
||||||
@ -22,7 +22,7 @@ export async function getStaticPaths({ paginate }) {
|
|||||||
paginate(
|
paginate(
|
||||||
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
|
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
|
||||||
{
|
{
|
||||||
params: { tag: cleanSlug(tag), blog: TAG_BASE || undefined },
|
params: { tag: tag, blog: TAG_BASE || undefined },
|
||||||
pageSize: BLOG.postsPerPage,
|
pageSize: BLOG.postsPerPage,
|
||||||
props: { tag },
|
props: { tag },
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import rss from '@astrojs/rss';
|
import rss from '@astrojs/rss';
|
||||||
|
|
||||||
import { SITE, BLOG } from '~/config.mjs';
|
import { SITE, BLOG } from '~/config.mjs';
|
||||||
import { fetchPosts } from '~/utils/posts';
|
import { fetchPosts } from '~/utils/blog';
|
||||||
import { getPermalink } from '~/utils/permalinks';
|
import { getPermalink } from '~/utils/permalinks';
|
||||||
|
|
||||||
export const get = async () => {
|
export const get = async () => {
|
||||||
|
@ -16,7 +16,7 @@ export interface Post {
|
|||||||
excerpt?: string;
|
excerpt?: string;
|
||||||
category?: string;
|
category?: string;
|
||||||
tags?: Array<string>;
|
tags?: Array<string>;
|
||||||
authors?: Array<string>;
|
author?: string;
|
||||||
|
|
||||||
Content: unknown;
|
Content: unknown;
|
||||||
content?: string;
|
content?: string;
|
||||||
|
@ -1,15 +1,24 @@
|
|||||||
import { getCollection, getEntry } from 'astro:content';
|
import { getCollection, getEntry } from 'astro:content';
|
||||||
import type { CollectionEntry } from 'astro:content';
|
import type { CollectionEntry } from 'astro:content';
|
||||||
import type { Post } from '~/types';
|
import type { Post } from '~/types';
|
||||||
|
import { cleanSlug } from './permalinks';
|
||||||
|
|
||||||
const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> => {
|
const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise<Post> => {
|
||||||
const { id, slug, data } = post;
|
const { id, slug, data } = post;
|
||||||
const { Content, injectedFrontmatter } = await post.render();
|
const { Content, injectedFrontmatter } = await post.render();
|
||||||
|
|
||||||
|
const { tags = [], category = 'default', author = 'Anonymous', publishDate, ...rest } = data;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: id,
|
id: id,
|
||||||
slug: slug,
|
slug: slug,
|
||||||
...data,
|
|
||||||
|
publishDate: new Date(publishDate),
|
||||||
|
category: cleanSlug(category),
|
||||||
|
tags: tags.map((tag: string) => cleanSlug(tag)),
|
||||||
|
author,
|
||||||
|
|
||||||
|
...rest,
|
||||||
|
|
||||||
Content: Content,
|
Content: Content,
|
||||||
// or 'body' in case you consume from API
|
// or 'body' in case you consume from API
|
Reference in New Issue
Block a user