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

feat(i18n): support tag index #382

Merged
merged 1 commit into from
Apr 12, 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
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,
},
};
};
Loading