Skip to content

Commit

Permalink
flatMap polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
karesztrk committed Apr 12, 2021
1 parent d074c53 commit a148e85
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
35 changes: 17 additions & 18 deletions src/lib/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ export type TagContent = {

export function listTags(limit?: number): TagContent[] {
const postContent = fetchPostContent();
console.log('Received tags: ');
console.log(postContent.map((content) => content.tags));
const allTags =
postContent && Array.isArray(postContent) && postContent.map
? Array.from(
new Set<string>(
postContent
.map((content) => content.tags)
.flat()
.filter((t) => t)
.map((t) => t.toLowerCase()),
),
)
: [];
const tags = allTags.map((tag) => ({
if (!postContent || !Array.isArray(postContent)) {
return [];
}

const tagSet = new Set<string>(
// flatMap() polyfill
[].concat(
...postContent.map((content) =>
content.tags.filter((t) => t).map((t) => t.toLowerCase()),
),
),
);
const tags = Array.from(tagSet).map((tag) => ({
name: tag,
slug: tag,
}));
if (!limit) {
return tags;

if (limit) {
return tags.slice(0, limit);
}
return tags.slice(0, limit);
return tags;
}
7 changes: 0 additions & 7 deletions src/pages/posts/tags/[[...slug]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
};

export const getStaticPaths: GetStaticPaths = async () => {
const tags = listTags();
if (!tags || !Array.isArray(tags) || !tags.flatMap) {
return {
paths: [],
fallback: false,
};
}
const paths = listTags().flatMap((tag) => {
const pages = Math.ceil(countPosts(tag.slug) / config.posts_per_page);
return Array.from(Array(pages).keys()).map((page) =>
Expand Down

0 comments on commit a148e85

Please sign in to comment.