-
Notifications
You must be signed in to change notification settings - Fork 14
/
prep-website.ts
122 lines (99 loc) · 4.3 KB
/
prep-website.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { parse, stringify } from "https://deno.land/std/yaml/mod.ts";
import { join, dirname, basename } from "https://deno.land/std/path/mod.ts";
const configPath = '_config.yml';
const schedulePath = 'schedule.yml';
const quartoProfile = Deno.env.get("QUARTO_PROFILE");
// -------------------------------- //
// Clean up file names //
// -------------------------------- //
// If this the script is during a partial-render, remove _ from filenames
async function unIgnoreFiles(schedulePath: string) {
const yamlContent = await Deno.readTextFile(schedulePath);
const schedule = parse(yamlContent) as any;
for (const week of schedule) {
for (const day of week.days) {
if (day.items) {
for (const item of day.items) {
if (item.render === false) {
const oldPath = item.href;
const dir = dirname(oldPath);
const filename = basename(oldPath);
const newPath = join(dir, `_${filename}`);
try {
await Deno.rename(newPath, oldPath);
console.log(`Renamed: ${newPath} to ${oldPath}`);
} catch (error) {
console.error(`Error renaming ${newPath} to ${oldPath}:`, error.message);
}
}
}
}
}
}
}
// If this the script is during a partial-render, remove _ from filenames
if (quartoProfile == "partial-site") {
console.log("> Unignoring Files ...");
await unIgnoreFiles(schedulePath);
}
// ---------------------------------------- //
// Make schedule.yml with all render: true //
// ---------------------------------------- //
// This step is skipped in a partial-render
async function makeFullSchedule(configPath: string, schedulePath: string) {
const yamlContent = await Deno.readTextFile(configPath);
const config = parse(yamlContent) as any;
if (config && config.schedule) {
const updatedSchedule = config.schedule.map(week => ({
...week,
days: week.days.map(day => ({
...day,
items: day.items ? day.items.map(item => ({
...item,
render: true // Set render to true for all items
})) : []
}))
}));
await Deno.writeTextFile(schedulePath, stringify(updatedSchedule));
}
}
// This step is skipped in a partial-render and full-render
if (quartoProfile !== "partial-site" && quartoProfile !== "staff-site") {
console.log("> Making schedule file ...");
await makeFullSchedule(configPath, schedulePath);
}
// -------------------------------- //
// Make Listings //
// -------------------------------- //
async function makeListings(schedulePath: string, configPath: string) {
const scheduleContent = await Deno.readTextFile(schedulePath);
const schedule = parse(scheduleContent) as Array<any>;
const configContent = await Deno.readTextFile(configPath);
const config = parse(configContent) as any;
const listingTypes = config['adaptive-nav']['listings'].map((listing: any) => listing.type);
const typeLists: Record<string, Array<{ path: string }>> = {};
const scheduleDir = dirname(schedulePath);
for (const week of schedule) {
for (const day of week.days) {
if (day.items && Array.isArray(day.items)) {
for (const item of day.items) {
if (item.render && listingTypes.includes(item.type)) {
const type = item.type; // Keep the original case
if (!typeLists[type]) {
typeLists[type] = [];
}
typeLists[type].push({ path: item.href });
}
}
}
}
}
for (const [type, items] of Object.entries(typeLists)) {
const outputPath = join(scheduleDir, `${type}-contents.yml`);
await Deno.writeTextFile(outputPath, stringify(items));
console.log(` - Created file: ${outputPath}`);
}
}
console.log("> Making contents files for listings...")
await makeListings(schedulePath, configPath);
console.log("> Rendering documents...")