-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-collections.ts
More file actions
187 lines (172 loc) · 5.12 KB
/
content-collections.ts
File metadata and controls
187 lines (172 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { exec as syncExec } from "node:child_process";
import fsp from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import { defineCollection, defineConfig } from "@content-collections/core";
import rehypeToc from "@stefanprobst/rehype-extract-toc";
import rehypeTocExport from "@stefanprobst/rehype-extract-toc/mdx";
import readingTime from "reading-time";
import rehypeCitation from "rehype-citation";
import rehypeKatex from "rehype-katex";
import rehypePresetMinify from "rehype-preset-minify";
import rehypePrism from "rehype-prism-plus";
import rehypeSlug from "rehype-slug";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import { z } from "zod";
import resolveImageBlurDataURL from "./lib/image-blur-data-url";
import { compileMDX } from "./lib/mdx";
import remarkConvertInlineFootnotes from "./lib/remark-convert-inline-footnote";
import searchIndex from "./lib/search-index";
import staticImages, { staticCoverImage } from "./lib/static-images";
import { sha256 } from "./lib/utils";
import withoutBody from "./lib/without-body";
const exec = promisify(syncExec);
const POST_DIRECTORY = "content/posts";
type ImageParams = { image: string; directory: string };
function calculateReadingTime(content: string) {
const contentWithoutSvg = content.replace(/<svg+.+?(?=<\/svg>)<\/svg>/gs, "");
return readingTime(contentWithoutSvg).minutes.toFixed(0);
}
function extractLocale(filePath: string) {
// Regular expression to match common locale patterns
const localeRegex = /index\.([a-z]{2})\.mdx$/i;
const match = filePath.match(localeRegex);
// Check if a locale was found, otherwise default to 'en'
if (match && match.length > 1) {
return match[1];
}
return "en"; // Default to English if not found
}
const getSlugAndDateFromDir = (dir: string) => {
const isDate = (dt: string) => String(new Date(dt)) !== "Invalid Date";
const dateAndSlug = dir.split("--");
if (dateAndSlug.length < 2) {
throw Error(
`invalid post folder format ${dir}. it should be yyyy-mm-dd--title-of-the-post`,
);
}
if (dateAndSlug[0] === undefined || !isDate(dateAndSlug[0])) {
throw Error("invalid date. it should be yyyy-mm-dd");
}
if (dateAndSlug[1] === undefined) {
throw Error("invalid or empty post title");
}
return { date: dateAndSlug[0], slug: dateAndSlug[1] };
};
async function lastModificationDate(filePath: string) {
const { stdout } = await exec(
`git log -1 --format=%ai -- ${path.join(POST_DIRECTORY, filePath)}`,
);
if (stdout) {
return new Date(stdout.trim()).toISOString();
}
return new Date().toISOString();
}
async function doesFileExist(path: string) {
try {
return (await fsp.stat(path)).isFile();
} catch (e) {
return false;
}
}
async function collectImageInformation({ image, directory }: ImageParams) {
const url = await staticCoverImage(
POST_DIRECTORY,
"public/posts",
"/posts",
directory,
image,
);
const blurDataURL = await resolveImageBlurDataURL(
POST_DIRECTORY,
directory,
image,
);
return { url, blurDataURL };
}
const posts = defineCollection({
name: "posts",
directory: POST_DIRECTORY,
include: "*/**.{md,mdx}",
schema: z.object({
title: z.string(),
summary: z.string(),
isPublished: z.boolean(),
tags: z.array(z.string()),
}),
transform: async (post, ctx) => {
const locale = extractLocale(post._meta.filePath);
const dir = path.join(process.cwd(), POST_DIRECTORY, post._meta.directory);
const hasReferences = await doesFileExist(path.join(dir, "references.bib"));
const mdx = await compileMDX(ctx, post, {
files: (appender) => {
const directory = path.join(
POST_DIRECTORY,
post._meta.directory,
"components",
);
appender.directory("./components", directory);
},
rehypePlugins: [
rehypeKatex,
[
rehypeCitation,
{
path: dir,
bibliography: hasReferences ? "references.bib" : undefined,
linkCitations: true,
lang:
locale === "es"
? "https://raw.githubusercontent.com/citation-style-language/locales/master/locales-es-MX.xml"
: undefined,
csl: "https://raw.githubusercontent.com/citation-style-language/styles/master/acm-sig-proceedings.csl",
},
],
rehypePrism,
rehypeSlug,
rehypeToc,
rehypeTocExport,
[
staticImages,
{
publicDir: path.join("public", "posts"),
resourcePath: "/posts",
sourceRoot: POST_DIRECTORY,
},
],
rehypePresetMinify,
],
remarkPlugins: [remarkMath, remarkConvertInlineFootnotes, remarkGfm],
});
const lastModification = await ctx.cache(
post._meta.filePath,
lastModificationDate,
);
const { slug, date } = getSlugAndDateFromDir(post._meta.directory);
const id = await sha256(`/${locale}/posts/${slug}`);
return {
...post,
content: {
mdx,
raw: post.content,
},
readingTime: calculateReadingTime(post.content),
lastModification,
locale,
date,
slug,
id,
url: `/posts/${slug}`,
};
},
onSuccess: async (documents) => {
await Promise.all([
withoutBody(documents, ".generated"),
searchIndex(documents, ".generated"),
]);
},
});
export default defineConfig({
collections: [posts],
});