-
Notifications
You must be signed in to change notification settings - Fork 19
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
feat: extension point for myst options #115
base: main
Are you sure you want to change the base?
Changes from 2 commits
280c8a1
a305824
38270d4
c53884a
4bea826
f9f9ed6
e560686
d163036
e7705fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import { MarkdownCell } from '@jupyterlab/cells'; | ||
import { NotebookPanel, StaticNotebook } from '@jupyterlab/notebook'; | ||
import { MySTMarkdownCell } from './MySTMarkdownCell'; | ||
import { MySTOptionsProvider, MySTNotebookDefaults } from './myst'; | ||
|
||
export class MySTContentFactory extends NotebookPanel.ContentFactory { | ||
|
||
mystOptions: MySTOptionsProvider<StaticNotebook>; | ||
|
||
constructor(options = {}, | ||
mystOptions = new MySTNotebookDefaults() as MySTOptionsProvider<StaticNotebook>) { | ||
super(options); | ||
this.mystOptions = mystOptions; | ||
} | ||
|
||
createMarkdownCell( | ||
options: MarkdownCell.IOptions, | ||
parent: StaticNotebook | ||
): MarkdownCell { | ||
if (!options.contentFactory) { | ||
options.contentFactory = this; | ||
} | ||
return new MySTMarkdownCell(options).initializeState(); | ||
return new MySTMarkdownCell(options, this.mystOptions).initializeState(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
import { mystParse } from 'myst-parser'; | ||||||
import { mystParse, AllOptions } from 'myst-parser'; | ||||||
import { liftChildren } from 'myst-common'; | ||||||
import { | ||||||
mathPlugin, | ||||||
|
@@ -36,6 +36,28 @@ import { imageUrlSourceTransform } from './images'; | |||||
import { internalLinksPlugin } from './links'; | ||||||
import { addCiteChildrenPlugin } from './citations'; | ||||||
|
||||||
export interface MySTOptions { | ||||||
|
||||||
parserOptions: Partial<AllOptions>; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fwkoch is there any thinking we need to on this before we start encouraging using these more widely?! They look pretty solid I think. |
||||||
|
||||||
export interface MySTOptionsProvider<Widget> { | ||||||
|
||||||
get(widget: Widget): MySTOptions; | ||||||
} | ||||||
|
||||||
export class MySTNotebookDefaults implements MySTOptionsProvider<StaticNotebook> { | ||||||
|
||||||
get(notebook: StaticNotebook): MySTOptions { | ||||||
return { | ||||||
parserOptions: { | ||||||
directives: [cardDirective, gridDirective, ...tabDirectives], | ||||||
roles: [evalRole], | ||||||
}, | ||||||
}; | ||||||
} | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love to see you are overriding things with other extensions @tavin! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
|
||||||
const evalRole: RoleSpec = { | ||||||
name: 'eval', | ||||||
body: { | ||||||
|
@@ -48,11 +70,8 @@ const evalRole: RoleSpec = { | |||||
} | ||||||
}; | ||||||
|
||||||
export function markdownParse(text: string): Root { | ||||||
const mdast = mystParse(text, { | ||||||
directives: [cardDirective, gridDirective, ...tabDirectives], | ||||||
roles: [evalRole] | ||||||
}); | ||||||
export function markdownParse(text: string, options: Partial<AllOptions>): Root { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am newish to typescript, actually I didn't know that was available syntax.... but anyway, I don't know? Why? |
||||||
const mdast = mystParse(text, options); | ||||||
// Parsing individually here requires that link and footnote references are contained to the cell | ||||||
// This is consistent with the current Jupyter markdown renderer | ||||||
unified() | ||||||
|
@@ -73,7 +92,10 @@ export function markdownParse(text: string): Root { | |||||
return mdast as Root; | ||||||
} | ||||||
|
||||||
export function parseContent(notebook: StaticNotebook): Promise<void> { | ||||||
export function parseContent( | ||||||
notebook: StaticNotebook, | ||||||
options: MySTOptions | ||||||
): Promise<void> { | ||||||
const cells = getCellList(notebook)?.filter( | ||||||
// In the future, we may want to process the code cells as well, but not now | ||||||
cell => cell.model.type === 'markdown' | ||||||
|
@@ -87,7 +109,7 @@ export function parseContent(notebook: StaticNotebook): Promise<void> { | |||||
const text = cell.model?.value.text ?? ''; | ||||||
if (!cell.myst.pre) { | ||||||
// This will be cleared when the cell is executed, and parsed again here | ||||||
cell.myst.pre = markdownParse(text); | ||||||
cell.myst.pre = markdownParse(text, options.parserOptions); | ||||||
} | ||||||
return { type: 'block', children: copyNode(cell.myst.pre).children }; | ||||||
}); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this because downstream you are calling this constructor?
Do we need this render in here at all? Or can we remove it + the option and explicitly call renderInput when we need to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's all just a horrible hack because of a bug in the 3.5.x parent class. A cursory look tells me it's fixed in the 3.6.x series. I don't know when it's okay for jupyterlab-myst to start following v3.6. But once the bug is gone, this code can be gone.