forked from remotion-dev/remotion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-cards.mjs
121 lines (101 loc) · 3.19 KB
/
render-cards.mjs
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
import { bundle } from "@remotion/bundler";
import { getCompositions, renderStill } from "@remotion/renderer";
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
import { readDir } from "./get-pages.mjs";
const data = [];
const root = path.join(process.cwd(), "docs");
const findId = (split, page) => {
const found = split.find((s) => s.startsWith("id: "));
if (found) {
return found.substr("id: ".length);
}
return page
.replace(process.cwd() + path.sep + "docs" + path.sep, "")
.replace(/.md$/, "");
};
const findTitle = (split) => {
const title = split
.find((s) => s.startsWith("title: "))
.replace(/^title:\s/, "");
if (title.startsWith('"')) {
return title.substr(1, title.length - 2);
}
return title;
};
const findCrumb = (split) => {
const crumb = split
.find((s) => s.startsWith("crumb: "))
?.replace(/^crumb:\s/, "");
if (crumb?.startsWith('"')) {
return crumb.substr(1, crumb.length - 2);
}
return crumb ?? null;
};
const pages = readDir(root);
for (const page of pages) {
const opened = fs.readFileSync(page, "utf8");
const frontmatter = opened.match(/---\n((.|\n)*?)---\n/);
if (!frontmatter) {
continue;
}
const split = frontmatter[1].split("\n");
const id = findId(split, page);
const title = findTitle(split);
const crumb = findCrumb(split);
const relativePath = page.replace(process.cwd() + path.sep, "");
const compId =
"articles-" + relativePath.replace(/\//g, "-").replace(/.md$/, "");
data.push({ id, title, relativePath, compId, crumb });
}
fs.writeFileSync(
path.join(process.cwd(), "src", "data", "articles.ts"),
`export const articles = ` + JSON.stringify(data, null, 2)
);
execSync("pnpm exec prettier src/data/articles.ts --write");
// render cards
const serveUrl = await bundle({
entryPoint: path.join(process.cwd(), "./src/remotion/entry.ts"),
publicDir: path.join(process.cwd(), "static"),
});
const compositions = await getCompositions(serveUrl);
for (const composition of compositions.filter(
(c) => c.id.startsWith("expert") || c.id.startsWith("template")
)) {
await renderStill({
composition,
output: `static/generated/${composition.id}.png`,
serveUrl,
});
console.log("Rendered", composition.id);
}
for (const entry of data) {
const composition = compositions.find((c) => c.id === entry.compId);
const output = `static/generated/${composition.id}.png`;
if (fs.existsSync(output)) {
console.log("Existed", composition.id);
continue;
}
const out = path.join(process.cwd(), entry.relativePath);
await renderStill({
composition,
output,
serveUrl,
});
const fileContents = fs.readFileSync(out, "utf-8");
const lines = fileContents
.split("\n")
.filter((l) => !l.startsWith("image: "));
const frontmatterLine = lines.findIndex((l) => l === "---");
if (frontmatterLine === -1) {
throw new Error("could not find frontmatter for " + composition.id);
}
const newLines = [
...lines.slice(0, frontmatterLine + 1),
`image: /${output.substring("/static".length)}`,
...lines.slice(frontmatterLine + 1),
].join("\n");
fs.writeFileSync(out, newLines);
console.log("Rendered", composition.id);
}