Skip to content

Commit

Permalink
refactor: move things around
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Feb 15, 2024
1 parent efd4c0d commit c468090
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 104 deletions.
4 changes: 2 additions & 2 deletions src/MySTMarkdownCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { ActivityMonitor } from '@jupyterlab/coreutils';
import { AttachmentsResolver } from '@jupyterlab/attachments';
import { IMapChange } from '@jupyter/ydoc';
import { IMySTMarkdownCell } from './types';
import { getUserExpressions, metadataSection } from './metadata';
import { getUserExpressions, metadataSection } from './userExpressions';
import { IMySTModel, MySTModel, MySTWidget } from './widget';
import { markdownParse, processCellMDAST, renderNotebook } from './myst';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { ITaskItemChange } from './TaskItemControllerProvider';
import { ITaskItemChange } from './providers';

class IMySTWidget {}

Expand Down
9 changes: 0 additions & 9 deletions src/__tests__/jupyterlab_myst.spec.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getUserExpressions,
setUserExpressions,
deleteUserExpressions
} from './metadata';
} from './userExpressions';
import {
INotebookTracker,
Notebook,
Expand Down
2 changes: 2 additions & 0 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './inlineExpression';
export * from './listItem';
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useEffect, useRef, useState } from 'react';
import { useUserExpressions } from './UserExpressionsProvider';
import { useUserExpressions } from '../providers';
import { SingletonLayout, Widget } from '@lumino/widgets';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import {
IUserExpressionMetadata,
IExpressionError,
IExpressionResult,
isError,
isOutput
} from './userExpressions';
import { IUserExpressionMetadata } from './metadata';
} from '../userExpressions';

