Skip to content

Commit

Permalink
feat(i18n): support tag index
Browse files Browse the repository at this point in the history
  • Loading branch information
la3rence authored and k8s-ci-bot committed Apr 12, 2024
1 parent d3b4989 commit c9d1808
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
23 changes: 23 additions & 0 deletions lib/ssg.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ export const getAllTags = async () => {
return [...new Set(tags)];
};

export const getAllTagsLocale = async () => {
const tags = [];
const allPosts = await getAllPostData();
allPosts?.map(post => {
if (post.tags) {
const tagArray = post.tags.split(",");
tagArray.map(each =>
tags.push({
label: each.trim().toLowerCase(),
locale: post.locale,
}),
);
}
});
// remove same tags with same locales
return tags.filter((tag, index) => {
return (
tags.findIndex(t => t.label === tag.label && t.locale === tag.locale) ===
index
);
});
};

const blogTOC = (toc, isBlog) => {
if (isBlog && config.enableToC && toc.children[0]?.children[0]) {
return {
Expand Down
24 changes: 12 additions & 12 deletions pages/tag/[tag].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPostsByTag, getAllTags } from "../../lib/ssg";
import { getPostsByTag, getAllTags, getAllTagsLocale } from "../../lib/ssg";
import Layout from "../../components/layout";
import Tag from "../../components/tag";
import { useRouter } from "next/router";
Expand All @@ -12,14 +12,16 @@ export default function TagPage(props) {
Tagged with <code>{query.tag}</code>
</h2>
<div className="mt-8">
{props.tags?.map(tag => (
<Tag
tag={tag}
key={tag}
highlight={query.tag.toLowerCase().trim() === tag}
locale={locale}
/>
))}
{props.tags
?.filter(item => item.locale === locale)
?.map(item => (
<Tag
tag={item.label}
key={`${item.label}_${item.locale}`}
highlight={query.tag.toLowerCase().trim() === item.label}
locale={item.locale}
/>
))}
</div>
<div className="mt-8 mx-4">
{props.data
Expand Down Expand Up @@ -51,7 +53,7 @@ export default function TagPage(props) {
export const getStaticProps = async context => {
const { tag } = context.params;
const postData = await getPostsByTag(tag);
const tags = await getAllTags();
const tags = await getAllTagsLocale();
return {
props: {
data: postData,
Expand All @@ -63,8 +65,6 @@ export const getStaticProps = async context => {
export const getStaticPaths = async context => {
const tags = await getAllTags();
const paths = [];
// todo: filter out tags by locale matching
// currenly show all tags even if no posts
context.locales.forEach(locale => {
const pathsWithLocale = tags.map(tag => {
return {
Expand Down
14 changes: 8 additions & 6 deletions pages/tag/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAllTags } from "../../lib/ssg";
import { getAllTagsLocale } from "../../lib/ssg";
import Layout from "../../components/layout";
import Tag from "../../components/tag";
import { useRouter } from "next/router";
Expand All @@ -9,19 +9,21 @@ export default function TagIndex(props) {
<Layout title={`Tags`} tags={`Tags`}>
<h2>Tags</h2>
<div className="mt-8">
{props.tags.map(tag => (
<Tag tag={tag} key={tag} locale={locale} />
))}
{props.tags
?.filter(item => item.locale === locale)
?.map(item => (
<Tag tag={item.label} key={item.label} locale={item.locale} />
))}
</div>
</Layout>
);
}

export const getStaticProps = async () => {
const tags = await getAllTags();
const tags = await getAllTagsLocale();
return {
props: {
tags: tags,
tags,
},
};
};

0 comments on commit c9d1808

Please sign in to comment.