fix: minor fixes

This commit is contained in:
saicaca 2024-01-21 20:19:34 +08:00
parent 3fcb566a54
commit 23533e9ac9
8 changed files with 45 additions and 36 deletions

View File

@ -25,7 +25,7 @@ Fuwari (not the final name maybe) is a static blog template built with [Astro](h
1. [Generate a new repository](https://github.com/saicaca/fuwari/generate) from this template. 1. [Generate a new repository](https://github.com/saicaca/fuwari/generate) from this template.
2. Edit the config file `src/config.ts` to customize your blog. 2. Edit the config file `src/config.ts` to customize your blog.
3. Run `pnpm run new-post -- <filename>` to create a new post and edit it in `src/content/posts/`. 3. Run `npm run new-post -- <filename>` or `pnpm run new-post <filename>` to create a new post and edit it in `src/content/posts/`.
4. Deploy your blog to Vercel, Netlify, GitHub Pages, etc. following [the guides](https://docs.astro.build/en/guides/deploy/). 4. Deploy your blog to Vercel, Netlify, GitHub Pages, etc. following [the guides](https://docs.astro.build/en/guides/deploy/).
## ⚙️ Frontmatter of Posts ## ⚙️ Frontmatter of Posts
@ -38,6 +38,7 @@ description: This is the first post of my new Astro blog.
image: /images/cover.jpg image: /images/cover.jpg
tags: [Foo, Bar] tags: [Foo, Bar]
category: Front-end category: Front-end
draft: false
--- ---
``` ```
@ -46,11 +47,11 @@ category: Front-end
All commands are run from the root of the project, from a terminal: All commands are run from the root of the project, from a terminal:
| Command | Action | | Command | Action |
|:---------------------------------|:-------------------------------------------------| |:------------------------------------------------------------------|:-------------------------------------------------|
| `pnpm install` | Installs dependencies | | `npm install` | Installs dependencies |
| `pnpm run dev` | Starts local dev server at `localhost:4321` | | `npm run dev` | Starts local dev server at `localhost:4321` |
| `pnpm run build` | Build your production site to `./dist/` | | `npm run build` | Build your production site to `./dist/` |
| `pnpm run preview` | Preview your build locally, before deploying | | `npm run preview` | Preview your build locally, before deploying |
| `pnpm run astro ...` | Run CLI commands like `astro add`, `astro check` | | `npm run astro ...` | Run CLI commands like `astro add`, `astro check` |
| `pnpm run astro -- --help` | Get help using the Astro CLI | | `npm run astro -- --help` | Get help using the Astro CLI |
| `pnpm run new-post -- <filename>` | Create a new post | | `npm run new-post -- <filename>`<br/>`pnpm run new-post <filename>` | Create a new post |

View File

@ -37,10 +37,11 @@ if (fs.existsSync(fullPath)) {
const content = `--- const content = `---
title: ${args[0]} title: ${args[0]}
published: ${getDate()} published: ${getDate()}
description: description: ''
image: image: ''
tags: [] tags: []
category: category: ''
draft: false
--- ---
` `

View File

@ -1,4 +1,5 @@
--- ---
import {UNCATEGORIZED} from "@constants/constants";
interface Props { interface Props {
keyword: string; keyword: string;
tags: string[]; tags: string[];
@ -9,6 +10,8 @@ const { keyword, tags, categories} = Astro.props;
import Button from "./control/Button.astro"; import Button from "./control/Button.astro";
import {getSortedPosts} from "../utils/content-utils"; import {getSortedPosts} from "../utils/content-utils";
import {getPostUrlBySlug} from "../utils/url-utils"; import {getPostUrlBySlug} from "../utils/url-utils";
import {i18n} from "../i18n/translation";
import I18nKey from "../i18n/i18nKey";
let posts = await getSortedPosts() let posts = await getSortedPosts()
@ -20,7 +23,8 @@ if (Array.isArray(tags) && tags.length > 0) {
if (Array.isArray(categories) && categories.length > 0) { if (Array.isArray(categories) && categories.length > 0) {
posts = posts.filter(post => posts = posts.filter(post =>
post.data.category && categories.includes(post.data.category) (post.data.category && categories.includes(post.data.category)) ||
(!post.data.category && categories.includes(UNCATEGORIZED))
); );
} }
@ -66,7 +70,7 @@ function formatTag(tag: string[]) {
<div class="w-[10%]"> <div class="w-[10%]">
<div class="h-3 w-3 bg-none rounded-full outline outline-[var(--primary)] mx-auto -outline-offset-[2px] z-50 outline-3"></div> <div class="h-3 w-3 bg-none rounded-full outline outline-[var(--primary)] mx-auto -outline-offset-[2px] z-50 outline-3"></div>
</div> </div>
<div class="w-[80%] transition text-left text-black/50 dark:text-white/50">{group.posts.length} Articles</div> <div class="w-[80%] transition text-left text-black/50 dark:text-white/50">{group.posts.length} {i18n(I18nKey.postsCount)}</div>
</div> </div>
{group.posts.map(post => ( {group.posts.map(post => (
<a href={getPostUrlBySlug(post.slug)} aria-label={post.data.title} class="group"> <a href={getPostUrlBySlug(post.slug)} aria-label={post.data.title} class="group">

View File

@ -4,7 +4,7 @@ const postsCollection = defineCollection({
schema: z.object({ schema: z.object({
title: z.string(), title: z.string(),
published: z.date(), published: z.date(),
draft: z.boolean(), draft: z.boolean().optional(),
description: z.string().optional(), description: z.string().optional(),
image: z.string().optional(), image: z.string().optional(),
tags: z.array(z.string()).optional(), tags: z.array(z.string()).optional(),

View File

@ -20,8 +20,8 @@ export const ja: Translation = {
[Key.wordsCount]: '文字', [Key.wordsCount]: '文字',
[Key.minuteCount]: '分', [Key.minuteCount]: '分',
[Key.minutesCount]: '分', [Key.minutesCount]: '分',
[Key.postCount]: 'post', [Key.postCount]: '件の投稿',
[Key.postsCount]: 'posts', [Key.postsCount]: '件の投稿',
[Key.primaryColor]: '原色', [Key.primaryColor]: '原色',

View File

@ -0,0 +1,11 @@
---
import MainGridLayout from "@layouts/MainGridLayout.astro";
import ArchivePanel from "@components/ArchivePanel.astro";
import {i18n} from "@i18n/translation";
import I18nKey from "@i18n/i18nKey";
import {UNCATEGORIZED} from "@constants/constants";
---
<MainGridLayout title={i18n(I18nKey.archive)}>
<ArchivePanel categories={[UNCATEGORIZED]}></ArchivePanel>
</MainGridLayout>

View File

@ -7,22 +7,16 @@ import {getPostUrlBySlug} from "@utils/url-utils";
export async function getStaticPaths({ paginate }) { export async function getStaticPaths({ paginate }) {
const allBlogPosts = await getSortedPosts(); const allBlogPosts = await getSortedPosts();
return paginate(allBlogPosts, { pageSize: 6 }); return paginate(allBlogPosts, { pageSize: 8 });
} }
const {page} = Astro.props; const {page} = Astro.props;
--- ---
<!-- 显示当前页面。也可以使用 Astro.params.page -->
<MainGridLayout> <MainGridLayout>
<div class="flex flex-col gap-4 mb-4"> <div class="flex flex-col gap-4 mb-4">
{page.data.map((entry: { data: { draft: boolean; title: string; tags: string[]; category: string; published: Date; image: string; description: string; }; slug: string; }) => { {page.data.map((entry: { data: { draft: boolean; title: string; tags: string[]; category: string; published: Date; image: string; description: string; }; slug: string; }) => {
// ここで draft が true の場合は何もレンダリングしない
if (import.meta.env.PROD && entry.data.draft) {
return null;
}
return ( return (
<TitleCard <TitleCard
entry={entry} entry={entry}
@ -41,11 +35,3 @@ const {page} = Astro.props;
</div> </div>
<Pagination class="mx-auto" page={page}></Pagination> <Pagination class="mx-auto" page={page}></Pagination>
</MainGridLayout> </MainGridLayout>
<script>
if (import.meta.env.DEV) {
console.log("開発環境");
} else {
console.log("本番環境");
}
</script>

View File

@ -1,7 +1,9 @@
import { getCollection } from 'astro:content' import { getCollection } from 'astro:content'
export async function getSortedPosts() { export async function getSortedPosts() {
const allBlogPosts = await getCollection('posts') const allBlogPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
})
const sorted = allBlogPosts.sort((a, b) => { const sorted = allBlogPosts.sort((a, b) => {
const dateA = new Date(a.data.published) const dateA = new Date(a.data.published)
const dateB = new Date(b.data.published) const dateB = new Date(b.data.published)
@ -26,7 +28,9 @@ export type Tag = {
} }
export async function getTagList(): Promise<Tag[]> { export async function getTagList(): Promise<Tag[]> {
const allBlogPosts = await getCollection('posts') const allBlogPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
})
const countMap: { [key: string]: number } = {} const countMap: { [key: string]: number } = {}
allBlogPosts.map(post => { allBlogPosts.map(post => {
@ -50,7 +54,9 @@ export type Category = {
} }
export async function getCategoryList(): Promise<Category[]> { export async function getCategoryList(): Promise<Category[]> {
const allBlogPosts = await getCollection('posts') const allBlogPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
})
const count: { [key: string]: number } = {} const count: { [key: string]: number } = {}
allBlogPosts.map(post => { allBlogPosts.map(post => {
if (!post.data.category) { if (!post.data.category) {