-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigation.js
More file actions
91 lines (69 loc) · 2.44 KB
/
navigation.js
File metadata and controls
91 lines (69 loc) · 2.44 KB
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
const fs = require('fs');
const path = require('path');
const CONTENT_DIR = path.join(process.cwd(), 'content');
function readJsonFile(fullPath) {
const raw = fs.readFileSync(fullPath, 'utf8');
return JSON.parse(raw);
}
function normalizeRelativePath(value = '') {
return String(value || '').replace(/\\/g, '/').replace(/^\/+/, '');
}
function getContextDirectory(contextRelativePath = '') {
if (!contextRelativePath) {
return CONTENT_DIR;
}
const normalizedRelativePath = normalizeRelativePath(contextRelativePath);
const absolutePath = path.resolve(CONTENT_DIR, normalizedRelativePath);
if (!absolutePath.startsWith(CONTENT_DIR)) {
return CONTENT_DIR;
}
if (path.extname(absolutePath)) {
return path.dirname(absolutePath);
}
return absolutePath;
}
function findNearestConfigPath(contextDirectory, fileName) {
let currentDirectory = contextDirectory;
while (true) {
const candidate = path.join(currentDirectory, fileName);
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
return candidate;
}
if (currentDirectory === CONTENT_DIR) {
break;
}
const parentDirectory = path.dirname(currentDirectory);
if (parentDirectory === currentDirectory || !parentDirectory.startsWith(CONTENT_DIR)) {
break;
}
currentDirectory = parentDirectory;
}
return path.join(CONTENT_DIR, fileName);
}
function readContextualJsonFile(fileName, contextRelativePath = '') {
const contextDirectory = getContextDirectory(contextRelativePath);
const configPath = findNearestConfigPath(contextDirectory, fileName);
return readJsonFile(configPath);
}
function getSidebarConfig(contextRelativePath = '') {
return readContextualJsonFile('sidebar.json', contextRelativePath);
}
function getHeaderConfig(contextRelativePath = '') {
return readContextualJsonFile('header.json', contextRelativePath);
}
function getSiteConfig(contextRelativePath = '') {
return readContextualJsonFile('site.json', contextRelativePath);
}
function getTemplatesConfig(contextRelativePath = '') {
return readContextualJsonFile('templates.json', contextRelativePath);
}
function getFooterConfig(contextRelativePath = '') {
return readContextualJsonFile('footer.json', contextRelativePath);
}
module.exports = {
getSidebarConfig,
getHeaderConfig,
getSiteConfig,
getTemplatesConfig,
getFooterConfig,
};