Skip to content

Commit

Permalink
Merge pull request #7216 from afshin/pageconfig
Browse files Browse the repository at this point in the history
Add PageConfig.Extension
  • Loading branch information
blink1073 committed Sep 16, 2019
2 parents 8eb3df9 + 1a0ffb5 commit 15d0c23
Showing 1 changed file with 60 additions and 10 deletions.
70 changes: 60 additions & 10 deletions packages/coreutils/src/pageconfig.ts
Expand Up @@ -14,7 +14,7 @@ declare var process: any;
declare var require: any;

/**
* The namespace for Page Config functions.
* The namespace for `PageConfig` functions.
*/
export namespace PageConfig {
/**
Expand All @@ -37,7 +37,7 @@ export namespace PageConfig {
*/
export function getOption(name: string): string {
if (configData) {
return configData[name] || Private.getBodyData(name);
return configData[name] || getBodyData(name);
}
configData = Object.create(null);
let found = false;
Expand Down Expand Up @@ -85,7 +85,7 @@ export namespace PageConfig {
}
}
}
return configData![name] || Private.getBodyData(name);
return configData![name] || getBodyData(name);
}

/**
Expand Down Expand Up @@ -157,7 +157,7 @@ export namespace PageConfig {
* Get the authorization token for a Jupyter application.
*/
export function getToken(): string {
return getOption('token') || Private.getBodyData('jupyterApiToken');
return getOption('token') || getBodyData('jupyterApiToken');
}

/**
Expand All @@ -175,18 +175,13 @@ export namespace PageConfig {
* Private page config data for the Jupyter application.
*/
let configData: { [key: string]: string } | null = null;
}

/**
* A namespace for module private data.
*/
namespace Private {
/**
* Get a url-encoded item from `body.data` and decode it
* We should never have any encoded URLs anywhere else in code
* until we are building an actual request.
*/
export function getBodyData(key: string): string {
function getBodyData(key: string): string {
if (typeof document === 'undefined' || !document.body) {
return '';
}
Expand All @@ -196,4 +191,59 @@ namespace Private {
}
return decodeURIComponent(val);
}

/**
* The namespace for page config `Extension` functions.
*/
export namespace Extension {
/**
* Populate an array from page config.
*
* @param key - The page config key (e.g., `deferredExtensions`).
*
* #### Notes
* This is intended for `deferredExtensions` and `disabledExtensions`.
*/
function populate(key: string): { raw: string; rule: RegExp }[] {
try {
const raw = getOption(key);
if (raw) {
return JSON.parse(raw).map((pattern: string) => {
return { raw: pattern, rule: new RegExp(pattern) };
});
}
} catch (error) {
console.warn(`Unable to parse ${key}.`, error);
}
return [];
}

/**
* The collection of deferred extensions in page config.
*/
export const deferred = populate('deferredExtensions');

/**
* The collection of disabled extensions in page config.
*/
export const disabled = populate('disabledExtensions');

/**
* Returns whether a plugin is deferred.
*
* @param id - The plugin ID.
*/
export function isDeferred(id: string): boolean {
return deferred.some(val => val.raw === id || val.rule.test(id));
}

/**
* Returns whether a plugin is disabled.
*
* @param id - The plugin ID.
*/
export function isDisabled(id: string): boolean {
return disabled.some(val => val.raw === id || val.rule.test(id));
}
}
}

0 comments on commit 15d0c23

Please sign in to comment.