-
Notifications
You must be signed in to change notification settings - Fork 14
/
partial-render.ts
161 lines (123 loc) · 5.17 KB
/
partial-render.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
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
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'; // Path to your config YAML file
const schedulePath = 'schedule.yml';
// -------------------------------- //
// Make partial schedule //
// -------------------------------- //
// Make schedule.yml where each item defaults to render: true
// This is overridden and set to false for items where date > live-as-of date
// That is overridden by a pre-existing render value that exists in _config.yml
function convertDateToISOFormat(dateStr: string, timezone: string): string {
const [month, day, year] = dateStr.split('/').map(num => num.padStart(2, '0'));
return `20${year}-${month}-${day}T00:00:00${timezone}`;
}
async function makePartialSchedule(configPath: string, schedulePath: string) {
const yamlContent = await Deno.readTextFile(configPath);
let config = parse(yamlContent) as any;
const renderAsOfStr = config["partial-render"]["render-as-of"];
const timezone = config["partial-render"]["timezone"];
const thresholdDate = await new Date(convertDateToISOFormat(renderAsOfStr, timezone));
const schedule = config.schedule.map(week => ({
...week,
days: week.days.map(day => ({
...day,
items: day.items ? day.items.map(item => ({
...item,
render: item.hasOwnProperty('render') ? item.render :
item.hasOwnProperty('date') ? new Date(convertDateToISOFormat(day.date, "+00:00")) < thresholdDate :
true
})) : []
}))
}));
await Deno.writeTextFile(schedulePath, stringify(schedule));
}
await makePartialSchedule(configPath, schedulePath);
// ----------------------------------- //
// Make Sidebar Nav from schedule //
// ----------------------------------- //
async function makeSidebarNav(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 sidebarTypes = config['adaptive-nav']['sidebar'];
const isHybridMode = config['adaptive-nav']?.hybrid || false;
let sidebarContents = [];
sidebarTypes.forEach(sidebarType => {
const type = sidebarType.type;
const landingPage = sidebarType['landing-page'];
let typeHrefs: string[] = [];
if (landingPage) {
typeHrefs.push(landingPage);
}
schedule.forEach(week => {
week.days.forEach(day => {
day.items.forEach(item => {
if (item.type === type && item.render) {
typeHrefs.push(item.href);
}
});
});
});
if (isHybridMode) {
sidebarContents.push({ title: type, contents: typeHrefs });
} else {
sidebarContents.push({ section: type, contents: typeHrefs });
}
});
const sidebarNav = {
website: {
sidebar: isHybridMode ? sidebarContents : { contents: sidebarContents }
}
};
const sidebarNavPath = join(dirname(schedulePath), 'sidebar-nav.yml');
await Deno.writeTextFile(sidebarNavPath, stringify(sidebarNav));
}
await makeSidebarNav(schedulePath, configPath);
// -------------------------------- //
// Ignore files based on schedule //
// -------------------------------- //
async function ignoreFiles(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(oldPath, newPath);
console.log(`Renamed: ${oldPath} to ${newPath}`);
} catch (error) {
console.error(`Error renaming ${oldPath} to ${newPath}:`, error.message);
}
}
}
}
}
}
}
console.log("> Ignoring Files")
await ignoreFiles(schedulePath);
// -------------------------------- //
// Render partial site //
// -------------------------------- //
async function runQuartoRender() {
const process = Deno.run({
cmd: ["quarto", "render", "--profile", "partial-site"],
stdout: "inherit",
stderr: "inherit",
});
const { code } = await process.status();
if (code !== 0) {
console.error('Error: Quarto render process exited with code', code);
}
process.close();
}
console.log("> Partial render list has been made.");
await runQuartoRender();