initial commit
This commit is contained in:
49
build-helpers/blogPostsPlugin.ts
Normal file
49
build-helpers/blogPostsPlugin.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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";
|
||||
|
||||
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"),
|
||||
)
|
||||
.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 };
|
||||
})
|
||||
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
|
||||
writeFileSync(outputFile, JSON.stringify(blogPosts, null, 2), "utf-8");
|
||||
|
||||
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();
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
38
build-helpers/mdxPrism.ts
Normal file
38
build-helpers/mdxPrism.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { visit } from "unist-util-visit";
|
||||
import { toString as nodeToString } from "hast-util-to-string";
|
||||
import { refractor } from "refractor";
|
||||
import tsx from "refractor/lang/tsx.js";
|
||||
|
||||
refractor.register(tsx);
|
||||
|
||||
export const mdxPrism = () => {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
return (tree: any) => {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
visit(tree, "element" as any, visitor);
|
||||
};
|
||||
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
function visitor(node: any, index: number | undefined, parent: any) {
|
||||
if (parent.type !== "mdxJsxFlowElement") {
|
||||
return;
|
||||
}
|
||||
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
const attrs = parent.attributes.reduce((a: any, c: any) => {
|
||||
if (c.type === "mdxJsxAttribute") {
|
||||
a[c.name] = c.value;
|
||||
}
|
||||
return a;
|
||||
}, {});
|
||||
|
||||
const lang = attrs.lang;
|
||||
if (!lang) {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = refractor.highlight(nodeToString(node), lang);
|
||||
|
||||
node.children = result.children;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user