-
Notifications
You must be signed in to change notification settings - Fork 18
/
pathlists.js
100 lines (89 loc) · 3.44 KB
/
pathlists.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
const {lessonSrc, lessonFiltertags} = require('./buildconstants');
const path = require('path');
const fs = require('fs');
const yaml = require('yamljs');
const yamlFront = require('yaml-front-matter');
const glob = require('glob');
// Need to replace backslashes with forward slashes on Windows, since glob keeps forward slashes
const lessonSrcPath = lessonSrc.replace(/\\/g, '/');
/**
* Get paths to all courses. Currently based on all "src/ * /index.md"
* @param ending Optional ending to be added at the end of the paths
* @returns {string[]} The paths without a leading /
*/
const coursePaths = (ending) => {
if (typeof ending === 'undefined') { ending = ''; }
return glob.sync(path.join(lessonSrcPath, '*/index.md'), {dot: true})
.map(p => p.replace(new RegExp(`^${lessonSrcPath}/(.*)/index\\.md$`), '$1' + ending));
};
/**
* Get paths to all lessons. Currently based on all "src / * / * /lesson.yml"
* @param {string} ending Optional ending to be added at the end of the paths
* @param {boolean} verbose Set to true for debug info
* @returns {string[]} The paths without a leading /
*/
const lessonPaths = (ending = '', verbose = false) => {
const availableLanguages = yaml.load(path.join(lessonFiltertags, 'keys.yml')).language;
if (verbose) {
console.log('Available languages:', availableLanguages);
}
// Get list of all lessons as paths like 'scratch/astrokatt'
const lessons = glob.sync(path.join(lessonSrcPath, '*/*/lesson.yml')).map(p => path.dirname(p));
let paths = [];
for (const lesson of lessons) {
const lessonTranslations = glob.sync(path.join(lesson, '*.md'))
.filter(p => !p.endsWith('index.md'))
.filter(p => {
try {
const {title, external, language} = yamlFront.loadFront(fs.readFileSync(p));
if (external) {
if (verbose) {
console.log('Skipping external lesson "' + title + '" (' + p + ')');
}
return false;
}
if (!language) {
if (verbose) {
console.warn('WARNING: Lesson "' + title + '" (' + p + ') has no language, skipping.');
}
return false;
}
if (!availableLanguages.includes(language)) {
if (verbose) {
console.log('NOTE: Lesson "' + title + '" (' + p + ') uses the language ' + language +
', which is not currently available, skipping.');
}
return false;
}
//console.log('OK:', language, title, p);
return true;
}
catch (e) {
console.error('Error while processing', p, ':', e);
return false;
}
})
.map(p => p.replace(new RegExp(`^(${lessonSrcPath}/)(.*)(\\.md)$`), '$2' + ending));
paths = paths.concat(lessonTranslations);
}
return paths;
};
const getStaticSitePaths = (verbose) => {
if (typeof verbose === 'undefined') { verbose = false; }
// The '/' will render to '/index.html'
const paths = [
'/', // '/' is the same as '/index.html'
'/404.html',
];
const courses = coursePaths('.html').map(p => '/' + p);
const lessons = lessonPaths('.html').map(p => '/' + p);
const staticPaths = paths.concat(courses).concat(lessons);
if (verbose) {
console.log('Static paths:');
console.log(staticPaths);
}
return staticPaths;
};
module.exports.coursePaths = coursePaths;
module.exports.lessonPaths = lessonPaths;
module.exports.getStaticSitePaths = getStaticSitePaths;