forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.json.js
63 lines (52 loc) · 1.42 KB
/
index.json.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
import * as fs from 'fs';
import send from '@polka/send';
import { extract_frontmatter } from '@sveltejs/site-kit/utils/markdown';
let json;
function get_sections() {
const slugs = new Set();
const sections = fs.readdirSync(`content/tutorial`)
.filter(dir => /^\d+/.test(dir))
.map(dir => {
let meta;
try {
meta = JSON.parse(fs.readFileSync(`content/tutorial/${dir}/meta.json`, 'utf-8'));
} catch (err) {
throw new Error(`Error reading metadata for ${dir}`);
}
return {
title: meta.title,
chapters: fs.readdirSync(`content/tutorial/${dir}`)
.filter(dir => /^\d+/.test(dir))
.map(tutorial => {
try {
const md = fs.readFileSync(`content/tutorial/${dir}/${tutorial}/text.md`, 'utf-8');
const { metadata } = extract_frontmatter(md);
const slug = tutorial.replace(/^\d+-/, '');
if (slugs.has(slug)) throw new Error(`Duplicate slug: ${slug}`);
slugs.add(slug);
return {
slug,
title: metadata.title,
section_dir: dir,
chapter_dir: tutorial,
};
} catch (err) {
throw new Error(`Error building tutorial ${dir}/${tutorial}: ${err.message}`);
}
})
};
});
return sections;
}
export function get(req, res) {
try {
if (!json || process.env.NODE_ENV !== 'production') {
json = get_sections();
}
send(res, 200, json);
} catch (err) {
send(res, 500, {
message: err.message
});
}
}