mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
feat: replace slugified title with unslugified tag name (#198)
Update slugified titles to be unslugified in the tag page. This is for better SEO and better a11y. Url and tag elelemnts will still be slugified. Closes: #179
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
---
|
---
|
||||||
export interface Props {
|
export interface Props {
|
||||||
name: string;
|
tag: string;
|
||||||
size?: "sm" | "lg";
|
size?: "sm" | "lg";
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, size = "sm" } = Astro.props;
|
const { tag, size = "sm" } = Astro.props;
|
||||||
---
|
---
|
||||||
|
|
||||||
<li
|
<li
|
||||||
@@ -13,8 +13,8 @@ const { name, size = "sm" } = Astro.props;
|
|||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<a
|
<a
|
||||||
href={`/tags/${name.toLowerCase()}`}
|
href={`/tags/${tag}`}
|
||||||
transition:name={name.toLowerCase()}
|
transition:name={tag}
|
||||||
class={`${size === "sm" ? "text-sm" : "text-lg"} pr-2 group`}
|
class={`${size === "sm" ? "text-sm" : "text-lg"} pr-2 group`}
|
||||||
>
|
>
|
||||||
<svg
|
<svg
|
||||||
@@ -24,7 +24,7 @@ const { name, size = "sm" } = Astro.props;
|
|||||||
d="M16.018 3.815 15.232 8h-4.966l.716-3.815-1.964-.37L8.232 8H4v2h3.857l-.751 4H3v2h3.731l-.714 3.805 1.965.369L8.766 16h4.966l-.714 3.805 1.965.369.783-4.174H20v-2h-3.859l.751-4H21V8h-3.733l.716-3.815-1.965-.37zM14.106 14H9.141l.751-4h4.966l-.752 4z"
|
d="M16.018 3.815 15.232 8h-4.966l.716-3.815-1.964-.37L8.232 8H4v2h3.857l-.751 4H3v2h3.731l-.714 3.805 1.965.369L8.766 16h4.966l-.714 3.805 1.965.369.783-4.174H20v-2h-3.859l.751-4H21V8h-3.733l.716-3.815-1.965-.37zM14.106 14H9.141l.751-4h4.966l-.752 4z"
|
||||||
></path>
|
></path>
|
||||||
</svg>
|
</svg>
|
||||||
<span>{name.toLowerCase()}</span>
|
<span>{tag}</span>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ const layoutProps = {
|
|||||||
</article>
|
</article>
|
||||||
|
|
||||||
<ul class="my-8">
|
<ul class="my-8">
|
||||||
{tags.map(tag => <Tag name={slugifyStr(tag)} />)}
|
{tags.map(tag => <Tag tag={slugifyStr(tag)} />)}
|
||||||
</ul>
|
</ul>
|
||||||
<div class="flex justify-end">
|
<div class="flex justify-end">
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import getSortedPosts from "@utils/getSortedPosts";
|
|||||||
export interface Props {
|
export interface Props {
|
||||||
post: CollectionEntry<"blog">;
|
post: CollectionEntry<"blog">;
|
||||||
tag: string;
|
tag: string;
|
||||||
|
tagName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
@@ -20,15 +21,15 @@ export async function getStaticPaths() {
|
|||||||
|
|
||||||
const tags = getUniqueTags(posts);
|
const tags = getUniqueTags(posts);
|
||||||
|
|
||||||
return tags.map(tag => {
|
return tags.map(({ tag, tagName }) => {
|
||||||
return {
|
return {
|
||||||
params: { tag },
|
params: { tag },
|
||||||
props: { tag },
|
props: { tag, tagName },
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const { tag } = Astro.props;
|
const { tag, tagName } = Astro.props;
|
||||||
|
|
||||||
const posts = await getCollection("blog", ({ data }) => !data.draft);
|
const posts = await getCollection("blog", ({ data }) => !data.draft);
|
||||||
|
|
||||||
@@ -37,12 +38,12 @@ const tagPosts = getPostsByTag(posts, tag);
|
|||||||
const sortTagsPost = getSortedPosts(tagPosts);
|
const sortTagsPost = getSortedPosts(tagPosts);
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title={`Tag:${tag} | ${SITE.title}`}>
|
<Layout title={`Tag: ${tagName} | ${SITE.title}`}>
|
||||||
<Header activeNav="tags" />
|
<Header activeNav="tags" />
|
||||||
<Main
|
<Main
|
||||||
pageTitle={[`Tag:`, `${tag}`]}
|
pageTitle={[`Tag:`, `${tagName}`]}
|
||||||
titleTransition={tag}
|
titleTransition={tag}
|
||||||
pageDesc={`All the articles with the tag "${tag}".`}
|
pageDesc={`All the articles with the tag "${tagName}".`}
|
||||||
>
|
>
|
||||||
<h1 slot="title" transition:name={tag}>{`Tag:${tag}`}</h1>
|
<h1 slot="title" transition:name={tag}>{`Tag:${tag}`}</h1>
|
||||||
<ul>
|
<ul>
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ let tags = getUniqueTags(posts);
|
|||||||
<Header activeNav="tags" />
|
<Header activeNav="tags" />
|
||||||
<Main pageTitle="Tags" pageDesc="All the tags used in posts.">
|
<Main pageTitle="Tags" pageDesc="All the tags used in posts.">
|
||||||
<ul>
|
<ul>
|
||||||
{tags.map(tag => <Tag name={tag} size="lg" />)}
|
{tags.map(({ tag }) => <Tag {tag} size="lg" />)}
|
||||||
</ul>
|
</ul>
|
||||||
</Main>
|
</Main>
|
||||||
<Footer />
|
<Footer />
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
import { slugifyStr } from "./slugify";
|
import { slugifyStr } from "./slugify";
|
||||||
import type { CollectionEntry } from "astro:content";
|
import type { CollectionEntry } from "astro:content";
|
||||||
|
|
||||||
|
interface Tag {
|
||||||
|
tag: string;
|
||||||
|
tagName: string;
|
||||||
|
}
|
||||||
|
|
||||||
const getUniqueTags = (posts: CollectionEntry<"blog">[]) => {
|
const getUniqueTags = (posts: CollectionEntry<"blog">[]) => {
|
||||||
const filteredPosts = posts.filter(({ data }) => !data.draft);
|
const filteredPosts = posts.filter(({ data }) => !data.draft);
|
||||||
const tags: string[] = filteredPosts
|
const tags: Tag[] = filteredPosts
|
||||||
.flatMap(post => post.data.tags)
|
.flatMap(post => post.data.tags)
|
||||||
.map(tag => slugifyStr(tag))
|
.map(tag => ({ tag: slugifyStr(tag), tagName: tag }))
|
||||||
.filter(
|
.filter(
|
||||||
(value: string, index: number, self: string[]) =>
|
(value, index, self) =>
|
||||||
self.indexOf(value) === index
|
self.findIndex(tag => tag.tag === value.tag) === index
|
||||||
)
|
)
|
||||||
.sort((tagA: string, tagB: string) => tagA.localeCompare(tagB));
|
.sort((tagA, tagB) => tagA.tag.localeCompare(tagB.tag));
|
||||||
return tags;
|
return tags;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user