-
Notifications
You must be signed in to change notification settings - Fork 34
/
generate-readme.ts
163 lines (123 loc) · 4.65 KB
/
generate-readme.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
162
163
import mustache from "mustache";
import GithubSlugger from "github-slugger";
import fs from "fs";
import { List } from "./types";
const slugger = new GithubSlugger();
export function generateReadme(list: List) {
// import the readme stub
const template = fs.readFileSync("./templates/readme.template.md", "utf-8");
// generate the table of contents
const contentsList = list
.map((l) => {
const { title } = l;
const slug = slugger.slug(title);
return `1. [**${title}**](#${slug})`;
})
.concat("1. [**Closed Groups**](#closed-groups)")
.join("\n");
// generate each list
const groupsLists = list.map((l) => {
const { title, description, rows } = l;
const sortedRows = Object.keys(rows)
.filter((group) => rows[group].closureReason === undefined) // remove closed groups
.sort(Intl.Collator().compare) // sort alphabetically, case insensitive
.map((group) => {
const { link, locations, keywords, careerLink } = rows[group];
const formattedName = `[**${group}**](${link})`;
const formattedLocations = locations.map((loc) => `[${loc}]`).join(" ");
let formattedCareerLink = "";
// if it's a link
if (
careerLink &&
/[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/.test(
careerLink
)
) {
formattedCareerLink = `[🌐](${careerLink})`;
}
// if it's an email
if (careerLink && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(careerLink)) {
formattedCareerLink = `[📧](mailto:${careerLink})`;
}
return [formattedName, formattedLocations, keywords, formattedCareerLink];
});
let md = "";
md += `## ${title}\n\n`;
if (description) md += `${description}\n\n`;
md += `| Name | Locations | Keywords | Jobs |\n`;
md += `| ---- | --------- | -------- | ---- |\n`;
md += sortedRows.map((row) => `| ${row[0]} | ${row[1]} | ${row[2]} | ${row[3]}`).join("\n");
md += "\n\n";
return md;
});
// create the markdown
const md = mustache.render(template, {
groups: groupsLists.join("\n"),
toc: contentsList,
});
fs.writeFileSync("README.md", md);
}
export function generateClosedReadme(list: List) {
// import the readme stub
const template = fs.readFileSync("./templates/closed.template.md", "utf-8");
// create markdown string
let groupsLists = "";
groupsLists += `| Name | Locations | Keywords | Closure Reason | up? |\n`;
groupsLists += `| ---- | --------- | -------- | -------------- | --- |\n`;
list.map((l) => {
const { rows } = l;
const sortedRows = Object.keys(rows)
.filter((group) => rows[group].closureReason !== undefined) // remove non-closed groups
.sort(Intl.Collator().compare) // sort alphabetically, case insensitive
.map((group) => {
const { link, locations, keywords, closureReason } = rows[group];
const linkedName = `[**${group}**](${link})`;
const locationsString = locations
.map(
(loc) =>
`![${loc}](https://img.shields.io/badge/-${encodeURIComponent(
loc
)}-lightgrey?style=flat)`
)
.join(" ");
const upImage = `![](https://img.shields.io/website?down_color=%2300000000&down_message=%E2%9D%8C&label=%20&style=flat-square&up_color=%2300000000&up_message=%F0%9F%8C%90&url=${encodeURIComponent(
link
)})`;
return [linkedName, locationsString, keywords, closureReason, upImage];
});
groupsLists += sortedRows
.map((row) => `| ${row[0]} | ${row[1]} | ${row[2]} | ${row[3]} | ${row[4]} |`)
.join("\n");
return null;
});
// create the markdown
const md = mustache.render(template, { groups: groupsLists });
fs.writeFileSync("CLOSED.md", md);
}
export function generateUpReadme(list: List) {
// import the readme stub
const template = fs.readFileSync("./templates/up.template.md", "utf-8");
// create markdown string
let groupsLists = "";
groupsLists += `| Name | up? |\n`;
groupsLists += `| ---- | --- |\n`;
list.map((l) => {
const { rows } = l;
const sortedRows = Object.keys(rows)
.filter((group) => rows[group].closureReason === undefined) // remove closed groups
.sort(Intl.Collator().compare) // sort alphabetically, case insensitive
.map((group) => {
const { link } = rows[group];
const linkedName = `[**${group}**](${link})`;
const upImage = `![](https://img.shields.io/website?down_color=%2300000000&down_message=%E2%9D%8C&label=%20&style=flat-square&up_color=%2300000000&up_message=%F0%9F%8C%90&url=${encodeURIComponent(
link
)})`;
return [linkedName, upImage];
});
groupsLists += sortedRows.map((row) => `| ${row[0]} | ${row[1]} |`).join("\n");
return null;
});
// create the markdown
const md = mustache.render(template, { groups: groupsLists });
fs.writeFileSync("UP.md", md);
}