-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathindex.js
134 lines (121 loc) · 4.29 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const path = require('path');
const globby = require('globby');
const {getSubFolder, idx} = require('@docusaurus/utils');
const createOrder = require('./order');
const loadSidebars = require('./sidebars');
const processMetadata = require('./metadata');
async function loadDocs({siteDir, docsDir, env, siteConfig}) {
// @tested - load all sidebars including versioned sidebars
const docsSidebars = loadSidebars({siteDir, env});
// @tested - build the docs ordering such as next, previous, category and sidebar
const order = createOrder(docsSidebars);
// Settle versions & translations from environment.
const translationEnabled = idx(env, ['translation', 'enabled']);
const enabledLanguages =
translationEnabled && idx(env, ['translation', 'enabledLanguages']);
const enabledLangTags =
(enabledLanguages && enabledLanguages.map(lang => lang.tag)) || [];
const defaultLangTag = idx(env, ['translation', 'defaultLanguage', 'tag']);
const versioningEnabled = idx(env, ['versioning', 'enabled']);
const versions =
(versioningEnabled && idx(env, ['versioning', 'versions'])) || [];
// Prepare metadata container.
const docsMetadatas = {};
// Metadata for default docs files.
const docsFiles = await globby(['**/*.md'], {
cwd: docsDir,
});
await Promise.all(
docsFiles.map(async source => {
// Do not allow reserved version/ translated folder name in 'docs'
// e.g: 'docs/version-1.0.0/' should not be allowed as it can cause unwanted bug
const subFolder = getSubFolder(path.resolve(docsDir, source), docsDir);
const versionsFolders = versions.map(version => `version-${version}`);
if ([...enabledLangTags, ...versionsFolders].includes(subFolder)) {
throw new Error(`You cannot have a folder named 'docs/${subFolder}/'`);
}
const metadata = await processMetadata(
source,
docsDir,
env,
order,
siteConfig,
);
docsMetadatas[metadata.id] = metadata;
}),
);
// Metadata for non-default-language docs.
if (translationEnabled) {
const translatedDir = path.join(siteDir, 'translated_docs');
const translatedFiles = await globby(['**/*.md'], {
cwd: translatedDir,
});
await Promise.all(
translatedFiles.map(async source => {
/*
Do not process disabled & default languages folder in `translated_docs`
e.g: 'translated_docs/ja/**' should not be processed if lang 'ja' is disabled
*/
const translatedFilePath = path.resolve(translatedDir, source);
const detectedLangTag = getSubFolder(translatedFilePath, translatedDir);
if (
detectedLangTag === defaultLangTag ||
!enabledLangTags.includes(detectedLangTag)
) {
return;
}
const metadata = await processMetadata(
source,
translatedDir,
env,
order,
siteConfig,
);
docsMetadatas[metadata.id] = metadata;
}),
);
}
// Metadata for versioned docs.
if (versioningEnabled) {
const versionedDir = path.join(siteDir, 'versioned_docs');
const versionedFiles = await globby(['**/*.md'], {
cwd: versionedDir,
});
await Promise.all(
versionedFiles.map(async source => {
const metadata = await processMetadata(
source,
versionedDir,
env,
order,
siteConfig,
);
docsMetadatas[metadata.id] = metadata;
}),
);
}
// Get the titles of the previous and next ids so that we can use them.
Object.keys(docsMetadatas).forEach(currentID => {
const previousID = idx(docsMetadatas, [currentID, 'previous']);
if (previousID) {
const previousTitle = idx(docsMetadatas, [previousID, 'title']);
docsMetadatas[currentID].previous_title = previousTitle || 'Previous';
}
const nextID = idx(docsMetadatas, [currentID, 'next']);
if (nextID) {
const nextTitle = idx(docsMetadatas, [nextID, 'title']);
docsMetadatas[currentID].next_title = nextTitle || 'Next';
}
});
return {
docsSidebars,
docsMetadatas,
};
}
module.exports = loadDocs;