This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
279 lines (242 loc) · 11.2 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
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
// Mods
import _ from 'lodash';
import Debug from 'debug';
import chokidar from 'chokidar';
// vuepress things
import {chalk, getDirname, logger, path} from '@vuepress/utils';
import {defaultTheme} from '@vuepress/theme-default';
// vuepress core plugins
import {googleAnalyticsPlugin} from '@vuepress/plugin-google-analytics';
import {registerComponentsPlugin} from '@vuepress/plugin-register-components';
import {searchPlugin} from '@vuepress/plugin-search';
// our plugins
import {alertPlugin} from './plugins/plugin-alert/index.js';
import {autometaPlugin} from './plugins/plugin-autometa/index.js';
import {contributorsPagePlugin} from './plugins/plugin-contributors-page/index.js';
import {docSearchPlusPlugin} from './plugins/plugin-docsearch-plus/index.js';
import {fauxInternalPlugin} from './plugins/plugin-faux-internal/index.js';
import {getPlugins} from './lib/plugins.js';
import {hubspotPlugin} from './plugins/plugin-hubspot-tracking/index.js';
import {readModePlugin} from './plugins/plugin-read-mode/index.js';
import {robotsTxtPlugin} from './plugins/plugin-robots/index.js';
import {sidebarHeaderPlugin} from './plugins/plugin-sidebar-header/index.js';
import {simpleTagsPlugin} from './plugins/plugin-simple-tags/index.js';
import {siteMapPlugin} from './plugins/plugin-sitemap/index.js';
import {versionsPagePlugin} from './plugins/plugin-versions-page/index.js';
// our defaults
import themeDefaults from './config/defaults.js';
import themeLandoV3Defaults from './config/landov3.js';
import themeLandoV4Defaults from './config/landov4.js';
const __dirname = getDirname(import.meta.url);
export const defaultThemePlus = options => {
const debug = Debug('@lando/vuepress-theme-default-plus'); // eslint-disable-line
// prefer landov4 if defaults not set
if (_.isEmpty(options.defaults) && options.landoDocs === 4) {
debug('no user defaults set, using lando v4 defaults');
options.defaults = themeLandoV4Defaults;
// ditto but for lando v3
} else if (_.isEmpty(options.defaults) && options.landoDocs === 3) {
debug('no user defaults set, using lando v3 defaults');
options.defaults = themeLandoV3Defaults;
// Same as above but for legacy things
} else if (_.isEmpty(options.defaults) && (options.landoDocs || options.lando)) {
debug('no user defaults set, using lando v3 defaults');
options.defaults = themeLandoV3Defaults;
// Otherwise if we are empty then just set to defaults
} else if (_.isEmpty(options.defaults)) {
debug('no user defaults set, using theme defaults');
options.defaults = themeDefaults;
}
// Rebase options on defaults
options = _.merge({}, options.defaults, options);
delete options.defaults;
// Remove defaults so its less confusing
debug('merging user config over defaults, result: %O', options);
// We want to preserve the value of options.repo but we do not want to set it because it will show up
// in the nav if we do
options.sourceRepo = options.repo;
delete options.repo;
debug('removed repo and set sourceRepo to %s', options.sourceRepo);
// SHARED NAVBAR
// If we want to show the shared navbar then lets add it to the begining of the navbar
if (!_.isNil(options.sharedNavbar) && _.isArray(options.sharedNavbar)) {
options.navbar = options.sharedNavbar.concat(options.navbar);
debug('prepended shared navbar to user specified navbar with %o', options.sharedNavbar);
}
// ALWAYS ON PLUGINS
const plugins = getPlugins(options);
debug('loaded always on plugins %o', _.map(plugins, plugin => plugin.name));
// GOOGLE ANALYTICS PLUGIN
if (options.ga) {
plugins.push(googleAnalyticsPlugin(options.ga));
debug('loaded google analytics plugin with config %o', options.ga);
}
// HUBSPOT TRACKING PLUGIN
if (options.hubspot) {
plugins.push(hubspotPlugin(options.hubspot));
debug('loaded hubspot tracking plugin with config %o', options.hubspot);
}
// AUTOMETA PLUGIN
if (options.autometa) {
plugins.push(autometaPlugin(options.autometa));
debug('loaded autometa plugin with config: %o', options.autometa);
}
// FAUX INTERNAL LINKS PLUGIN
if (options.baseUrl) {
plugins.push(fauxInternalPlugin(options));
debug('loaded faux internal plugin with baseurl: %o', options.baseUrl);
}
// SEARCH PLUGIN
if (options.search) {
// Use advanced search
if (options.search.apiKey && options.search.indexName) {
options.search.searchBase = options.search.searchBase || options.baseUrl;
plugins.push(docSearchPlusPlugin(options.search));
debug('loaded docsearch plus plugin with config: %o', options.search);
// Use default search
} else {
plugins.push(searchPlugin());
debug('loaded core search plus');
}
}
// REGISTER PAGE TYPE COMPONENTS
if (options.pageTypes) {
const entries = new Map(options.pageTypes.map(page => ([page.name, page.path])));
const components = Object.fromEntries(entries);
debug('registered page type components: %o', components);
plugins.push(registerComponentsPlugin({components}));
}
// ROBOTS.TXT PLUGIN
if (options.robots) {
options.robots.host = options.robots.host || options.baseUrl || options.autometa.canonicalUrl;
if (options.sitemap && options.robots.host) {
options.robots.sitemap = `${new URL(options.robots.host).origin}/sitemap.xml`;
}
plugins.push(robotsTxtPlugin(options.robots));
debug('loaded robots.txt plugin with config: %o', options.robots);
}
// SITEMAP PLUGIN
if (options.sitemap) {
if (options.sitemap === true) options.sitemap = {};
options.sitemap.baseUrl = options.sitemap.baseUrl || options.autometa.canonicalUrl || options.baseUrl;
plugins.push(siteMapPlugin(options.sitemap));
debug('loaded sitemap plugin with config: %o', options.sitemap);
}
// SIDEBAR HEADER PLUGIN
if (options.sidebarHeader) {
options.sidebarHeader.repo = options.sidebarHeader.repo || options.sourceRepo;
options.sidebarHeader.auto = true;
plugins.push(sidebarHeaderPlugin(options.sidebarHeader));
debug('loaded sidebar header plugin with config: %o', options.sidebarHeader);
}
// READ MODE PLUGIN
if (options.readMode) {
plugins.push(readModePlugin(options.readMode));
debug('loaded read mode plugin with config: %o', options.readMode);
}
// VERSIONS PAGE PLUGIN
if (options.versionsPage) {
options.versionsPage.repo = options.versionsPage.repo || options.sourceRepo;
options.versionsPage.docsDir = options.versionsPage.docsDir || options.docsDir;
options.versionsPage.docsBranch = options.versionsPage.docsBranch || options.docsBranch;
plugins.push(versionsPagePlugin(options.versionsPage, options.sidebar));
debug('loaded versions page plugin with config: %o', options.versionsPage);
// globally add the Version list component
plugins.push(registerComponentsPlugin({
components: {
VersionsList: path.resolve(__dirname, 'plugins', 'plugin-versions-page', 'VersionsList.vue'),
},
}));
}
// CONTRIBTUORS PAGE PLUGIN
if (options.contributorsPage) {
options.contributorsPage.repo = options.contributorsPage.repo || options.sourceRepo;
options.contributorsPage.docsDir = options.contributorsPage.docsDir || options.docsDir;
options.contributorsPage.docsBranch = options.contributorsPage.docsBranch || options.docsBranch;
plugins.push(contributorsPagePlugin(options.contributorsPage, options.sidebar));
debug('loaded contributors page plugin with config: %o', options.contributorsPage);
// globally add the Version list component
plugins.push(registerComponentsPlugin({
components: {
ContributorList: path.resolve(__dirname, 'plugins', 'plugin-contributors-page', 'ContributorList.vue'),
},
}));
}
// SIMPLE TAGS PLUGIN
if (options.tags) {
plugins.push(simpleTagsPlugin(options.tags));
debug('loaded simple tags plugin with config: %o', options.tags);
plugins.push(registerComponentsPlugin({
components: {
TagPage: path.resolve(__dirname, 'plugins', 'plugin-simple-tags', 'TagPage.vue'),
},
}));
}
// ALERT PLUGIN
if (options.alert) {
plugins.push(alertPlugin(options.alert));
debug('loaded alert plugin with config: %o', options.alert);
}
return {
name: '@lando/vuepress-theme-default-plus',
extends: defaultTheme(options),
clientConfigFile: path.resolve(__dirname, 'client.js'),
alias: {
...{
// override defaults
'@theme/NavbarBrand.vue': path.resolve(__dirname, 'components', 'CustomNavbarBrand.vue'),
'@theme/NavbarDropdown.vue': path.resolve(__dirname, 'components', 'CustomNavbarDropdown.vue'),
'@theme/PageMeta.vue': path.resolve(__dirname, 'components', 'CustomPageMeta.vue'),
// make overideable
'@theme/BlogHeader.vue': path.resolve(__dirname, 'components', 'BlogHeader.vue'),
'@theme/BlogPost.vue': path.resolve(__dirname, 'components', 'BlogPost.vue'),
'@theme/Guide.vue': path.resolve(__dirname, 'components', 'Guide.vue'),
'@theme/GuideHeader.vue': path.resolve(__dirname, 'components', 'GuideHeader.vue'),
'@theme/CarbonAds.vue': path.resolve(__dirname, 'components', 'CarbonAds.vue'),
'@theme/SocialLinks.vue': path.resolve(__dirname, 'components', 'SocialLinks.vue'),
'@theme/TOC.vue': path.resolve(__dirname, 'components', 'TOC.vue'),
// override overrides
'@theme/CustomNavbarBrand.vue': path.resolve(__dirname, 'components', 'CustomNavbarBrand.vue'),
'@theme/CustomPageMeta.vue': path.resolve(__dirname, 'components', 'CustomPageMeta.vue'),
// @NOTE: special we override the plugin alias because this is requried in our layout regardless of whether the plugin
// loads or not
'@theme/Alert.vue': path.resolve(__dirname, 'plugins', 'plugin-alert', 'Alert.vue'),
'@theme/ReadMode.vue': path.resolve(__dirname, 'plugins', 'plugin-read-mode', 'ReadMode.vue'),
'@theme/SidebarHeader.vue': path.resolve(__dirname, 'plugins', 'plugin-sidebar-header', 'SidebarHeader.vue'),
'@theme/TagList.vue': path.resolve(__dirname, 'plugins', 'plugin-simple-tags', 'TagList.vue'),
},
...options.alias,
},
define: {
__THEME_OPTIONS__: options,
},
// all our plugins
plugins,
// we need to make sure that blueimp is included in dep optimization because it is commonjs
extendsBundlerOptions: (bundlerOptions, app) => {
// extends options of @vuepress/bundler-vite
if (app.options.bundler.name === '@vuepress/bundler-vite') {
const path = 'viteOptions.optimizeDeps.include';
const includes = _.get(bundlerOptions, path, []);
includes.push('blueimp-md5');
_.set(bundlerOptions, path, _.uniq(includes));
debug('modified vite to include blueimp-md5, includes are now %j', _.get(bundlerOptions, path));
}
},
// Watch our plugin dir for changes as well
async onWatched(app, watchers, restart) {
const cwd = process.cwd();
const dirs = [
path.resolve(__dirname, 'config'),
path.resolve(__dirname, 'lib'),
path.resolve(__dirname, 'plugins'),
];
const watcher = chokidar.watch(dirs, {cwd, ignoreInitial: true});
watcher.on('change', file => {
logger.info(`config ${chalk.magenta(file)} is modified`);
restart();
});
watchers.push(watcher);
},
};
};