UNPKG

thebe-core

Version:

Typescript based core functionality for Thebe

81 lines (71 loc) 2.01 kB
import { Config } from './config'; import type { ThebeEvents } from './events'; import type { ServerSettings, BinderOptions, KernelOptions, CoreOptions, SavedSessionOptions, MathjaxOptions, } from './types'; import { shortId } from './utils'; export function makeBinderOptions(opts: BinderOptions) { return { repo: 'executablebooks/thebe-binder-base', ref: 'HEAD', binderUrl: 'https://mybinder.org', repoProvider: 'github', ...opts, }; } export function makeSavedSessionOptions(opts: SavedSessionOptions): Required<SavedSessionOptions> { return { enabled: true, maxAge: 86400, storagePrefix: 'thebe-binder', ...opts, }; } export function makeKernelOptions(opts: KernelOptions): Required<KernelOptions> { return { path: opts.path ?? '/', kernelName: opts.kernelName ?? 'python', }; } export function makeServerSettings(settings: ServerSettings): Required<ServerSettings> { const baseUrl = settings.baseUrl ?? 'http://localhost:8888'; const wsUrl = settings.wsUrl ?? baseUrl.replace(/^http/, 'ws'); return { token: shortId(), // randomized default token to prevent accidental access to a local server appendToken: true, ...settings, wsUrl, baseUrl, }; } export function makeMathjaxOptions(opts?: MathjaxOptions) { return { mathjaxUrl: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js', mathjaxConfig: 'TeX-AMS_CHTML-full,Safe', ...opts, }; } export function makeConfiguration( options: CoreOptions & { [k: string]: any }, events?: ThebeEvents, ) { return new Config(options, { events }); } export function ensureCoreOptions( options: CoreOptions & { [k: string]: any }, events?: ThebeEvents, ): Required<CoreOptions> { const config = new Config(options, { events }); return { ...config.base, binderOptions: config.binder, savedSessionOptions: config.savedSessions, kernelOptions: config.kernels, serverSettings: config.serverSettings, }; }