-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
125 lines (112 loc) · 3.65 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
import { CSSResult } from "lit";
import { getReadTheDocsConfig } from "./readthedocs-config";
import * as notification from "./notification";
import * as analytics from "./analytics";
import * as search from "./search";
import * as docdiff from "./docdiff";
import * as flyout from "./flyout";
import * as ethicalads from "./ethicalads";
import * as hotkeys from "./hotkeys";
import * as linkpreviews from "./linkpreviews";
import * as filetreediff from "./filetreediff";
import * as customscript from "./customscript";
import { default as objectPath } from "object-path";
import {
docTool,
domReady,
isEmbedded,
IS_PRODUCTION,
setupLogging,
getMetadataValue,
setupHistoryEvents,
} from "./utils";
import doctoolsStyleSheet from "./doctools.css";
export function setup() {
const addons = [
flyout.FlyoutAddon,
notification.NotificationAddon,
analytics.AnalyticsAddon,
ethicalads.EthicalAdsAddon,
search.SearchAddon,
// HotKeys & FileTreeDiff have to be initialized before DocDiff because when
// `?readthedocs-diff=true` DocDiff triggers an event that HotKeys has to
// listen to to update its internal state.
// https://github.com/readthedocs/addons/blob/47645b013724cdf244716b549a5baa28409fafcb/src/docdiff.js#L105-L111
hotkeys.HotKeysAddon,
filetreediff.FileTreeDiffAddon,
linkpreviews.LinkPreviewsAddon,
customscript.CustomScriptAddon,
docdiff.DocDiffAddon,
];
return new Promise((resolve) => {
domReady
.then(() => {
setupLogging();
setupHistoryEvents();
let sendUrlParam = false;
for (const addon of addons) {
if (addon.requiresUrlParam()) {
sendUrlParam = true;
break;
}
}
// Apply fixes to variables for individual documentation tools
const elementHtml = document.querySelector("html");
if (elementHtml) {
// Inject styles at the parent DOM to set variables at :root
let styleSheet = doctoolsStyleSheet;
if (doctoolsStyleSheet instanceof CSSResult) {
styleSheet = doctoolsStyleSheet.styleSheet;
}
document.adoptedStyleSheets = [styleSheet];
// If we detect a documentation tool, set attributes on :root to allow
// for CSS selectors to utilize these values.
if (docTool.documentationTool) {
elementHtml.setAttribute(
"data-readthedocs-tool",
docTool.documentationTool,
);
}
if (docTool.documentationTheme) {
elementHtml.setAttribute(
"data-readthedocs-tool-theme",
docTool.documentationTheme,
);
}
}
return getReadTheDocsConfig(sendUrlParam);
})
.then((config) => {
const loadWhenEmbedded = objectPath.get(
config,
"addons.options.load_when_embedded",
false,
);
if (isEmbedded() && !loadWhenEmbedded) {
return false;
}
const httpStatus = getMetadataValue("readthedocs-http-status");
let promises = [];
if (!IS_PRODUCTION) {
// Addons that are only available on development
console.log("Development mode.");
}
for (const addon of addons) {
if (addon.isEnabled(config, httpStatus)) {
promises.push(
new Promise((resolve) => {
return resolve(new addon(config));
}),
);
}
}
return Promise.all(promises);
})
.then(() => {
return resolve();
})
.catch((err) => {
console.error(err);
});
});
}