|
| 1 | +import { readdirSync, readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { |
| 4 | + addServerScanDir, |
| 5 | + addServerTemplate, |
| 6 | + createResolver, |
| 7 | + defineNuxtModule, |
| 8 | +} from "@nuxt/kit"; |
| 9 | +import { parse } from "comark"; |
| 10 | +import highlight from "comark/plugins/highlight"; |
| 11 | +import matter from "gray-matter"; |
| 12 | +import { z } from "zod"; |
| 13 | +import type { BlogArticle } from "./content/runtime/types/blog-article"; |
| 14 | + |
| 15 | +const frontmatterSchema = z.object({ |
| 16 | + title: z.string().optional(), |
| 17 | + description: z.string().optional(), |
| 18 | + date: z.coerce.date(), |
| 19 | + tags: z.array(z.string()), |
| 20 | + categories: z.array(z.string()), |
| 21 | + cover_image: z.string().optional(), |
| 22 | + img: z.string().optional(), |
| 23 | + external_url: z.string().url().optional(), |
| 24 | + external_site: z.string().optional(), |
| 25 | + canonical_url: z.string().url().optional(), |
| 26 | + draft: z.boolean().optional(), |
| 27 | +}); |
| 28 | + |
| 29 | +function slugFromFilePath(filePath: string) { |
| 30 | + return filePath.split("/").pop()?.replace(/\.md$/, "") || ""; |
| 31 | +} |
| 32 | + |
| 33 | +function excerptFromBody(body: string) { |
| 34 | + const summary = body.split(/<!--\s*more\s*-->/i)[0] || body; |
| 35 | + |
| 36 | + return summary |
| 37 | + .replace(/```[\s\S]*?```/g, "") |
| 38 | + .replace(/<!--[\s\S]*?-->/g, "") |
| 39 | + .replace(/<[^>]+>/g, "") |
| 40 | + .replace(/!\[[^\]]*]\([^)]+\)/g, "") |
| 41 | + .replace(/\[([^\]]+)]\([^)]+\)/g, "$1") |
| 42 | + .replace(/[#>*_`~-]/g, "") |
| 43 | + .replace(/\s+/g, " ") |
| 44 | + .trim(); |
| 45 | +} |
| 46 | + |
| 47 | +async function parseArticle( |
| 48 | + filePath: string, |
| 49 | + rawMarkdown: string, |
| 50 | +): Promise<BlogArticle> { |
| 51 | + const slug = slugFromFilePath(filePath); |
| 52 | + const { data, content } = matter(rawMarkdown); |
| 53 | + const frontmatter = frontmatterSchema.parse(data); |
| 54 | + const tree = await parse(content.trim(), { |
| 55 | + plugins: [highlight()], |
| 56 | + }); |
| 57 | + |
| 58 | + return { |
| 59 | + ...frontmatter, |
| 60 | + _id: `content:blog:${slug}`, |
| 61 | + slug, |
| 62 | + path: `/blog/${slug}`, |
| 63 | + tree, |
| 64 | + excerpt: excerptFromBody(content), |
| 65 | + date: frontmatter.date.toISOString(), |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +async function loadArticles(rootDir: string) { |
| 70 | + const blogContentDir = join(rootDir, "content", "blog"); |
| 71 | + const articles = await Promise.all( |
| 72 | + readdirSync(blogContentDir) |
| 73 | + .filter((fileName) => fileName.endsWith(".md")) |
| 74 | + .map((fileName) => { |
| 75 | + const filePath = join(blogContentDir, fileName); |
| 76 | + return parseArticle(filePath, readFileSync(filePath, "utf8")); |
| 77 | + }), |
| 78 | + ); |
| 79 | + |
| 80 | + return articles.sort( |
| 81 | + (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime(), |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +export default defineNuxtModule({ |
| 86 | + meta: { |
| 87 | + name: "content", |
| 88 | + }, |
| 89 | + setup(_, nuxt) { |
| 90 | + const resolver = createResolver(import.meta.url); |
| 91 | + |
| 92 | + addServerTemplate({ |
| 93 | + filename: "#content/articles", |
| 94 | + async getContents() { |
| 95 | + const articles = await loadArticles(nuxt.options.rootDir); |
| 96 | + |
| 97 | + return `const articles = ${JSON.stringify(articles, null, 2)} |
| 98 | +
|
| 99 | +const articleSummaries = articles.map(({ tree, ...article }) => article) |
| 100 | +
|
| 101 | +export function getAllBlogArticles(options = {}) { |
| 102 | + if (options.includeDrafts) { |
| 103 | + return articles |
| 104 | + } |
| 105 | +
|
| 106 | + return articles.filter((article) => !article.draft) |
| 107 | +} |
| 108 | +
|
| 109 | +export function getAllBlogArticleSummaries(options = {}) { |
| 110 | + if (options.includeDrafts) { |
| 111 | + return articleSummaries |
| 112 | + } |
| 113 | +
|
| 114 | + return articleSummaries.filter((article) => !article.draft) |
| 115 | +} |
| 116 | +
|
| 117 | +export function getBlogArticleBySlug(slug) { |
| 118 | + return articles.find((article) => article.slug === slug) |
| 119 | +} |
| 120 | +`; |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + addServerScanDir(resolver.resolve("./content/runtime/server")); |
| 125 | + }, |
| 126 | +}); |
0 commit comments