Skip to content

Commit

Permalink
feat: 更改 pubDate
Browse files Browse the repository at this point in the history
  • Loading branch information
songxingguo committed Jun 5, 2024
1 parent c9468c6 commit 664a0d3
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/components/Post.astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const url = !post.data.original
</h1>
<div class="mt-2 text-3.5">
<span>{t("posted_at")}</span>
<time>{formatDate(post.data.pubDate)}</time>
<time>{formatDate(post.data.date)}</time>
{
post.data.categories &&
post.data.categories.map((category) => (
Expand Down
2 changes: 1 addition & 1 deletion src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const posts = defineCollection({
z.object({
title: z.string(),
description: z.string().optional(),
pubDate: z.coerce.date(),
date: z.coerce.date(),
customData: z.string().optional(),
banner: image()
.refine((img) => Math.max(img.width, img.height) <= 4096, {
Expand Down
29 changes: 16 additions & 13 deletions src/pages/archive.astro
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
---
import LayoutDefault from '~/layouts/LayoutDefault.astro'
import ListSection from '~/components/ListSection.astro'
import ListItem from '~/components/ListItem.astro'
import { getPosts, formatDate } from '~/utils'
import LayoutDefault from "~/layouts/LayoutDefault.astro";
import ListSection from "~/components/ListSection.astro";
import ListItem from "~/components/ListItem.astro";
import { getPosts, formatDate } from "~/utils";
const posts = await getPosts()
const posts = await getPosts();
const postByYear = new Map<number, Post[]>()
const postByYear = new Map<number, Post[]>();
posts.forEach((post: Post) => {
const pubDate = post.data.pubDate ?? new Date()
const year = pubDate.getFullYear()
const pubDate = post.data.date ?? new Date();
const year = pubDate.getFullYear();
if (!postByYear.has(year)) {
postByYear.set(year, [])
postByYear.set(year, []);
}
postByYear.get(year)!.push(post)
})
postByYear.get(year)!.push(post);
});
---

<LayoutDefault>
Expand All @@ -24,7 +23,11 @@ posts.forEach((post: Post) => {
Array.from(postByYear.entries()).map(([year, posts]) => (
<ListSection title={year}>
{posts.map((post) => (
<ListItem title={post.data.title} href={`/posts/${post.slug}/`} description={formatDate(post.data.pubDate)} />
<ListItem
title={post.data.title}
href={`/posts/${post.slug}/`}
description={formatDate(post.data.date)}
/>
))}
</ListSection>
))
Expand Down
25 changes: 12 additions & 13 deletions src/pages/atom.xml.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import rss from '@astrojs/rss';
import { getPosts } from '~/utils';
import rss from "@astrojs/rss";
import { getPosts } from "~/utils";
import { THEME_CONFIG } from "~/theme.config";
import type { APIContext } from 'astro';
import sanitizeHtml from 'sanitize-html';
import MarkdownIt from 'markdown-it';
import type { APIContext } from "astro";
import sanitizeHtml from "sanitize-html";
import MarkdownIt from "markdown-it";

const parser = new MarkdownIt();

const { title, desc, website, author } = THEME_CONFIG

const { title, desc, website, author } = THEME_CONFIG;

export async function GET(_context: APIContext) {
const posts = await getPosts()
const allowedTags = sanitizeHtml.defaults.allowedTags.concat(['img'])
const posts = await getPosts();
const allowedTags = sanitizeHtml.defaults.allowedTags.concat(["img"]);
return rss({
title: title,
description: desc,
Expand All @@ -21,17 +20,17 @@ export async function GET(_context: APIContext) {
return {
link: `/posts/${post.slug}/`,
author: author,
content: sanitizeHtml(parser.render(post.body), { allowedTags, }),
content: sanitizeHtml(parser.render(post.body), { allowedTags }),
title: post.data.title,
pubDate: post.data.pubDate,
pubDate: post.data.date,
description: post.data.description,
customData: post.data.customData,
categories: post.data.categories,
commentsUrl: post.data.commentsUrl,
source: post.data.source,
enclosure: post.data.enclosure,
}
};
}),
stylesheet: '/pretty-feed-v3.xsl',
stylesheet: "/pretty-feed-v3.xsl",
});
}
2 changes: 1 addition & 1 deletion src/pages/categories/[...category].astro
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const { name } = Astro.params;
<ListItem
title={post.data.title}
href={`/posts/${post.slug}/`}
description={formatDate(post.data.pubDate)}
description={formatDate(post.data.date)}
/>
))
}
Expand Down
65 changes: 34 additions & 31 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
import { getCollection } from 'astro:content'
import sanitizeHtml from 'sanitize-html'
import MarkdownIt from 'markdown-it'
import { getCollection } from "astro:content";
import sanitizeHtml from "sanitize-html";
import MarkdownIt from "markdown-it";

export async function getCategories() {
const posts = await getPosts()
const posts = await getPosts();

const categories = new Map<string, Post[]>()
const categories = new Map<string, Post[]>();

posts.forEach((post) => {
if (post.data.categories) {
post.data.categories.forEach((c) => {
const posts = categories.get(c) || []
posts.push(post)
categories.set(c, posts)
})
const posts = categories.get(c) || [];
posts.push(post);
categories.set(c, posts);
});
}
})
});

return categories
return categories;
}

export async function getPosts() {
const posts = await getCollection('posts')
const posts = await getCollection("posts");
posts.sort((a, b) => {
const aDate = a.data.pubDate || new Date()
const bDate = b.data.pubDate || new Date()
return bDate.getTime() - aDate.getTime()
})
return posts
const aDate = a.data.date || new Date();
const bDate = b.data.date || new Date();
return bDate.getTime() - aDate.getTime();
});
return posts;
}

const parser = new MarkdownIt()
const parser = new MarkdownIt();

export function getPostDescription(post: Post) {
if (post.data.description) {
return post.data.description
return post.data.description;
}

const html = parser.render(post.body)
const sanitized = sanitizeHtml(html, { allowedTags: [] })
return sanitized.slice(0, 400)
const html = parser.render(post.body);
const sanitized = sanitizeHtml(html, { allowedTags: [] });
return sanitized.slice(0, 400);
}

export function formatDate(date?: Date) {
if(!date) return '--'
const year = date.getFullYear().toString().padStart(4, '0')
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
if (!date) return "--";
const year = date.getFullYear().toString().padStart(4, "0");
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");

return `${year}-${month}-${day}`
return `${year}-${month}-${day}`;
}

export function getPathFromCategory(category: string, category_map: {name: string, path: string}[]) {
const mappingPath = category_map.find(l => l.name === category)
return mappingPath ? mappingPath.path : category
}
export function getPathFromCategory(
category: string,
category_map: { name: string; path: string }[]
) {
const mappingPath = category_map.find((l) => l.name === category);
return mappingPath ? mappingPath.path : category;
}

0 comments on commit 664a0d3

Please sign in to comment.