This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathextract-objectives.ts
65 lines (50 loc) · 1.82 KB
/
extract-objectives.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
// A quick script to extract titles and objectives
// for all content as requested by Next Shift Learning team
import { readdir, readFile, writeFile } from "fs/promises";
import { join } from "path";
import greyMatter from "gray-matter";
const log = console.log;
const directory = join(__dirname, "content");
const allItems = await readdir(directory, {
withFileTypes: true,
});
const filesOnly = allItems
.filter((file) => !file.isDirectory())
.map((file) => file.name);
const getThingsWeCareAbout = async (fileName: string) => {
const fileContent = await readFile(join(directory, fileName), "utf-8");
const parsed = greyMatter(fileContent);
const content = parsed.content;
return {
title: parsed.data.title,
objectives: parsed.data.objectives,
content,
};
};
const FILE = "course-objectives-and-links.md";
const lines: Array<string> = [];
lines.push(`# Course Objectives and Links`);
lines.push(`|title|link|objectives|summaryParagraph|`);
lines.push(`|-----|----|----------|----------------|`);
await Promise.all(
filesOnly.map(async (file) => {
const thingsWeCareAbout = await getThingsWeCareAbout(file);
let title = thingsWeCareAbout.title;
let link = `https://www.soldev.app/course/${file.split(".")[0]}`;
let objectives = thingsWeCareAbout.objectives.join(", ");
// cut the thingsWeCareAbout.content string for only items between '# Summary' and '# Lesson'
let summaryParagraph = "";
try {
summaryParagraph = thingsWeCareAbout.content
.split("# Summary")[1]
.split("# Lesson")[0]
.replace(/\n/g, " ");
} catch (error) {
log(`Error: bad formatting in ${file}`);
return;
}
lines.push(`|${title}|${link}|${objectives}|${summaryParagraph}|`);
}),
);
// write the contents array to a file
await writeFile(FILE, lines.join("\n"));