-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.js
142 lines (122 loc) · 3.35 KB
/
lib.js
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
import YAML from "yaml";
import remark from "remark";
import html from "remark-html";
import React from "react";
import fs from "fs";
import path from "path";
import matter from "gray-matter";
export async function getStrings(lang) {
const filepath = path.join(
process.cwd(),
"data",
"translations",
`${lang}.yml`
);
return YAML.parse(fs.readFileSync(filepath).toString());
}
export async function getLangs() {
return Object.fromEntries(
await Promise.all(
fs
.readdirSync(path.join(process.cwd(), "data", "translations"))
.map((filename) => filename.replace(/\.yml/, ""))
.map(async (lang) => [lang, (await getStrings(lang)).language])
)
);
}
export async function getMarkdownFile(filepath) {
const fileContent = fs.readFileSync(filepath);
const matterResult = matter(fileContent);
const content = (
await remark().use(html).process(matterResult.content)
).toString();
return { content, ...matterResult.data };
}
export async function getTags() {
return YAML.parse(
fs.readFileSync(path.join(process.cwd(), "data", "tags.yml")).toString()
);
}
export async function getTag(slug) {
let allTags = (await getTags())
.map((tagSet) => tagSet.tags)
.reduce((tags, setTags) => ({ ...tags, ...setTags }), {});
return allTags[slug];
}
export async function getCompanies() {
let companies = fs
.readdirSync(path.join(process.cwd(), "data", "companies"))
.map((fileName) => fileName.replace(/\.md$/, ""))
.sort()
.reduce((companies, filename) => {
return companies.length > 0 &&
filename.startsWith(companies[companies.length - 1])
? companies
: [...companies, filename];
}, []); // filter out translations
return await Promise.all(
companies.map(async (slug) => ({
slug,
name: (await getCompany(slug)).name,
}))
);
}
export async function getCompany(slug, lang) {
let company = await getMarkdownFile(
path.join(process.cwd(), "data", "companies", `${slug}.md`)
);
if (company.updated) {
company.updated = company.updated.toString();
}
if (company.content.trim() === "") {
delete company.content;
}
if (lang && lang !== company.defaultLang) {
try {
company.content = (
await getMarkdownFile(
path.join(process.cwd(), "data", "companies", `${slug}_${lang}.md`)
)
).content;
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
company.noTranslation = true;
}
}
return { slug, ...company };
}
export async function getAboutPages() {
return await Promise.all(
fs
.readdirSync(path.join(process.cwd(), "about"))
.map((fileName) => fileName.replace(/\.md$/, ""))
);
}
export async function getAboutPage(slug, lang) {
if (lang) {
try {
return await getMarkdownFile(
path.join(process.cwd(), "about", `${slug}_${lang}.md`)
);
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
}
}
return {
noTranslation: lang && lang !== "en",
...(await getMarkdownFile(path.join(process.cwd(), "about", `${slug}.md`))),
};
}
export async function getCommonProps(context) {
return {
companiesList: await getCompanies(),
tags: await getTags(),
strings: await getStrings(context.params.lang),
lang: context.params.lang,
langs: await getLangs(),
};
}