UNPKG

10.6 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4var __importDefault = (this && this.__importDefault) || function (mod) {
5 return (mod && mod.__esModule) ? mod : { "default": mod };
6};
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.PageConfig = void 0;
9const coreutils_1 = require("@lumino/coreutils");
10const minimist_1 = __importDefault(require("minimist"));
11const url_1 = require("./url");
12/**
13 * The namespace for `PageConfig` functions.
14 */
15var PageConfig;
16(function (PageConfig) {
17 /**
18 * Get global configuration data for the Jupyter application.
19 *
20 * @param name - The name of the configuration option.
21 *
22 * @returns The config value or an empty string if not found.
23 *
24 * #### Notes
25 * All values are treated as strings.
26 * For browser based applications, it is assumed that the page HTML
27 * includes a script tag with the id `jupyter-config-data` containing the
28 * configuration as valid JSON. In order to support the classic Notebook,
29 * we fall back on checking for `body` data of the given `name`.
30 *
31 * For node applications, it is assumed that the process was launched
32 * with a `--jupyter-config-data` option pointing to a JSON settings
33 * file.
34 */
35 function getOption(name) {
36 if (configData) {
37 return configData[name] || getBodyData(name);
38 }
39 configData = Object.create(null);
40 let found = false;
41 // Use script tag if available.
42 if (typeof document !== 'undefined' && document) {
43 const el = document.getElementById('jupyter-config-data');
44 if (el) {
45 configData = JSON.parse(el.textContent || '');
46 found = true;
47 }
48 }
49 // Otherwise use CLI if given.
50 if (!found && typeof process !== 'undefined' && process.argv) {
51 try {
52 const cli = (0, minimist_1.default)(process.argv.slice(2));
53 const path = require('path');
54 let fullPath = '';
55 if ('jupyter-config-data' in cli) {
56 fullPath = path.resolve(cli['jupyter-config-data']);
57 }
58 else if ('JUPYTER_CONFIG_DATA' in process.env) {
59 fullPath = path.resolve(process.env['JUPYTER_CONFIG_DATA']);
60 }
61 if (fullPath) {
62 // Force Webpack to ignore this require.
63 // eslint-disable-next-line
64 configData = eval('require')(fullPath);
65 }
66 }
67 catch (e) {
68 console.error(e);
69 }
70 }
71 if (!coreutils_1.JSONExt.isObject(configData)) {
72 configData = Object.create(null);
73 }
74 else {
75 for (const key in configData) {
76 // PageConfig expects strings
77 if (typeof configData[key] !== 'string') {
78 configData[key] = JSON.stringify(configData[key]);
79 }
80 }
81 }
82 return configData[name] || getBodyData(name);
83 }
84 PageConfig.getOption = getOption;
85 /**
86 * Set global configuration data for the Jupyter application.
87 *
88 * @param name - The name of the configuration option.
89 * @param value - The value to set the option to.
90 *
91 * @returns The last config value or an empty string if it doesn't exist.
92 */
93 function setOption(name, value) {
94 const last = getOption(name);
95 configData[name] = value;
96 return last;
97 }
98 PageConfig.setOption = setOption;
99 /**
100 * Get the base url for a Jupyter application, or the base url of the page.
101 */
102 function getBaseUrl() {
103 return url_1.URLExt.normalize(getOption('baseUrl') || '/');
104 }
105 PageConfig.getBaseUrl = getBaseUrl;
106 /**
107 * Get the tree url for a JupyterLab application.
108 */
109 function getTreeUrl() {
110 return url_1.URLExt.join(getBaseUrl(), getOption('treeUrl'));
111 }
112 PageConfig.getTreeUrl = getTreeUrl;
113 /**
114 * Get the base url for sharing links (usually baseUrl)
115 */
116 function getShareUrl() {
117 return url_1.URLExt.normalize(getOption('shareUrl') || getBaseUrl());
118 }
119 PageConfig.getShareUrl = getShareUrl;
120 /**
121 * Get the tree url for shareable links.
122 * Usually the same as treeUrl,
123 * but overrideable e.g. when sharing with JupyterHub.
124 */
125 function getTreeShareUrl() {
126 return url_1.URLExt.normalize(url_1.URLExt.join(getShareUrl(), getOption('treeUrl')));
127 }
128 PageConfig.getTreeShareUrl = getTreeShareUrl;
129 /**
130 * Create a new URL given an optional mode and tree path.
131 *
132 * This is used to create URLS when the mode or tree path change as the user
133 * changes mode or the current document in the main area. If fields in
134 * options are omitted, the value in PageConfig will be used.
135 *
136 * @param options - IGetUrlOptions for the new path.
137 */
138 function getUrl(options) {
139 var _a, _b, _c, _d;
140 let path = options.toShare ? getShareUrl() : getBaseUrl();
141 const mode = (_a = options.mode) !== null && _a !== void 0 ? _a : getOption('mode');
142 const workspace = (_b = options.workspace) !== null && _b !== void 0 ? _b : getOption('workspace');
143 const labOrDoc = mode === 'single-document' ? 'doc' : 'lab';
144 path = url_1.URLExt.join(path, labOrDoc);
145 if (workspace !== PageConfig.defaultWorkspace) {
146 path = url_1.URLExt.join(path, 'workspaces', encodeURIComponent((_c = getOption('workspace')) !== null && _c !== void 0 ? _c : PageConfig.defaultWorkspace));
147 }
148 const treePath = (_d = options.treePath) !== null && _d !== void 0 ? _d : getOption('treePath');
149 if (treePath) {
150 path = url_1.URLExt.join(path, 'tree', url_1.URLExt.encodeParts(treePath));
151 }
152 return path;
153 }
154 PageConfig.getUrl = getUrl;
155 PageConfig.defaultWorkspace = 'default';
156 /**
157 * Get the base websocket url for a Jupyter application, or an empty string.
158 */
159 function getWsUrl(baseUrl) {
160 let wsUrl = getOption('wsUrl');
161 if (!wsUrl) {
162 baseUrl = baseUrl ? url_1.URLExt.normalize(baseUrl) : getBaseUrl();
163 if (baseUrl.indexOf('http') !== 0) {
164 return '';
165 }
166 wsUrl = 'ws' + baseUrl.slice(4);
167 }
168 return url_1.URLExt.normalize(wsUrl);
169 }
170 PageConfig.getWsUrl = getWsUrl;
171 /**
172 * Returns the URL converting this notebook to a certain
173 * format with nbconvert.
174 */
175 function getNBConvertURL({ path, format, download }) {
176 const notebookPath = url_1.URLExt.encodeParts(path);
177 const url = url_1.URLExt.join(getBaseUrl(), 'nbconvert', format, notebookPath);
178 if (download) {
179 return url + '?download=true';
180 }
181 return url;
182 }
183 PageConfig.getNBConvertURL = getNBConvertURL;
184 /**
185 * Get the authorization token for a Jupyter application.
186 */
187 function getToken() {
188 return getOption('token') || getBodyData('jupyterApiToken');
189 }
190 PageConfig.getToken = getToken;
191 /**
192 * Get the Notebook version info [major, minor, patch].
193 */
194 function getNotebookVersion() {
195 const notebookVersion = getOption('notebookVersion');
196 if (notebookVersion === '') {
197 return [0, 0, 0];
198 }
199 return JSON.parse(notebookVersion);
200 }
201 PageConfig.getNotebookVersion = getNotebookVersion;
202 /**
203 * Private page config data for the Jupyter application.
204 */
205 let configData = null;
206 /**
207 * Get a url-encoded item from `body.data` and decode it
208 * We should never have any encoded URLs anywhere else in code
209 * until we are building an actual request.
210 */
211 function getBodyData(key) {
212 if (typeof document === 'undefined' || !document.body) {
213 return '';
214 }
215 const val = document.body.dataset[key];
216 if (typeof val === 'undefined') {
217 return '';
218 }
219 return decodeURIComponent(val);
220 }
221 /**
222 * The namespace for page config `Extension` functions.
223 */
224 let Extension;
225 (function (Extension) {
226 /**
227 * Populate an array from page config.
228 *
229 * @param key - The page config key (e.g., `deferredExtensions`).
230 *
231 * #### Notes
232 * This is intended for `deferredExtensions` and `disabledExtensions`.
233 */
234 function populate(key) {
235 try {
236 const raw = getOption(key);
237 if (raw) {
238 return JSON.parse(raw);
239 }
240 }
241 catch (error) {
242 console.warn(`Unable to parse ${key}.`, error);
243 }
244 return [];
245 }
246 /**
247 * The collection of deferred extensions in page config.
248 */
249 Extension.deferred = populate('deferredExtensions');
250 /**
251 * The collection of disabled extensions in page config.
252 */
253 Extension.disabled = populate('disabledExtensions');
254 /**
255 * Returns whether a plugin is deferred.
256 *
257 * @param id - The plugin ID.
258 */
259 function isDeferred(id) {
260 // Check for either a full plugin id match or an extension
261 // name match.
262 const separatorIndex = id.indexOf(':');
263 let extName = '';
264 if (separatorIndex !== -1) {
265 extName = id.slice(0, separatorIndex);
266 }
267 return Extension.deferred.some(val => val === id || (extName && val === extName));
268 }
269 Extension.isDeferred = isDeferred;
270 /**
271 * Returns whether a plugin is disabled.
272 *
273 * @param id - The plugin ID.
274 */
275 function isDisabled(id) {
276 // Check for either a full plugin id match or an extension
277 // name match.
278 const separatorIndex = id.indexOf(':');
279 let extName = '';
280 if (separatorIndex !== -1) {
281 extName = id.slice(0, separatorIndex);
282 }
283 return Extension.disabled.some(val => val === id || (extName && val === extName));
284 }
285 Extension.isDisabled = isDisabled;
286 })(Extension = PageConfig.Extension || (PageConfig.Extension = {}));
287})(PageConfig || (exports.PageConfig = PageConfig = {}));
288//# sourceMappingURL=pageconfig.js.map
\No newline at end of file