2021-02-11 00:41:53 +00:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import type * as markdownIt from 'markdown-it';
|
2021-08-28 00:21:53 +00:00
|
|
|
import type { RendererContext } from 'vscode-notebook-renderer';
|
2021-02-11 00:41:53 +00:00
|
|
|
|
2021-04-27 02:58:24 +00:00
|
|
|
const styleHref = import.meta.url.replace(/katex.js$/, 'katex.min.css');
|
2021-03-11 22:02:51 +00:00
|
|
|
|
2021-08-28 00:21:53 +00:00
|
|
|
export async function activate(ctx: RendererContext<void>) {
|
2022-06-30 23:19:49 +00:00
|
|
|
const markdownItRenderer = (await ctx.getRenderer('vscode.markdown-it-renderer')) as undefined | any;
|
2021-05-26 16:15:00 +00:00
|
|
|
if (!markdownItRenderer) {
|
2022-06-30 23:19:49 +00:00
|
|
|
throw new Error(`Could not load 'vscode.markdown-it-renderer'`);
|
2021-05-26 16:15:00 +00:00
|
|
|
}
|
2021-03-11 22:02:51 +00:00
|
|
|
|
2021-08-04 04:46:02 +00:00
|
|
|
// Add katex styles to be copied to shadow dom
|
2021-05-13 02:32:57 +00:00
|
|
|
const link = document.createElement('link');
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.classList.add('markdown-style');
|
|
|
|
link.href = styleHref;
|
2021-05-07 00:10:10 +00:00
|
|
|
|
2021-08-04 04:46:02 +00:00
|
|
|
// Add same katex style to root document.
|
|
|
|
// This is needed for the font to be loaded correctly inside the shadow dom.
|
|
|
|
//
|
|
|
|
// Seems like https://bugs.chromium.org/p/chromium/issues/detail?id=336876
|
|
|
|
const linkHead = document.createElement('link');
|
|
|
|
linkHead.rel = 'stylesheet';
|
|
|
|
linkHead.href = styleHref;
|
|
|
|
document.head.appendChild(linkHead);
|
|
|
|
|
2021-05-13 02:32:57 +00:00
|
|
|
const style = document.createElement('style');
|
|
|
|
style.textContent = `
|
|
|
|
.katex-error {
|
|
|
|
color: var(--vscode-editorError-foreground);
|
|
|
|
}
|
2022-11-02 08:09:32 +00:00
|
|
|
.katex-block {
|
|
|
|
counter-reset: katexEqnNo mmlEqnNo;
|
|
|
|
}
|
2021-05-13 02:32:57 +00:00
|
|
|
`;
|
2021-06-09 22:15:48 +00:00
|
|
|
|
|
|
|
// Put Everything into a template
|
|
|
|
const styleTemplate = document.createElement('template');
|
|
|
|
styleTemplate.classList.add('markdown-style');
|
|
|
|
styleTemplate.content.appendChild(style);
|
|
|
|
styleTemplate.content.appendChild(link);
|
|
|
|
document.head.appendChild(styleTemplate);
|
2021-02-23 02:10:27 +00:00
|
|
|
|
2024-01-13 02:46:08 +00:00
|
|
|
const katex = require('@vscode/markdown-it-katex').default;
|
2022-05-02 16:45:06 +00:00
|
|
|
const macros = {};
|
2021-05-13 02:32:57 +00:00
|
|
|
markdownItRenderer.extendMarkdownIt((md: markdownIt.MarkdownIt) => {
|
2021-10-12 02:08:29 +00:00
|
|
|
return md.use(katex, {
|
|
|
|
globalGroup: true,
|
|
|
|
enableBareBlocks: true,
|
2022-05-02 16:45:06 +00:00
|
|
|
macros
|
2021-10-12 02:08:29 +00:00
|
|
|
});
|
2021-05-13 02:32:57 +00:00
|
|
|
});
|
2021-04-27 02:58:24 +00:00
|
|
|
}
|