From 5632b9bcf830ead69e01ca2c9ef18b0edacad829 Mon Sep 17 00:00:00 2001 From: Sat Naing Date: Sun, 11 Sep 2022 14:08:04 +0630 Subject: [PATCH] Create getUniqueTags function --- src/utils/getUniqueTags.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/utils/getUniqueTags.ts diff --git a/src/utils/getUniqueTags.ts b/src/utils/getUniqueTags.ts new file mode 100644 index 0000000..b83bb13 --- /dev/null +++ b/src/utils/getUniqueTags.ts @@ -0,0 +1,18 @@ +import slugify from "./slugify"; +import type { MarkdownInstance } from "astro"; +import type { Frontmatter } from "./types"; + +const getUniqueTags = (posts: MarkdownInstance[]) => { + let tags: string[] = []; + posts.forEach((post) => { + tags = [...tags, ...post.frontmatter.tags] + .map((tag) => slugify(tag)) + .filter( + (value: string, index: number, self: string[]) => + self.indexOf(value) === index + ); + }); + return tags; +}; + +export default getUniqueTags;