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

First cut at unifying notebook renderers apis #123738

Merged
merged 3 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 6 additions & 18 deletions extensions/markdown-language-features/notebook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,23 @@

const MarkdownIt = require('markdown-it');

export async function activate(ctx: {
dependencies: ReadonlyArray<{ entrypoint: string }>
}) {
export function activate() {
let markdownIt = new MarkdownIt({
html: true
});

// Should we load the deps before this point?
// Also could we await inside `renderMarkup`?
await Promise.all(ctx.dependencies.map(async (dep) => {
try {
const api = await import(dep.entrypoint);
if (api?.extendMarkdownIt) {
markdownIt = api.extendMarkdownIt(markdownIt);
}
} catch (e) {
console.error('Could not load markdown entryPoint', e);
}
}));

return {
renderMarkup: (context: { element: HTMLElement, content: string }) => {
const rendered = markdownIt.render(context.content);
renderCell: (_id: string, context: { element: HTMLElement, value: string }) => {
const rendered = markdownIt.render(context.value);
context.element.innerHTML = rendered;

// Insert styles into markdown preview shadow dom so that they are applied
for (const markdownStyleNode of document.getElementsByClassName('markdown-style')) {
context.element.insertAdjacentElement('beforebegin', markdownStyleNode.cloneNode(true) as Element);
}
},
extendMarkdownIt: (f: (md: typeof markdownIt) => void) => {
f(markdownIt);
}
};
}
10 changes: 8 additions & 2 deletions extensions/notebook-markdown-extensions/notebook/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import type * as markdownIt from 'markdown-it';

const emoji = require('markdown-it-emoji');

export function extendMarkdownIt(md: markdownIt.MarkdownIt) {
return md.use(emoji);
export function activate(ctx: {
getRenderer: (id: string) => any
}) {
const markdownItRenderer = ctx.getRenderer('markdownItRenderer');

markdownItRenderer.extendMarkdownIt((md: markdownIt.MarkdownIt) => {
return md.use(emoji);
});
}
37 changes: 21 additions & 16 deletions extensions/notebook-markdown-extensions/notebook/katex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ import type * as markdownIt from 'markdown-it';

const styleHref = import.meta.url.replace(/katex.js$/, 'katex.min.css');

const link = document.createElement('link');
link.rel = 'stylesheet';
link.classList.add('markdown-style');
link.href = styleHref;
document.head.append(link);
export function activate(ctx: {
getRenderer: (id: string) => any
}) {
const markdownItRenderer = ctx.getRenderer('markdownItRenderer');

const style = document.createElement('style');
style.classList.add('markdown-style');
style.textContent = `
.katex-error {
color: var(--vscode-editorError-foreground);
}
`;
document.head.append(style);
const link = document.createElement('link');
link.rel = 'stylesheet';
link.classList.add('markdown-style');
link.href = styleHref;
document.head.append(link);

const katex = require('@iktakahiro/markdown-it-katex');
const style = document.createElement('style');
style.classList.add('markdown-style');
style.textContent = `
.katex-error {
color: var(--vscode-editorError-foreground);
}
`;
document.head.append(style);

export function extendMarkdownIt(md: markdownIt.MarkdownIt) {
return md.use(katex);
const katex = require('@iktakahiro/markdown-it-katex');
markdownItRenderer.extendMarkdownIt((md: markdownIt.MarkdownIt) => {
return md.use(katex);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { asWebviewUri } from 'vs/workbench/api/common/shared/webview';
import { CellEditState, ICellOutputViewModel, ICommonCellInfo, ICommonNotebookEditor, IDisplayOutputLayoutUpdateRequest, IDisplayOutputViewModel, IGenericCellViewModel, IInsetRenderOutput, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { preloadsScriptStr, WebviewPreloadRenderer } from 'vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads';
import { preloadsScriptStr, RendererMetadata } from 'vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads';
import { transformWebviewThemeVars } from 'vs/workbench/contrib/notebook/browser/view/renderers/webviewThemeMapping';
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
import { INotebookKernel, INotebookRendererInfo, NotebookRendererMatch } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookKernel, INotebookRendererInfo } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { IWebviewService, WebviewContentPurpose, WebviewElement } from 'vs/workbench/contrib/webview/browser/webview';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
Expand Down Expand Up @@ -453,7 +453,7 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Disposable {
this.element.style.position = 'absolute';
}
private generateContent(coreDependencies: string, baseUrl: string) {
const markupRenderer = this.getMarkdownRenderer();
const renderersData = this.getRendererData();
const outputWidth = `calc(100% - ${this.options.leftMargin + this.options.rightMargin + this.options.runGutter}px)`;
const outputMarginLeft = `${this.options.leftMargin + this.options.runGutter}px`;
return html`
Expand Down Expand Up @@ -707,36 +707,19 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Disposable {
</script>
${coreDependencies}
<div id='container' class="widgetarea" style="position: absolute;width:100%;top: 0px"></div>
<script type="module">${preloadsScriptStr(this.options, markupRenderer)}</script>
<script type="module">${preloadsScriptStr(this.options, renderersData)}</script>
</body>
</html>`;
}

private getMarkdownRenderer(): WebviewPreloadRenderer[] {
const markdownMimeType = 'text/markdown';
const allRenderers = this.notebookService.getRenderers()
.filter(renderer => renderer.matchesWithoutKernel(markdownMimeType) !== NotebookRendererMatch.Never);

const topLevelMarkdownRenderers = allRenderers
.filter(renderer => renderer.dependencies.length === 0);

const subRenderers = new Map<string, Array<{ entrypoint: string }>>();
for (const renderer of allRenderers) {
for (const dep of renderer.dependencies) {
if (!subRenderers.has(dep)) {
subRenderers.set(dep, []);
}
const entryPoint = this.asWebviewUri(renderer.entrypoint, renderer.extensionLocation);
subRenderers.get(dep)!.push({ entrypoint: entryPoint.toString(true) });
}
}

return topLevelMarkdownRenderers.map((renderer): WebviewPreloadRenderer => {
const src = this.asWebviewUri(renderer.entrypoint, renderer.extensionLocation);
private getRendererData(): RendererMetadata[] {
return this.notebookService.getRenderers().map((renderer): RendererMetadata => {
connor4312 marked this conversation as resolved.
Show resolved Hide resolved
const entrypoint = this.asWebviewUri(renderer.entrypoint, renderer.extensionLocation).toString();
return {
entrypoint: src.toString(),
id: renderer.id,
entrypoint,
mimeTypes: renderer.mimeTypes,
dependencies: subRenderers.get(renderer.id) || [],
dependencies: Array.from(renderer.dependencies.values())
};
});
}
Expand Down
Loading