forked from DeterminateSystems/zero-to-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iles.config.ts
102 lines (90 loc) · 3.1 KB
/
iles.config.ts
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
import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs";
import headings from "@islands/headings";
import icons from "@islands/icons";
import { RawPageMatter, RouteToRender, SSGContext, defineConfig } from "iles";
import rehypeExternalLinks from "rehype-external-links";
import rehypeSlugCustomId from "rehype-slug-custom-id";
import remarkEmoji from "remark-emoji";
import remarkGfm from "remark-gfm";
import codeBlockPlugin from "./src/plugins/code";
import site from "./src/site";
const { url: siteUrl } = site;
export default defineConfig({
siteUrl,
extendFrontmatter(frontmatter: RawPageMatter, filename: string) {
// Set the layout for e.g. src/pages/section/foo.mdx to section
const section = filename.split("/").at(2)!;
// Infer the order from the filename
if (section === "start") {
const order = getFileSlug(filename)!;
frontmatter["order"] = parseInt(order);
}
if (["concepts", "start"].includes(section) && filename.endsWith("mdx")) {
frontmatter.layout = filename.split("/")[2];
}
// Add an `id` parameter to concept pages based on filename
if (filename.split("/")[2] === "concepts") {
const id = getFileSlug(filename);
frontmatter["id"] = id;
}
},
markdown: {
rehypePlugins: [
[
rehypeExternalLinks,
{ target: "_blank", rel: ["noopener", "noreferrer"] },
],
[rehypeSlugCustomId, { enableCustomId: true }],
],
remarkPlugins: [remarkGfm, remarkEmoji],
},
modules: [
headings(),
icons(),
codeBlockPlugin({
aliases: {
shell: "bash",
},
}),
],
prettyUrls: true,
ssg: {
onSiteRendered: ({ pages, config }: SSGContext) => {
// Only necessary when checking internal links
if (process.env["ENV"] === "ci") {
const out = config.outDir;
pages
.filter((page: RouteToRender) => page.outputFilename !== "index.html")
.forEach((page: RouteToRender) => {
const filename = page.outputFilename;
const html = page.rendered;
const pathSegments = filename.split("/");
const relativeOutputDir =
pathSegments.length > 1
? // Convert e.g. /start/install.html to /start/install/index.html
`${pathSegments.at(0)}/${getFileSlug(filename)}`
: // Convert e.g. /start.html to /start/index.html
getFileSlug(filename);
const outputFilename = `${out}/${filename}`;
rmSync(outputFilename);
const newOutputDir = `${out}/${relativeOutputDir}`;
if (!existsSync(newOutputDir)) {
mkdirSync(newOutputDir);
}
const filepath = `${newOutputDir}/index.html`;
writeFileSync(filepath, html);
});
}
},
},
turbo: false, // turbo messes up posthog's pageview tracking
vite: {
mode: process.env["ENV"] === "production" ? "production" : "development",
server: {
port: 3000,
open: true,
},
},
});
// Utils
const getFileSlug = (path: string) => path.split("/").at(-1)?.split(".").at(0);