-
Notifications
You must be signed in to change notification settings - Fork 0
/
docusaurus.config.js
401 lines (362 loc) · 10.9 KB
/
docusaurus.config.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// @ts-check
// `@type` JSDoc annotations allow editor autocompletion and type checking
// (when paired with `@ts-check`).
// There are various equivalent ways to declare your Docusaurus config.
// See: https://docusaurus.io/docs/api/docusaurus-config
import {themes as prismThemes} from 'prism-react-renderer';
import fs from 'fs';
import path from 'path';
import dotenv from "dotenv";
// const languages = ['es', 'en', 'fr', 'de', 'pt', 'it', 'ja']
dotenv.config();
function getJson(file) {
const configJsonPath = path.join(__dirname, file);
const data = fs.readFileSync(configJsonPath, 'utf8');
return JSON.parse(data);
}
const configurations = getJson('config.json');
const planConfig = getJson('plan.json');
function retrieveCustomDomain() {
try {
const customDomain = process.env.CUSTOM_DOMAIN;
if (!customDomain) return 'https://docs.writedocs.io';
return `https://${customDomain}`;
} catch (error) {
return 'https://docs.writedocs.io';
}
}
function loadGtag() {
try {
const gtag = process.env.GTAG;
if (gtag) {
return {
gtag: {
trackingID: gtag,
anonymizeIP: true,
}
};
}
if (configurations.integrations.gtag) {
return {
gtag: {
trackingID: configurations.integrations.gtag,
anonymizeIP: true,
}
};
}
} catch (error) {
}
}
function getFirstPageFromJson(sectionName) {
try {
const jsonData = configurations.sidebars;
const section = jsonData.find(({ sidebarRef }) => sidebarRef === sectionName);
if (section) {
const firstCategory = section.categories[0];
if (firstCategory) {
const firstPage = firstCategory.pages[0];
if (firstPage) {
if (typeof firstPage === 'string') {
return firstPage;
} else if (typeof firstPage === 'object' && firstPage.page) {
return firstPage.page;
} else if (typeof firstPage === 'object') {
return firstPage.pages[0]
}
}
}
}
return null;
} catch (error) {
console.error('Error reading or parsing JSON file:', error);
return null;
}
}
function createOpenApiConfig() {
const fileNames = configurations.apiFiles;
const directoryPath = 'openAPI';
const proxyUrl = 'https://proxy.writechoice.io/';
const outputBaseDir = 'docs/reference';
const normalizedFileNames = fileNames.map(fileName => {
// If the path doesn't start with "openapi/", add the directoryPath prefix
if (!fileName.startsWith(`${directoryPath}/`)) {
return path.join(directoryPath, fileName);
}
return fileName;
});
function getAllFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
fileList = getAllFiles(filePath, fileList);
} else {
fileList.push(filePath);
}
});
return fileList;
}
const allFiles = getAllFiles(directoryPath);
const validFiles = normalizedFileNames.filter((file) =>
allFiles.includes(file)
);
const config = validFiles.reduce((acc, file) => {
const fileName = path.parse(file).name;
const specPath = file;
const relativePath = path.relative(directoryPath, path.dirname(file));
const outputDir = path.join(outputBaseDir, relativePath, fileName.replace('_', '-'));
const keyName = relativePath && relativePath !== '.'
? `${relativePath}-${fileName}`
: fileName;
acc[keyName] = {
specPath,
proxy: proxyUrl,
outputDir,
};
return acc;
}, {});
return {
id: 'openapi',
docsPluginId: 'classic',
config,
};
}
function createNavigationArray() {
const { navbar, externalLinks, languages } = configurations;
const { plan } = planConfig;
const navigationArray = [];
const navbarWithIcons = ['guides', 'api reference', 'changelog'];
const navbarNames = navbar.map((item) => item.label.toLowerCase());
const allItemsIncluded = navbarNames.every((name) => navbarWithIcons.includes(name));
navigationArray.push({
to: '/',
label: 'Home',
position: 'left',
className: `home_btn ${!configurations.homepage.endsWith(".html") && 'hide_home_btn'}`,
});
if (plan === 'free') {
navigationArray.push(
{
type: 'docSidebar',
position: 'left',
sidebarId: 'apiReference',
className: 'apireference_btn',
label: 'API Reference',
}
);
return navigationArray;
}
for (let index in navbar) {
if (navbar[index].sidebarRef) {
if (navbar[index].sidebarRef === 'docs') {
navigationArray.push({
type: 'doc',
position: 'left',
label: navbar[index].label,
className: allItemsIncluded ? `${navbar[index].label.toLowerCase()}_btn` : 'btn',
docId: getFirstPageFromJson(navbar[index].sidebarRef),
});
} else {
navigationArray.push({
type: 'docSidebar',
position: 'left',
sidebarId: navbar[index].sidebarRef,
className: allItemsIncluded ? `${navbar[index].sidebarRef.toLowerCase()}_btn` : 'btn',
label: navbar[index].label,
});
}
} else if (navbar[index].dropdown) {
const dropdown = [];
navbar[index].dropdown.forEach(({label, sidebarRef}) => {
dropdown.push({
type: 'doc',
label: label,
docId: getFirstPageFromJson(sidebarRef),
})
});
navigationArray.push({
type: 'dropdown',
label: navbar[index].label,
position: 'left',
items: dropdown
})
}
}
if (configurations.changelog) {
navigationArray.push(
{to: 'changelog', label: 'Changelog', position: 'left', className: allItemsIncluded ? 'changelog_btn' : 'btn' },
);
}
if (externalLinks) {
externalLinks.slice(0, 2).forEach(({ link, name }) => {
const item = {
to: link,
position: 'right',
className: 'wd_navbar_link_btn',
label: name
};
navigationArray.push(item);
})
}
if (languages && languages.length > 1) {
navigationArray.push({
type: 'localeDropdown',
position: 'right',
className: 'language_dropdown'
})
}
navigationArray.push({ type: 'search', position: 'right' });
return navigationArray;
}
function defineColorScheme() {
try {
const { colorMode } = configurations;
const { default: defaultMode, switchOff } = colorMode;
return {
respectPrefersColorScheme: false,
defaultMode: defaultMode ? defaultMode : 'light',
disableSwitch: switchOff ? true : false
}
} catch (error) {
return {
respectPrefersColorScheme: false,
defaultMode: 'light',
disableSwitch: false
}
}
}
/** @type {import('@docusaurus/types').Config} */
const config = {
title: configurations.websiteName,
tagline: configurations.description,
favicon: configurations.images.favicon,
// Set the production url of your site here
url: retrieveCustomDomain(),
// Set the /<baseUrl>/ pathname under which your site is served
// For GitHub pages deployment, it is often '/<projectName>/'
baseUrl: '/',
onBrokenLinks: 'warn',
onBrokenMarkdownLinks: 'warn',
// Even if you don't use internationalization, you can use this field to set
// useful metadata like html lang. For example, if your site is Chinese, you
// may want to replace "en" with "zh-Hans".
i18n: {
defaultLocale: configurations.languages ? configurations.languages[0] : "en",
locales: configurations.languages && configurations.languages.length > 0 ? configurations.languages : ["en"],
path: 'i18n',
},
presets: [
[
'classic',
/** @type {import('@docusaurus/preset-classic').Options} */
({
docs: {
routeBasePath: '/',
sidebarPath: require.resolve('./sidebars.js'),
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
// editUrl: 'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/',
docItemComponent: "@theme/ApiItem",
},
blog: false,
theme: {
customCss: './src/css/custom.css',
},
...loadGtag()
}),
],
],
themeConfig:
/** @type {import('@docusaurus/preset-classic').ThemeConfig} */
({
colorMode: defineColorScheme(),
metadata: [
{ name: 'og:site_name', content: configurations.websiteName || "" },
{ name: 'og:title', content: configurations.websiteName || "" },
{ name: 'og:description', content: configurations.description || "" },
{ name: 'description', content: configurations.description || "" },
],
docs: {
sidebar: {
// hideable: true,
autoCollapseCategories: true,
},
},
zoom: {
selector: '.markdown :not(em) > img:not(.no_zoom)',
background: {
light: 'rgb(255, 255, 255,0.9)',
dark: 'rgb(50, 50, 50)',
},
config: {
// options you can specify via https://github.com/francoischalifour/medium-zoom#usage
margin: 80,
scrollOffset: 50,
},
},
image: configurations.images.metadata || configurations.images.logo,
navbar: {
title: configurations.websiteName || "",
logo: {
alt: `${configurations.websiteName} logo`,
src: configurations.images.logo,
},
items: createNavigationArray(),
},
navbarBreakpoint: '1060px',
footer: {
style: 'dark',
copyright: `Copyright © ${new Date().getFullYear()} | ${configurations.websiteName}`,
},
prism: {
theme: prismThemes.oceanicNext,
darkTheme: prismThemes.oceanicNext,
additionalLanguages: [
"ruby",
"csharp",
"php",
"java",
"powershell",
"json",
"bash",
"yaml",
],
},
}),
plugins: [
'docusaurus-plugin-image-zoom',
[ require.resolve('docusaurus-lunr-search'), {
maxHits: '7',
highlightResult: 'true'
}],
[
"docusaurus-plugin-openapi-docs",
createOpenApiConfig()
],
[
'@docusaurus/plugin-content-blog',
{
id: 'changelog',
routeBasePath: 'changelog',
path: './changelog',
blogSidebarCount: 'ALL',
blogSidebarTitle: 'Changelog',
showReadingTime: false,
blogTitle: 'Changelog',
blogDescription: 'Changelog',
},
],
],
themes: ["docusaurus-theme-openapi-docs"],
future: {
experimental_faster: {
swcJsLoader: true,
swcJsMinimizer: true,
swcHtmlMinimizer: true,
lightningCssMinimizer: true,
rspackBundler: false,
mdxCrossCompilerCache: true,
},
},
};
export default config;