feat: remove markdown build helpers
This commit is contained in:
@@ -1,52 +1,36 @@
|
||||
import type { Plugin } from "vite";
|
||||
import { readSync } from "to-vfile";
|
||||
import { matter } from "vfile-matter";
|
||||
import { resolve, join } from "node:path";
|
||||
import { readdirSync, statSync, writeFileSync } from "node:fs";
|
||||
import { exec } from "node:child_process";
|
||||
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import matter from "gray-matter";
|
||||
|
||||
/*
|
||||
* Code from andii.dev
|
||||
*/
|
||||
const processFiles = () => {
|
||||
const outputFile = resolve("src/data/posts.json");
|
||||
const blogDir = resolve("src/routes/blog");
|
||||
const files = readdirSync(blogDir);
|
||||
const blogPosts = files
|
||||
.filter(
|
||||
(file) => statSync(join(blogDir, file)).isFile() && file.endsWith(".mdx"),
|
||||
)
|
||||
const BLOG_DIR = resolve("src/routes/blog");
|
||||
const OUTPUT_FILE = resolve("src/data/posts.json");
|
||||
|
||||
function generatePostsJson() {
|
||||
const posts = readdirSync(BLOG_DIR)
|
||||
.filter((file) => file.endsWith(".mdx"))
|
||||
.map((file) => {
|
||||
const f = readSync(resolve("src/routes/blog", file));
|
||||
matter(f);
|
||||
return {
|
||||
...(f.data.matter as object),
|
||||
slug: file.replace(".mdx", ""),
|
||||
} as { date: string; slug: string };
|
||||
const content = readFileSync(join(BLOG_DIR, file), "utf-8");
|
||||
const { data } = matter(content);
|
||||
return { ...data, slug: file.replace(".mdx", "") };
|
||||
})
|
||||
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
.sort(
|
||||
(a, b) =>
|
||||
new Date(b.date as string).getTime() -
|
||||
new Date(a.date as string).getTime(),
|
||||
);
|
||||
|
||||
writeFileSync(outputFile, JSON.stringify(blogPosts, null, 2), "utf-8");
|
||||
writeFileSync(OUTPUT_FILE, JSON.stringify(posts, null, 2));
|
||||
}
|
||||
|
||||
exec("bunx @biomejs/biome format --write ./src/data/posts.json");
|
||||
};
|
||||
|
||||
export const blogPostsPlugin = (): Plugin => {
|
||||
return {
|
||||
name: "blog-posts-gen",
|
||||
buildEnd() {
|
||||
processFiles();
|
||||
},
|
||||
configureServer(server) {
|
||||
server.watcher.on("change", (filePath) => {
|
||||
if (
|
||||
!filePath.includes("/src/routes/blog") &&
|
||||
!filePath.includes("blogPostsPlugin.ts")
|
||||
)
|
||||
return;
|
||||
|
||||
processFiles();
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
export const blogPostsPlugin = (): Plugin => ({
|
||||
name: "blog-posts-gen",
|
||||
buildStart: generatePostsJson,
|
||||
configureServer(server) {
|
||||
server.watcher.on("change", (path) => {
|
||||
if (path.includes("src/routes/blog") && path.endsWith(".mdx")) {
|
||||
generatePostsJson();
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user