forked from bytebase/bytebase.com-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuxt.config.js
450 lines (408 loc) · 12.3 KB
/
nuxt.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import path from "path";
import fse from "fs-extra";
import slug from "slug";
import { camelCase, findLast, last } from "lodash";
import {
databaseFeatureList,
databaseVCSList,
databaseWebhookList,
} from "./common/matrix";
import { ALPHA_LIST } from "./common/glossary";
const VERSION = fse.readFileSync("VERSION").toString();
function getContentOfNode(node) {
if (node.type === "text") {
return node.value;
} else {
let content = "";
for (const child of node.children) {
content = content + getContentOfNode(child);
}
return content;
}
}
// unwantedRouteListForSitemap is the unwanted url(prefix) for sitemap.
const unwantedRouteListForSitemap = ["/zh/docs", "/zh/changelog", "/zh/blog"];
const generateSitemap = async (routes) => {
const baseUrl = "https://www.bytebase.com";
const routeXMLTagSet = new Set();
for (const route of routes) {
let isUnwantedRoute = false;
for (const item of unwantedRouteListForSitemap) {
if (route.startsWith(item)) {
isUnwantedRoute = true;
break;
}
}
if (isUnwantedRoute === false) {
routeXMLTagSet.add(`<url>
<loc>${baseUrl}${route}</loc>
</url>`);
}
}
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
${Array.from(routeXMLTagSet).join("\n")}
</urlset>`;
fse.appendFileSync("./dist/sitemap.xml", xml);
};
function glossaryRouteList() {
const list = [];
for (const alpha of ALPHA_LIST) {
for (const glossary of alpha.list) {
list.push(`/database-glossary/${slug(glossary.name)}`);
}
}
return list;
}
function databaseFeatureRouteList() {
const list = [];
for (const feature of databaseFeatureList()) {
list.push(`/database-feature/${feature.slug}`);
}
return list;
}
function databaseVCSRouteList() {
const list = [];
for (const feature of databaseVCSList()) {
list.push(`/vcs/${feature.slug}`);
}
return list;
}
function webhookRouteList() {
const list = [];
for (const webhook of databaseWebhookList()) {
list.push(`/webhook/${webhook.slug}`);
}
return list;
}
async function getChineseBlogRouteList() {
const { $content } = require("@nuxt/content");
const data = await $content("blog").fetch();
const list = [];
for (const item of data) {
if (item.tags.includes("Chinese")) {
list.push(`/blog/${item.slug}`);
}
}
return list;
}
function mergedLocalMessages(folder) {
const message = {};
const pathes = fse.readdirSync(folder);
for (const name of pathes) {
const fullpath = path.resolve(folder, name);
if (fse.statSync(fullpath).isFile() && /\.json$/.test(name)) {
const local = name.split(".")[0];
console.log(`reading localization file from fullpath: ${fullpath}`);
message[local] = {
...(message[local] || {}),
...fse.readJSONSync(fullpath),
};
} else if (fse.statSync(fullpath).isDirectory()) {
const nestedMessage = mergedLocalMessages(fullpath);
for (const [key, value] of Object.entries(nestedMessage)) {
console.log(`merge ${name}:${key} into localization message`);
const existed = message[key] || {};
message[key] = {
...existed,
[name]: {
...(existed[name] || {}),
...value,
},
};
}
}
}
return message;
}
export default {
// Target: https://go.nuxtjs.dev/config-target
target: "static",
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: "Bytebase | Database DevOps",
htmlAttrs: {
lang: "en",
},
meta: [
{ charset: "utf-8" },
{
name: "viewport",
content:
"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0",
},
{
hid: "description",
name: "description",
content:
"Safer and faster database change and version control for DBAs and Developers",
},
{ name: "format-detection", content: "telephone=no" },
],
link: [
{
rel: "icon",
type: "image/*",
href: "/favicon.ico",
},
{
rel: "preconnect",
href: "https://fonts.googleapis.com",
},
{
rel: "preconnect",
href: "https://fonts.gstatic.com",
crossOriginIsolated: true,
},
{
rel: "stylesheet",
href: "https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&family=Noto+Sans+SC:wght@100;300;400;500;700;900&display=swap",
},
],
},
router: {
linkActiveClass: "router-active-link underline",
linkExactActiveClass: "router-exact-active-link underline",
prefetchPayloads: false,
extendRoutes(routes, resolve) {
routes.push({
path: "/database-review-guide",
redirect: "/sql-review-guide",
});
},
},
content: {
dir: "content",
liveEdit: false,
},
i18n: {
defaultLocale: "en",
seo: true,
locales: [
{
name: "English",
code: "en",
iso: "en-US",
file: "en.json",
},
{
name: "简体中文",
code: "zh",
iso: "zh-CN",
file: "zh.json",
},
],
vueI18n: {
fallbackLocale: "en",
messages: mergedLocalMessages(path.resolve(__dirname, "./locales/")),
},
detectBrowserLanguage: {
useCookie: true,
cookieKey: "i18n_redirected",
redirectOn: "root",
},
pages: {
demo: {
en: false,
},
},
},
generate: {
routes: async () => {
return []
.concat(glossaryRouteList())
.concat(databaseFeatureRouteList())
.concat(databaseVCSRouteList())
.concat(webhookRouteList());
},
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: ["~/assets/css/variables.css", "~/assets/css/global.css"],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
// Plugin for vue-gtag
"~/plugin/vue-gtag",
],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
// https://go.nuxtjs.dev/typescript
"@nuxt/typescript-build",
// https://go.nuxtjs.dev/tailwindcss
"@nuxtjs/tailwindcss",
"@nuxtjs/composition-api/module",
"@pinia/nuxt",
"@nuxtjs/google-analytics",
"@nuxtjs/web-vitals",
],
// Modules: https://go.nuxtjs.dev/config-modules
modules: ["vue-plausible", "@nuxt/content", "@nuxtjs/i18n"],
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extend(config, ctx) {
config.module.rules.push({
test: /\.ya?ml$/,
loader: "yaml-loader",
});
},
},
plausible: {
// see configuration section
domain: "www.bytebase.com",
},
googleAnalytics: {
id: "UA-202806916-1",
},
env: {
// GA4 stream id. https://analytics.google.com/analytics/web/#/a202806916p295313050/admin/streams/table/3080936169
gtagKey: "G-4BZ4JH7449",
hostname: "https://www.bytebase.com",
},
// Using hooks to solve static prefix problem in dev server and built.
hooks: {
// redirect /static to / in dev server.
render: {
setupMiddleware(app) {
const staticPath = "/static";
app.use(staticPath, (req, res) => {
res.writeHead(302, {
location: req.originalUrl.slice(staticPath.length),
});
res.end();
});
},
},
// copy /static to ./dist/static in generation folder.
generate: {
async done(generator) {
// Generate `sitemap.xml` with chinese blogs.
const chineseBlogRouteList = await getChineseBlogRouteList();
generateSitemap(
Array.from(generator.generatedRoutes).concat(chineseBlogRouteList)
);
try {
// Patch docs index objects of algolia.
const { $content } = require("@nuxt/content");
const data = await $content("docs", {
deep: true,
})
.where({ slug: { $regex: /^(?!_)/ } })
.fetch();
const objects = [];
for (const item of data) {
const DOC_PATH_PREFIX = "/docs/en";
const path = item.path.slice(DOC_PATH_PREFIX.length);
const dataObject = {
objectID: path,
url: `/docs${path}`,
hierarchy: {
lvl0: "Documentation",
lvl1: item.title,
lvl2: null,
lvl3: null,
lvl4: null,
lvl5: null,
lvl6: null,
},
type: "lvl1",
};
objects.push(dataObject);
for (const node of item.body.children) {
if (
node.type === "element" &&
node.tag.length === 2 &&
node.tag.startsWith("h")
) {
const level = node.tag.slice(1);
const type = `lvl${level}`;
const title = getContentOfNode(node);
const dataObject = {
objectID: path + objects.length,
url: `/docs${path}#${node.props.id}`,
hierarchy: {},
type: type,
content: getContentOfNode(node),
};
const lastObject = findLast(
objects,
(o) => o.type === `lvl${level - 1}`
);
if (lastObject) {
dataObject.hierarchy = {
...lastObject.hierarchy,
};
}
dataObject.hierarchy[type] = title;
objects.push(dataObject);
} else {
const lastObject = last(objects);
if (lastObject && lastObject.type === "content") {
lastObject.content =
lastObject.content + getContentOfNode(node);
} else {
const dataObject = {
objectID: path + objects.length,
url: `/docs${path}`,
hierarchy: lastObject.hierarchy,
type: "content",
content: getContentOfNode(node),
};
objects.push(dataObject);
}
}
}
}
const algoliasearch = require("algoliasearch");
const client = algoliasearch(
"2M7XI1QIDY",
process.env.ALGOLIA_ADMIN_API_KEY
);
const index = client.initIndex("bytebase-docs");
await index.clearObjects();
await index.saveObjects(objects);
} catch (error) {
// We already have a complete data.
// So if failed in patch, then do nothing.
console.log("error", error);
}
console.log("Copying ./static folder to ./dist/static/");
try {
await fse.copy("./static", "./dist/static");
console.log("Copy succeed!");
} catch (error) {
console.error("Copy failed, err", error);
}
},
},
"content:file:beforeParse": (file) => {
if (file.extension === ".md" && file.path.includes("docs")) {
file.data = file.data.replace(/%%bb_version%%/g, VERSION);
}
},
"content:file:beforeInsert": (document) => {
if (document.extension === ".md") {
const removeMd = require("remove-markdown");
document.bodyPlainText = removeMd(document.text);
if (document.tags) {
document.tags = document.tags.split(", ");
} else {
const invalidTags = ["en", "zh"];
const rawTags = document.dir.split("/");
const tags = [];
for (const tag of rawTags) {
if (tag && !invalidTags.includes(tag)) {
tags.push(tag);
}
}
document.tags = tags;
}
for (const key of Object.keys(document)) {
if (key !== camelCase(key)) {
document[camelCase(key)] = document[key];
}
}
}
},
},
};