-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
71 lines (58 loc) · 1.68 KB
/
index.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
import fs from "fs"
import path from "path"
const CONTENT_DIRECTORY = path.join(process.cwd(), "content")
/*
**************
directory structure
**************
content
|-topic
|-section
|-1
| |- notes.md
| |- app.js
|-2
**************
return value structure
**************
{
sectionName: { numberOfPages, title },
sectionName2: { numberOfPages, title }
}
*/
export function sectionProperties(topic) {
const sections = fs.readdirSync(path.join(CONTENT_DIRECTORY, topic))
let result = {}
for (let section of sections) {
const sectionDir = path.join(CONTENT_DIRECTORY, topic, section)
const propertiesContent = fs.readFileSync(
path.join(sectionDir, "properties.json"),
"utf8"
)
const numberOfPages = fs
.readdirSync(sectionDir, { withFileTypes: true })
.filter(f => f.isDirectory()).length
result = {
...result,
[section]: {
numberOfPages,
properties: JSON.parse(propertiesContent),
},
}
}
return result
}
export function pageContents(topic, section, pageId) {
const directory = path.join(CONTENT_DIRECTORY, topic, section, pageId.toString())
const files = fs.readdirSync(directory)
const hasApp = files.includes("app.js") ? true : false
let notesString = "No note"
try {
if (files.includes("notes.md")) {
notesString = fs.readFileSync(path.join(directory, "notes.md"), "utf8")
}
} catch (error) {
console.log(`Error reading note.md in ${directory}`)
}
return { notesString, hasApp }
}