slugify tag paths

This commit is contained in:
Shadowfacts 2021-04-12 18:31:03 -04:00
parent 795afa5c1c
commit 272a4b1326
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 7 additions and 3 deletions

View File

@ -11,6 +11,8 @@ import layout from "../layout";
async function generatePostFromFile(postPath: string, additionalMetadata: object = {}): Promise<Page> {
const page = await metadata.get(postPath);
(<PostMetadata>page.metadata).tagSlugs = (<PostMetadata>page.metadata).tags.map((tag) => slugify(tag));
for (const k in additionalMetadata) {
if (additionalMetadata.hasOwnProperty(k)) page.metadata[k] = additionalMetadata[k];
}

View File

@ -1,5 +1,6 @@
import { Page, PostMetadata } from "../metadata";
import generatePaginated from "./paginated";
import slugify from "@sindresorhus/slugify";
export default async function(posts: Page[]): Promise<Map<string, Page[]>> {
const taggedPosts = new Map<string, Page[]>();
@ -15,7 +16,7 @@ export default async function(posts: Page[]): Promise<Map<string, Page[]>> {
}
taggedPosts.forEach((tagPosts, tag) => {
generatePaginated(tagPosts, `/${tag}/`, "site/tag.html.ejs", {
generatePaginated(tagPosts, `/${slugify(tag)}/`, "site/tag.html.ejs", {
tag
}, {
title: `${tag} posts`

View File

@ -17,6 +17,7 @@ export interface PostMetadata extends Metadata {
title: string;
slug: string;
tags: string[];
tagSlugs: string[];
date: string | Date;
readingTime?: number;
wordCount?: number;

View File

@ -11,7 +11,7 @@
<% if (metadata.tags) { %>
in
<% for (let i = 0; i < metadata.tags.length; i++) { %>
<span itemprop="articleSection"><a href="/<%= metadata.tags[i] %>/" rel="tag"><%= metadata.tags[i] %></a></span><%= i < metadata.tags.length - 1 ? ", " : "" %>
<span itemprop="articleSection"><a href="/<%= metadata.tagSlugs[i] %>/" rel="tag"><%= metadata.tags[i] %></a></span><%= i < metadata.tags.length - 1 ? ", " : "" %>
<% } %>
<% } else if (metadata.series) { %>
in

View File

@ -1,6 +1,6 @@
```
metadata.title = "Let's Build a Programming Language: Part 0"
metadata.tags = ["let's build a programming language", "rust"]
metadata.tags = ["build a programming language", "rust"]
metadata.date = "2021-04-12 17:27:42 -0400"
metadata.shortDesc = ""
metadata.slug = "lets-build-a-programming-language"