-
Notifications
You must be signed in to change notification settings - Fork 0
/
_create-index.mjs
76 lines (62 loc) · 1.87 KB
/
_create-index.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
import fs from "fs"
function repeat(c, count) {
while (c.length < count) {
c += c;
}
return c.substring(0, count);
}
const header = fs.readFileSync("./_header.md").toString().trim().split("\n");
const footer = fs.readFileSync("./_footer.md").toString().trim().split("\n");
const data = JSON.parse(fs.readFileSync("./_data.json").toString());
const output = [ ];
output.push(...header);
for (const { name, tag } of data.categories) {
const getLink = (info, _name) => {
const name = _name || info.name;
const link = [ ];
if (info.filename) {
link.push(`[${ name }](./${ tag }/${ info.filename })`);
} else {
if (!info.url) { return ""; }
link.push(name);
}
if (info.url) {
link.push(` <sup>[[external](${ info.url })]</sup>`);
}
return link.join("");
};
output.push("", "");
output.push(name);
output.push(repeat("-", name.length));
output.push("");
data.parts.sort((a, b) => {
const cmpName = a.name.localeCompare(b.name);
if (cmpName) { return cmpName; }
return a.descr.localeCompare(b.descr);
});
for (const part of data.parts) {
if (part.cat !== tag) { continue; }
part.done = true;
output.push(`- **${ part.name }** - *${ part.descr }*`);
const datasheet = getLink(part, "datasheet");
if (datasheet) { output.push(` - ${ datasheet }`); }
if (part.urls) {
for (const url of part.urls) {
const line = getLink(url);
if (!line) { continue; }
output.push(` - ${ line }`);
}
}
if (part.mpn) {
output.push(` - LCSC Part ${ part.mpn } <sup>[[JLCPCB](https://jlcpcb.com/partdetail/${ part.mpn })]</sup>`);
}
}
}
output.push("", "");
output.push(...footer);
fs.writeFileSync("./README.md", output.join("\n"));
for (const part of data.parts) {
if (!part.done) {
console.log(`Warning: ${ part.name } was not output`);
}
}