Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the blog drafts leaded when building #5

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/components/PostViewTitle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {formatDate} from "../utils/formatDate";
import {dealLabel} from "../utils/dealLabel";
import {t} from '../i18n/utils'
const {title, date, slug, category, tags, sticky = false} = Astro.props
const {title, date, slug, category, tags, sticky = false, draft = false} = Astro.props
---

<div class="text-skin-base">
Expand All @@ -24,6 +24,16 @@ const {title, date, slug, category, tags, sticky = false} = Astro.props
)
}

<!-- draft -->
{
draft &&
<>
<div class="divider-vertical" />
<i class="ri-git-pr-draft-line" />
<div>draft</div>
</>
}

<!-- category -->
{
category && dealLabel(category).map((categoryName) => (
Expand Down
4 changes: 3 additions & 1 deletion src/pages/blog/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import Donate from '../../components/Donate.astro'
import {donate} from "../../consts";

export async function getStaticPaths() {
const blogEntries = await getCollection('blog');
const blogEntries = (await getCollection('blog')).filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
});
return blogEntries.map(entry => ({
params: {slug: entry.slug}, props: {entry,},
}));
Expand Down
4 changes: 3 additions & 1 deletion src/pages/rss.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {site} from "../consts";
import {getCollection} from "astro:content";

export async function GET(context) {
const blog = await getCollection('blog');
const blog = (await getCollection('blog')).filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
});
return rss({
title: site.title,
description: site.description,
Expand Down
10 changes: 5 additions & 5 deletions src/utils/getCollectionByName.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {getCollection} from "astro:content";

export const getCollectionByName = async (name: string) => {
let posts = await getCollection(name)
let posts = await getCollection(name);
if (posts && posts.length > 0 ) {
return posts.filter(post => !post.data.draft).map(post => {
return post
})
}else {
return posts.filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
});
} else {
return []
}
}
14 changes: 8 additions & 6 deletions src/utils/getCountByCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import _ from 'lodash'
import { dealLabel } from './dealLabel'

const getCountByCategory = (posts) => {
let category: string[] = [];
const filteredPosts = posts.filter(({ data }) => !data.draft);
filteredPosts.forEach(post => {
category = _.compact([...category, ..._.flattenDeep(dealLabel(post.data.category))])
});
return _.countBy(category);
let category: string[] = [];
const filteredPosts = posts.filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
});
filteredPosts.forEach(post => {
category = _.compact([...category, ..._.flattenDeep(dealLabel(post.data.category))])
});
return _.countBy(category);
};

export default getCountByCategory;
14 changes: 8 additions & 6 deletions src/utils/getCountByTagName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import _ from 'lodash'
import { dealLabel } from './dealLabel'

const getCountByTagName = (posts) => {
let tags: string[] = [];
const filteredPosts = posts.filter(({ data }) => !data.draft);
filteredPosts.forEach(post => {
tags = _.compact([...tags, ..._.flattenDeep(dealLabel(post.data.tags))])
});
return _.countBy(tags);
let tags: string[] = [];
const filteredPosts = posts.filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
});
filteredPosts.forEach(post => {
tags = _.compact([...tags, ..._.flattenDeep(dealLabel(post.data.tags))])
});
return _.countBy(tags);
};

export default getCountByTagName;
2 changes: 1 addition & 1 deletion src/utils/getPostsByCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import _ from 'lodash'
import { dealLabel } from './dealLabel';

const getPostsByCategory = (posts, category: string) =>
posts.filter(post => _.flattenDeep(dealLabel(post.data.category)).includes(category))
posts.filter(post => _.flattenDeep(dealLabel(post.data.category)).includes(category))

export default getPostsByCategory;
2 changes: 1 addition & 1 deletion src/utils/getPostsByTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import _ from 'lodash'
import { dealLabel } from './dealLabel';

const getPostsByTag = (posts, tag: string) =>
posts.filter(post => _.flattenDeep(dealLabel(post.data.tags)).includes(tag))
posts.filter(post => _.flattenDeep(dealLabel(post.data.tags)).includes(tag))
export default getPostsByTag;
4 changes: 3 additions & 1 deletion src/utils/getUniqeCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { dealLabel } from './dealLabel';

const getUniqueCategory = (posts) => {
let category: string[] = [];
const filteredPosts = posts.filter(({ data }) => !data.draft);
const filteredPosts = posts.filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
});
filteredPosts.forEach(post => {
category = [...category, ..._.flattenDeep(dealLabel(post.data.category))]
.filter(
Expand Down
4 changes: 3 additions & 1 deletion src/utils/getUniqueTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { dealLabel } from './dealLabel';

const getUniqueTags = (posts) => {
let tags: string[] = [];
const filteredPosts = posts.filter(({ data }) => !data.draft);
const filteredPosts = posts.filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
});
filteredPosts.forEach(post => {
tags = [...tags, ...dealLabel(post.data.tags)]
.filter(
Expand Down
4 changes: 3 additions & 1 deletion src/utils/sortPostsByDate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import dayjs from 'dayjs'
export const sortPostsByDate = (posts) =>
posts
.filter(({ data }) => !data.draft)
.filter(({data}) => {
return import.meta.env.PROD ? !data.draft : true
})
.sort((a, b) => dayjs(b.data.date).unix() - dayjs(a.data.date).unix());