-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathintegrity-enforcement.ts
More file actions
81 lines (74 loc) · 2.73 KB
/
integrity-enforcement.ts
File metadata and controls
81 lines (74 loc) · 2.73 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
import fs from 'fs/promises';
import peopleRaw from '../static/people.json?raw';
import type { BlogPost, Paper, Person } from '../src/lib/app-types';
import { parsePostData, stripHTML } from "../src/lib/pasre-post";
import markdownit from 'markdown-it'
async function generateIndex() {
const people = JSON.parse(peopleRaw) as Person[];
const personMap = Object.fromEntries(people.map((p) => [`${p.first_name} ${p.last_name}`, p]));
const paperList = await fs.readdir('./static/papers');
const papers = [] as Paper[];
for (const paper of paperList) {
const paperRaw = await fs
.readFile(`./static/papers/${paper}`, 'utf8')
.then((x) => JSON.parse(x) as Paper);
const updatedAuthors = paperRaw.authors.map((author) => {
const key = `${author.first_name} ${author.last_name}`;
const { url, display_name } = personMap[key] || {};
return {
...author,
display_name: display_name || undefined,
url: url || undefined
};
});
const updatedPaper = {
...paperRaw,
authors: updatedAuthors
};
if (updatedPaper.visible) {
papers.push(updatedPaper);
}
await fs.writeFile(`./static/papers/${paper}`, JSON.stringify(updatedPaper, null, 2));
}
papers.sort((a, b) => {
const ad = a.mod_date;
const bd = b.mod_date;
const at = a.title;
const bt = b.title;
// sort by reverse mod date, break ties by alphabetic title order
return ad < bd ? 1 : ad > bd ? -1 : at < bt ? -1 : at > bt ? 1 : 0;
});
await fs.writeFile('./static/papers-index.json', JSON.stringify(papers, null, 2));
}
async function generateBlogList() {
const postList = await fs.readdir('./static/blog-assets/posts');
const md = markdownit({ html: true, linkify: true });
const posts = [] as BlogPost[];
for (const post of postList) {
const web_name = post.split(".").slice(0, -1).join(".");
const postRaw = await fs
.readFile(`./static/blog-assets/posts/${post}`, 'utf8')
.then((x) => parsePostData(x, web_name) as BlogPost);
const rendered_post = md.render(postRaw.post)
const summary = stripHTML(rendered_post).slice(0, 100)
const first_image = postRaw.first_image;
posts.push({ meta: postRaw.meta, post: summary, first_image });
}
posts.sort((a, b) => {
const ad = a.meta.date;
const bd = b.meta.date;
const at = a.meta.title;
const bt = b.meta.title;
// sort by reverse mod date, break ties by alphabetic title order
return ad < bd ? 1 : ad > bd ? -1 : at < bt ? -1 : at > bt ? 1 : 0;
});
posts.forEach((a, i) => {
a.meta.recent = (i < 5);
})
await fs.writeFile('./static/blog-index.json', JSON.stringify(posts, null, 2));
}
async function main() {
await generateIndex();
await generateBlogList()
}
main();