diff --git a/src/components/ArchivePanel.astro b/src/components/ArchivePanel.astro index 7fb180b..9a59a9d 100644 --- a/src/components/ArchivePanel.astro +++ b/src/components/ArchivePanel.astro @@ -20,7 +20,7 @@ if (Array.isArray(tags) && tags.length > 0) { if (Array.isArray(categories) && categories.length > 0) { posts = posts.filter(post => - Array.isArray(post.data.categories) && post.data.categories.some(category => categories.includes(category)) + post.data.category && categories.includes(post.data.category) ); } diff --git a/src/components/PostMetadata.astro b/src/components/PostMetadata.astro index 2db87ef..d09b51b 100644 --- a/src/components/PostMetadata.astro +++ b/src/components/PostMetadata.astro @@ -8,10 +8,9 @@ interface Props { class: string; published: Date; tags: string[]; - categories: string[]; category: string; } -const {published, tags, categories} = Astro.props; +const {published, tags, category} = Astro.props; const className = Astro.props.class; --- @@ -25,23 +24,21 @@ const className = Astro.props.class; {formatDateToYYYYMMDD(published)} - +
- {(categories && categories.length > 0) && categories.map(category => )} - {!(categories && categories.length > 0) &&
{i18n(I18nKey.uncategorized)}
} +
+ } + {!category &&
{i18n(I18nKey.uncategorized)}
}
diff --git a/src/components/TitleCardNew.astro b/src/components/TitleCardNew.astro index 2d317d6..75550b7 100644 --- a/src/components/TitleCardNew.astro +++ b/src/components/TitleCardNew.astro @@ -7,13 +7,12 @@ interface Props { url: string; published: Date; tags: string[]; - categories: string[]; category: string; image: string; description: string; words: number; } -const { entry, title, url, published, tags, categories, category, image, description, words } = Astro.props; +const { entry, title, url, published, tags, category, image, description, words } = Astro.props; const className = Astro.props.class; // console.log(Astro.props); import ImageBox from "./misc/ImageBox.astro"; @@ -29,11 +28,6 @@ const coverWidth = "28%"; const { remarkPluginFrontmatter } = await entry.render(); -let cate = categories; -if (category) { - cate = [category]; -} - ---
@@ -49,7 +43,7 @@ if (category) { - +
{ description } diff --git a/src/components/widget/Categories.astro b/src/components/widget/Categories.astro index b707cd7..98d2eb8 100644 --- a/src/components/widget/Categories.astro +++ b/src/components/widget/Categories.astro @@ -3,31 +3,25 @@ import WidgetLayout from "./WidgetLayout.astro"; import {i18n} from "../../i18n/translation"; import I18nKey from "../../i18n/i18nKey"; -import {CategoryMap, getCategoryMap} from "../../utils/content-utils"; -import CategoriesLink from "./CategoriesLink.astro"; +import {Category, getCategoryList} from "../../utils/content-utils"; +import {getCategoryUrl} from "../../utils/url-utils"; +import ButtonLink from "../control/ButtonLink.astro"; -const categories = await getCategoryMap(); +const categories = await getCategoryList(); const COLLAPSED_HEIGHT = "120px"; const COLLAPSE_THRESHOLD = 5; -function count(categoryMap: CategoryMap): number { - let res = 0; - for (const key in categoryMap) { - res++; - res += count(categoryMap[key].children); - } - return res; -} - -const isCollapsed = count(categories) >= COLLAPSE_THRESHOLD; +const isCollapsed = categories.length >= COLLAPSE_THRESHOLD; interface Props { - categories: CategoryMap; + categories: Category[]; } --- - + {categories.map((c) => + {c.name} + )} \ No newline at end of file diff --git a/src/components/widget/CategoriesLink.astro b/src/components/widget/CategoriesLink.astro deleted file mode 100644 index 7025c21..0000000 --- a/src/components/widget/CategoriesLink.astro +++ /dev/null @@ -1,21 +0,0 @@ ---- - -import {CategoryMap} from "../../utils/content-utils"; -import {getCategoryUrl} from "../../utils/url-utils"; -import ButtonLink from "../control/ButtonLink.astro"; - -interface Props { - categories: CategoryMap; -} - -const {categories} = Astro.props; - ---- -
- {Object.entries(categories).map(([key, value]) => - {value.name} -
- {Object.keys(value.children).length > 0 && } -
- )} -
diff --git a/src/pages/archive/category/[category].astro b/src/pages/archive/category/[category].astro index dd4071b..0312cdc 100644 --- a/src/pages/archive/category/[category].astro +++ b/src/pages/archive/category/[category].astro @@ -1,6 +1,5 @@ --- - -import {getSortedPosts} from "../../../utils/content-utils"; +import {getCategoryList, getSortedPosts} from "../../../utils/content-utils"; import MainGridLayout from "../../../layouts/MainGridLayout.astro"; import ArchivePanel from "../../../components/ArchivePanel.astro"; import {i18n} from "../../../i18n/translation"; @@ -8,21 +7,11 @@ import I18nKey from "../../../i18n/i18nKey"; export async function getStaticPaths() { - let posts = await getSortedPosts() - - const allCategories = posts.reduce((acc, post) => { - if (!Array.isArray(post.data.categories)) - return acc; - post.data.categories.forEach(category => acc.add(category)); - return acc; - }, new Set()); - - const allCategoriesArray = Array.from(allCategories); - - return allCategoriesArray.map(category => { + const categories = await getCategoryList(); + return categories.map(category => { return { params: { - category: category + category: category.name } } }); diff --git a/src/pages/page/[page].astro b/src/pages/page/[page].astro index a925020..dd2f181 100644 --- a/src/pages/page/[page].astro +++ b/src/pages/page/[page].astro @@ -22,7 +22,6 @@ const {page} = Astro.props; entry={entry} title={entry.data.title} tags={entry.data.tags} - categories={entry.data.categories} category={entry.data.category} published={entry.data.published} url={getPostUrlBySlug(entry.slug)} diff --git a/src/pages/posts/[slug].astro b/src/pages/posts/[slug].astro index 3df8e84..043c3d9 100644 --- a/src/pages/posts/[slug].astro +++ b/src/pages/posts/[slug].astro @@ -64,7 +64,7 @@ const { remarkPluginFrontmatter } = await entry.render(); class="mb-5" published={entry.data.published} tags={entry.data.tags} - categories={entry.data.categories} + category={entry.data.category} > diff --git a/src/utils/content-utils.ts b/src/utils/content-utils.ts index 3485c67..1f30870 100644 --- a/src/utils/content-utils.ts +++ b/src/utils/content-utils.ts @@ -20,7 +20,12 @@ export async function getSortedPosts() { return sorted; } -export async function getTagList(): Promise<{ name: string; count: number }[]> { +export type Tag = { + name: string; + count: number; +} + +export async function getTagList(): Promise { const allBlogPosts = await getCollection("posts"); const countMap: { [key: string]: number } = {}; @@ -32,44 +37,38 @@ export async function getTagList(): Promise<{ name: string; count: number }[]> { }); // sort tags - const keys: string[] = Object.keys(countMap).sort(function (a, b) { - var textA = a.toLowerCase(); - var textB = b.toLowerCase(); - if (textA < textB) { - return -1; - } - if (textA > textB) { - return 1; - } - return 0; + const keys: string[] = Object.keys(countMap).sort((a, b) => { + return a.toLowerCase().localeCompare(b.toLowerCase()); }); return keys.map((key) => ({name: key, count: countMap[key]})); } -type Category = { +export type Category = { name: string; count: number; - children: CategoryMap; } -export type CategoryMap = { [key: string]: Category }; - -export async function getCategoryMap(): Promise { +export async function getCategoryList(): Promise { const allBlogPosts = await getCollection("posts"); - let root: CategoryMap = {}; + let count : {[key: string]: number} = {}; allBlogPosts.map((post) => { - let current = root; - if (post.data.category) { - post.data.categories = [post.data.category]; - } - if (!post.data.categories) + if (!post.data.category) { return; - for (const c of post.data.categories) { - if (!current[c]) current[c] = {name: c, count: 0, children: {}}; - current[c].count++; - current = current[c].children; } + if (!count[post.data.category]) { + count[post.data.category] = 0; + } + count[post.data.category]++; }); - return root; + + let lst = Object.keys(count).sort((a, b) => { + return a.toLowerCase().localeCompare(b.toLowerCase()); + }); + + let ret : Category[] = []; + for (const c of lst) { + ret.push({name: c, count: count[c]}); + } + return ret; } \ No newline at end of file