on master help sign in
lib: refactor directories as switch statement

Allows for fallback directoriies to be grabbed from pwd. By doing so you can programmatically grab nested directories from [slug] pages.

commit5af14209edecff3377bed6d5f5e6ae7ec8279e86 authorMatilde Park <matilde@tlon.io> dateThu 22 Jul 2021, 15:28 (5 years ago) parent187dcf2
1 file changed+28−17
lib/lib.js+28−17
lib/lib.js+28−17
@@ -5,18 +5,29 @@ import { join, parse } from "path";
55import matter from "gray-matter";
66import toml from "@iarna/toml";
77
8−const directories = {
9− blog: join(process.cwd(), "content/blog"),
10− events: join(process.cwd(), "content/events"),
11− docs: join(process.cwd(), "content/docs"),
12− grants: join(process.cwd(), "content/grants"),
13− media: join(process.cwd(), "content/media"),
14− updates: join(process.cwd(), "content/updates"),
15− "/": join(process.cwd(), "content"),
8+const directories = (dir) => {
9+ switch (dir) {
10+ case "blog":
11+ return join(process.cwd(), "content/blog");
12+ case "events":
13+ return join(process.cwd(), "content/events");
14+ case "docs":
15+ return join(process.cwd(), "content/docs");
16+ case "grants":
17+ return join(process.cwd(), "content/grants");
18+ case "media":
19+ return join(process.cwd(), "content/media");
20+ case "updates":
21+ return join(process.cwd(), "content/updates");
22+ case "/":
23+ return join(process.cwd(), "content");
24+ default:
25+ return join(process.cwd(), `content/${dir}`);
26+ }
1627};
1728
1829export function getPostSlugs(key) {
19− return fs.readdirSync(directories[key]);
30+ return fs.readdirSync(directories(key));
2031}
2132
2233// NB Gavin: This could be much simpler if we converted everything to a more common standard, like YAML
@@ -30,7 +41,7 @@ const options = {
3041
3142export function getPostBySlug(slug, fields = [], key) {
3243 const realSlug = slug.replace(/\.md$/, "");
33− const fullPath = join(directories[key], `${realSlug}.md`);
44+ const fullPath = join(directories(key), `${realSlug}.md`);
3445 const fileContents = fs.readFileSync(fullPath, "utf8");
3546 const { data, content } = matter(fileContents, options);
3647 const items = {};
@@ -83,7 +94,7 @@ function getAllPaths(dirPath, arrayOfFiles) {
8394}
8495
8596export function getDocsPaths() {
86− return getAllPaths(directories.docs, []);
97+ return getAllPaths(directories("docs"), []);
8798}
8899
89100export function getNextPost(slug, fields = [], key) {
@@ -234,13 +245,13 @@ export const getPage = (path) => {
234245
235246// Returns an array of unique grants categories
236247export function getGrantsCategories() {
237− const paths = fs.readdirSync(directories.grants);
248+ const paths = fs.readdirSync(directories("grants"));
238249 const uniqueCategories = paths
239250 // Get only markdown files
240251 .filter((path) => parse(path).ext === ".md")
241252 // Parse frontmatter
242253 .map((path) =>
243− matter(fs.readFileSync(join(directories.grants, path)), options)
254+ matter(fs.readFileSync(join(directories("grants"), path)), options)
244255 )
245256 // Each markdown file has an array of categories. Collapse these arrays into one array.
246257 .reduce((acc, metadata) => {
@@ -258,13 +269,13 @@ export function getGrantsCategories() {
258269
259270// Returns an array of unique grants types
260271export function getGrantsTypes() {
261− const paths = fs.readdirSync(directories.grants);
272+ const paths = fs.readdirSync(directories("grants"));
262273 const uniqueTypes = paths
263274 // Get only markdown files
264275 .filter((path) => parse(path).ext === ".md")
265276 // Parse frontmatter to JSON
266277 .map((path) =>
267− matter(fs.readFileSync(join(directories.grants, path)), options)
278+ matter(fs.readFileSync(join(directories("grants"), path)), options)
268279 )
269280 // Each markdown file has an array of types. Collapse these arrays into one array.
270281 .reduce((acc, metadata) => {
@@ -282,13 +293,13 @@ export function getGrantsTypes() {
282293
283294// Returns a number which represents how many grants are open, or uncompleted
284295export function getOpenGrantsCount() {
285− const paths = fs.readdirSync(directories.grants);
296+ const paths = fs.readdirSync(directories("grants"));
286297 const count = paths
287298 // Get only markdown files
288299 .filter((path) => parse(path).ext === ".md")
289300 // Parse frontmatter to JSON
290301 .map((path) =>
291− matter(fs.readFileSync(join(directories.grants, path)), options)
302+ matter(fs.readFileSync(join(directories("grants"), path)), options)
292303 )
293304 // Count grants which are both uncompleted and not assigned
294305 .reduce((acc, metadata) => {