export interface IRenderedExpressionOptions {
expression: string;
Expand Down Expand Up @@ -213,7 +213,7 @@ function MIMEBundleRenderer({
return <div ref={ref} className="not-prose inline-block" />;
}

export function InlineRenderer({ value }: { value?: string }): JSX.Element {
export function InlineExpression({ value }: { value?: string }): JSX.Element {
const { expressions, rendermime, trusted } = useUserExpressions();

if (!expressions || !rendermime) {
Expand Down
32 changes: 18 additions & 14 deletions src/taskItem.tsx → src/components/listItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react';
import { NodeRenderer } from '@myst-theme/providers';
import type { ListItem } from 'myst-spec-ext';
import { useTaskItemController } from './TaskItemControllerProvider';
import { useTaskItemController } from '../providers';
import { MyST } from 'myst-to-react';

function TaskItem({
Expand All @@ -10,7 +8,7 @@ function TaskItem({
children
}: {
checked?: boolean;
children: React.ReactNode;
children?: React.ReactNode;
line?: number;
}) {
// The rendering waiting on promises from Jupyter is slow
Expand Down Expand Up @@ -38,23 +36,29 @@ function TaskItem({
);
}

export const listItem: NodeRenderer<ListItem & { checked?: boolean }> = ({
node
}) => {
if (node.checked === null) {
export function ListItem({
checked,
line,
children
}: {
checked?: boolean,
line?: number,
children?: any[]

}): JSX.Element {
if (checked === null) {
return (
<li key={node.key}>
<MyST ast={node.children} />
<li>
<MyST ast={children} />
</li>
);
}
return (
<TaskItem
key={node.key}
checked={node.checked}
line={node.position?.start.line}
checked={checked}
line={line}
>
<MyST ast={node.children} />
<MyST ast={children} />
</TaskItem>
);
};
56 changes: 0 additions & 56 deletions src/metadata.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/myst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ import { exerciseDirectives } from 'myst-ext-exercise';
import { StaticNotebook } from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { imageUrlSourceTransform } from './images';
import { internalLinksTransform } from './links';
import { addCiteChildrenPlugin } from './citations';
import { IUserExpressionMetadata } from './metadata';
import { imageUrlSourceTransform, internalLinksTransform, addCiteChildrenPlugin } from './transforms';
import { IUserExpressionMetadata } from './userExpressions';
import { IMySTMarkdownCell } from './types';
import { Cell, ICellModel } from '@jupyterlab/cells';
import { MySTModel } from './widget';
Expand Down
3 changes: 3 additions & 0 deletions src/providers/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './sanitizer';
export * from './taskItem';
export * from './userExpressions';
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { createContext, useContext } from 'react';
import { IUserExpressionMetadata } from './metadata';
import { IUserExpressionMetadata } from '../userExpressions';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';

type UserExpressionsState = {
Expand Down
11 changes: 6 additions & 5 deletions src/renderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import React from 'react';
import { DEFAULT_RENDERERS } from 'myst-to-react';
import { MermaidNodeRenderer } from '@myst-theme/diagrams';
import { NodeRenderer } from '@myst-theme/providers';
import { InlineRenderer } from './inlineExpression';
import { listItem } from './taskItem';
import { useSanitizer } from './SanitizerProvider';
import { InlineExpression, ListItem } from './components';
import { useSanitizer } from './providers';

export const renderers: Record<string, NodeRenderer> = {
...DEFAULT_RENDERERS,
mermaid: MermaidNodeRenderer,
inlineExpression: ({ node }) => {
return <InlineRenderer value={node.value} />;
return <InlineExpression value={node.value} />;
},
listItem: ({ node }) => {
return <ListItem checked={node.checked} line={node.position?.start.line} children={node.children}/>;
},
listItem,
html: ({ node }, children) => {
const { sanitizer } = useSanitizer();
if (sanitizer !== undefined) {
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions src/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './citations';
export * from './images';
export * from './links';
File renamed without changes.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MarkdownCell } from '@jupyterlab/cells';
import { IMySTModel } from './widget';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';


export type IMySTMarkdownCell = MarkdownCell & {
readonly fragmentMDAST: any | undefined;
readonly attachmentsResolver: IRenderMime.IResolver;
Expand Down
66 changes: 66 additions & 0 deletions src/userExpressions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { PartialJSONObject } from '@lumino/coreutils';
import { Cell } from '@jupyterlab/cells';
import type { IMySTMarkdownCell } from './types';


export const metadataSection = 'user_expressions';

/**
* Interfaces for `IExecuteReplyMsg.user_expressisons`
Expand Down Expand Up @@ -32,3 +37,64 @@ export function isOutput(
export function isError(output: IExpressionResult): output is IExpressionError {
return output.status === 'error';
}


export interface IUserExpressionMetadata extends PartialJSONObject {
expression: string;
result: IExpressionResult;
}

export interface IUserExpressionsMetadata extends PartialJSONObject {
[metadataSection]: IUserExpressionMetadata[];
}


export function getUserExpressions(
cell: IMySTMarkdownCell | Cell
): IUserExpressionMetadata[] | undefined {
if (!cell.model.getMetadata) {
// this is JupyterLab 3.6
return (cell.model.metadata as any)?.get(metadataSection) as
| IUserExpressionMetadata[]
| undefined;
}
return cell.model.getMetadata(metadataSection) as
| IUserExpressionMetadata[]
| undefined;
}

export function setUserExpressions(
cell: IMySTMarkdownCell | Cell,
expressions: IUserExpressionMetadata[]
) {
if (!cell) {
return;
}
if (!cell.model.setMetadata) {
// this is JupyterLab 3.6
(cell.model.metadata as any).set(metadataSection, expressions);
} else {
cell.model.setMetadata(metadataSection, expressions);
}
}

export function deleteUserExpressions(cell: IMySTMarkdownCell | Cell) {
if (!cell) {
return;
}
if (!cell.model.setMetadata) {
// this is JupyterLab 3.6
(cell.model.metadata as any).delete(metadataSection);
} else {
cell.model.deleteMetadata(metadataSection);
}
}

export interface IUserExpressionMetadata extends PartialJSONObject {
expression: string;
result: IExpressionResult;
}

export interface IUserExpressionsMetadata extends PartialJSONObject {
[metadataSection]: IUserExpressionMetadata[];
}
12 changes: 5 additions & 7 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ import {
} from '@myst-theme/providers';
import { MyST } from 'myst-to-react';
import React from 'react';
import { UserExpressionsProvider } from './UserExpressionsProvider';
import {
import { UserExpressionsProvider,
ITaskItemChange,
ITaskItemController,
TaskItemControllerProvider
} from './TaskItemControllerProvider';
TaskItemControllerProvider,
SanitizerProvider } from './providers';
import { renderers } from './renderers';
import { IUserExpressionMetadata } from './metadata';
import { linkFactory } from './links';
import { SanitizerProvider } from './SanitizerProvider';
import { IUserExpressionMetadata } from './userExpressions';
import { linkFactory } from './transforms';

/**
* The MIME type for Markdown.
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"strictNullChecks": true,
"target": "ES2018"
},
"include": ["src/*"]
"include": ["src/**/*"]
}

0 comments on commit c468090

Please sign in to comment.