-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyllabus.js
99 lines (71 loc) · 2.87 KB
/
syllabus.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
const yaml = require('yaml');
const marked = require("marked");
const matter = require('gray-matter');
const { getFrontMatterStringFromObject } = require("./utils");
const { SYLLABUS_PATTERN } = require("./constants");
function createSyllabusEntries(syllabus, messages) {
let syllabusText = "";
const syllabusEntries = Object.entries(syllabus);
syllabusEntries.forEach(([key, value], i, s) => {
syllabusText += `### ${value.title}\n\n`
if (value.schedule) {
const weekOrDay = Object.keys(value.schedule)[0];
const weekOrDaySingular = weekOrDay.slice(0, -1);
const unit = weekOrDaySingular[0].toUpperCase() + weekOrDaySingular.slice(1);
const unitLowerCase = unit.toLowerCase();
const scheduleEntries = Object.entries(value.schedule[weekOrDay]);
scheduleEntries.forEach(([index, v], idx, list) => {
const indexPadded = index.padStart(2, "0");
const link = v.open ? `${unitLowerCase}${indexPadded}/index.md` : `#${unitLowerCase}${indexPadded}/index.md`;
if (v.no_unit) {
syllabusText += ` - [**${v.title}**](${link})`;
} else {
syllabusText += ` - [**${unit} ${indexPadded}**: ${v.title}](${link})`;
}
if (!v.open) {
syllabusText += ` _(${messages.MODULE_CURRENTLY_NOT_AVAILABLE})_`;
}
const isLastSyllabusEntry = i === (s.length - 1);
const isLastScheduleEntry = idx === (list.length - 1);
if (!(isLastSyllabusEntry && isLastScheduleEntry)) {
syllabusText += "\n";
}
if (idx === (list.length - 1) && (i !== (s.length - 1))) {
syllabusText += "\n";
}
});
}
});
return syllabusText;
}
function replaceMarkdownWithSyllabus(markdownTokens, syllabusText) {
return markdownTokens.map(token => {
const isParagraph = token.type === "paragraph";
const hasText = token.text;
if (!hasText) {
return token;
}
const containsSyllabusPattern = token.text.indexOf(SYLLABUS_PATTERN) > -1;
if (isParagraph && hasText && containsSyllabusPattern) {
token.raw = syllabusText;
}
return token;
});
}
function createSyllabusFromMarkdownText({ configYaml, textContent }) {
const { Syllabus: syllabus, messages } = yaml.parse(configYaml);
// Parse markdown and separate Frontmatter and main content:
const { content, data: fm, orig } = matter(textContent);
// Parse markdown tokens:
const markdownTokens = marked.lexer(content);
const syllabusText = createSyllabusEntries(syllabus, messages);
const updatedMarkdown = replaceMarkdownWithSyllabus(markdownTokens, syllabusText);
// APPEND FRONTMATTER TO THE OUTPUT FILE:
const fmString = getFrontMatterStringFromObject(fm);
// WRITE MARKDOWN FILE:
const outputText = updatedMarkdown.map(t => t.raw).join("");
return fmString + outputText;
}
module.exports = {
createSyllabusFromMarkdownText
}