Skip to content

Commit

Permalink
feat: add missing lib file
Browse files Browse the repository at this point in the history
  • Loading branch information
bluenex committed Jan 29, 2024
1 parent 4c23a14 commit f86c5a5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { readFileSync } from "fs";
import { glob } from "glob";
import matter from "gray-matter";
import path from "path";

const postsDirectory = path.join(process.cwd(), "posts");

export function getAllTagsAndYears() {
const fileNames = glob.sync(`${postsDirectory}/**/*.md`);
const yearsSet = new Set<string>();
const tagsSet = new Set<string>();

fileNames.forEach((fileName) => {
const [id] = fileName.split("/").slice(-1);
const [year] = id.split("-");

// add year
yearsSet.add(year);

const filePath = path.join(postsDirectory, id);
const fileContents = readFileSync(filePath, "utf8");

const matterResult = matter(fileContents);

matterResult.data.tags.forEach((tag: string) => {
// add tag
tagsSet.add(tag);
});
});

return {
years: Array.from(yearsSet).sort((a, b) => Number(b) - Number(a)),
tags: Array.from(tagsSet).sort(),
};
}

0 comments on commit f86c5a5

Please sign in to comment.