Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐿 fix thebe types #788

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-feet-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-frontmatter': patch
---

Changes thebe types to correctly provide the shapes of expanded thebe options after frontmatter validation. Updated the validator to use the types.
4 changes: 2 additions & 2 deletions packages/myst-frontmatter/src/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Licenses } from '../licenses/types.js';
import type { Numbering } from '../numbering/types.js';
import type { ProjectSettings } from '../settings/types.js';
import type { SiteFrontmatter } from '../site/types.js';
import type { Thebe } from '../thebe/types.js';
import type { ExpandedThebeFrontmatter } from '../thebe/types.js';

export type ProjectAndPageFrontmatter = SiteFrontmatter & {
date?: string;
Expand Down Expand Up @@ -34,5 +34,5 @@ export type ProjectFrontmatter = ProjectAndPageFrontmatter & {
references?: Record<string, { url: string }>;
requirements?: string[];
resources?: string[];
thebe?: Thebe;
thebe?: ExpandedThebeFrontmatter;
};
9 changes: 8 additions & 1 deletion packages/myst-frontmatter/src/thebe/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type Thebe = {
export type ThebeFrontmatter = boolean | string | ThebeFrontmatterObject;

export type ThebeFrontmatterObject = {
lite?: boolean;
binder?: boolean | BinderHubOptions;
server?: JupyterServerOptions;
Expand All @@ -9,6 +11,11 @@ export type Thebe = {
mathjaxConfig?: string;
};

// NOTE: currently a subtle difference but will likely grow with lite options
export type ExpandedThebeFrontmatter = Omit<ThebeFrontmatterObject, 'binder'> & {
binder?: BinderHubOptions;
};

export type WellKnownRepoProviders = 'github' | 'gitlab' | 'git' | 'gist';
export type BinderProviders = WellKnownRepoProviders | string;

Expand Down
25 changes: 18 additions & 7 deletions packages/myst-frontmatter/src/thebe/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import {
defined,
incrementOptions,
validateBoolean,
validateObject,
validateObjectKeys,
validateString,
validateUrl,
validationError,
} from 'simple-validators';
import { GITHUB_USERNAME_REPO_REGEX } from '../utils/validators.js';
import type { BinderHubOptions, JupyterServerOptions, Thebe } from './types.js';
import type {
BinderHubOptions,
JupyterServerOptions,
ExpandedThebeFrontmatter,
ThebeFrontmatter,
ThebeFrontmatterObject,
} from './types.js';

const THEBE_KEYS = [
'lite',
Expand All @@ -32,10 +37,10 @@ const JUPYTER_SERVER_OPTIONS_KEYS = ['url', 'token'];
* https://thebe-core.curve.space/docs-core/a-configuration
*/
export function validateThebe(
input: any,
input: ThebeFrontmatter,
github: string | undefined,
opts: ValidationOptions,
): Thebe | undefined {
): ExpandedThebeFrontmatter | undefined {
if (input === false) return undefined;
if (input === 'lite') return { lite: true };
if (typeof input === 'string' && input !== 'binder') {
Expand All @@ -45,16 +50,22 @@ export function validateThebe(
);
}

let inputObject: Record<string, any> = input;
let inputObject: ThebeFrontmatterObject;
if (input === true || input === 'binder') {
// expand boolean methods to object
inputObject = { binder: true };
} else {
inputObject = input;
}

const value: Thebe | undefined = validateObjectKeys(inputObject, { optional: THEBE_KEYS }, opts);
const value: ThebeFrontmatter | undefined = validateObjectKeys(
inputObject,
{ optional: THEBE_KEYS },
opts,
);

if (value === undefined) return undefined;
const output: Thebe = {};
const output: ExpandedThebeFrontmatter = {};
if (defined(value.lite)) {
output.lite = validateBoolean(value.lite, incrementOptions('lite', opts));
}
Expand Down
Loading