Skip to content

Commit 1430477

Browse files
committed
vitepress: Centralize loading of local.js config file
1 parent df678ef commit 1430477

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

lib/utility.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { fileURLToPath } from 'url';
1010
const __filename = fileURLToPath(import.meta.url);
1111
const __dirname = dirname(__filename);
1212

13-
const local_conf = __dirname + '/../.vitepress/local.js'
13+
const local_conf_path = __dirname + '/../.vitepress/local.js'
1414

1515
export function normalizeArrayData(data, keys) {
1616
for (const [k, v] of Object.entries(data)) {
@@ -29,15 +29,26 @@ export function normalizeArrayData(data, keys) {
2929
return data
3030
}
3131

32+
let local_conf = null
33+
async function loadLocalConf() {
34+
if (local_conf === null) {
35+
if (fs.existsSync(local_conf_path)) {
36+
local_conf = await import(local_conf_path)
37+
} else {
38+
local_conf = false
39+
}
40+
}
41+
42+
return local_conf
43+
}
44+
3245
export async function loadData(id) {
3346
let path = '../data/' + id + '.js'
3447

3548
/* Check for config override file. */
36-
if (fs.existsSync(local_conf)) {
37-
const data = await import(local_conf)
38-
if (data.data_paths[id]) {
39-
path = data.data_paths[id]
40-
}
49+
const lconf = await loadLocalConf()
50+
if (lconf && lconf.data_paths[id]) {
51+
path = lconf.data_paths[id]
4152
}
4253

4354
try {
@@ -50,47 +61,39 @@ export async function loadData(id) {
5061

5162
export async function sourceFiles() {
5263
/* Check for config override file. */
53-
if (fs.existsSync(local_conf)) {
54-
const data = await import(local_conf)
55-
if (data.source_paths) {
56-
return data.source_paths
57-
}
64+
const lconf = await loadLocalConf()
65+
if (lconf && lconf.source_paths) {
66+
return lconf.source_paths
5867
}
5968

6069
return [ 'docs/**/*.md' ]
6170
}
6271

6372
export async function watchFiles() {
6473
/* Check for config override file. */
65-
if (fs.existsSync(local_conf)) {
66-
const data = await import(local_conf)
67-
if (data.watch_paths) {
68-
return data.watch_paths
69-
}
74+
const lconf = await loadLocalConf()
75+
if (lconf && lconf.watch_paths) {
76+
return lconf.watch_paths
7077
}
7178

7279
return [ 'docs/**/*.md', 'docs/**/*.inc', 'data/**/*' ]
7380
}
7481

7582
export async function manFiles() {
7683
/* Check for config override file. */
77-
if (fs.existsSync(local_conf)) {
78-
const data = await import(local_conf)
79-
if (data.man_paths) {
80-
return data.man_paths
81-
}
84+
const lconf = await loadLocalConf()
85+
if (lconf && lconf.man_paths) {
86+
return lconf.man_paths
8287
}
8388

8489
return [ 'docs/core/man/*.[[:digit:]].md' ]
8590
}
8691

8792
export async function pluginFiles() {
8893
/* Check for config override file. */
89-
if (fs.existsSync(local_conf)) {
90-
const data = await import(local_conf)
91-
if (data.plugin_paths) {
92-
return data.plugin_paths
93-
}
94+
const lconf = await loadLocalConf()
95+
if (lconf && lconf.plugin_paths) {
96+
return lconf.plugin_paths
9497
}
9598

9699
return [ 'docs/core/plugins/*.md' ]
@@ -102,11 +105,10 @@ export async function frontmatterIter(callback) {
102105
let spt = []
103106
let spt_conf = { 'docs/:path(.*)': ':path' }
104107

105-
if (fs.existsSync(local_conf)) {
106-
const data = await import(local_conf)
107-
if (data.source_path_translations) {
108-
spt_conf = data.source_path_translations
109-
}
108+
/* Check for config override file. */
109+
const lconf = await loadLocalConf()
110+
if (lconf && lconf.source_path_translations) {
111+
spt_conf = lconf.source_path_translations
110112
}
111113

112114
spt = Object.entries(spt_conf).map(([from, to]) => ({

0 commit comments

Comments
 (0)