Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/markdown-math/notebook/katex.ts
4772 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import type * as markdownIt from 'markdown-it';
6
import type { RendererContext } from 'vscode-notebook-renderer';
7
8
const styleHref = import.meta.url.replace(/katex.js$/, 'katex.min.css');
9
10
export async function activate(ctx: RendererContext<void>) {
11
const markdownItRenderer = (await ctx.getRenderer('vscode.markdown-it-renderer')) as undefined | any;
12
if (!markdownItRenderer) {
13
throw new Error(`Could not load 'vscode.markdown-it-renderer'`);
14
}
15
16
// Add katex styles to be copied to shadow dom
17
const link = document.createElement('link');
18
link.rel = 'stylesheet';
19
link.classList.add('markdown-style');
20
link.href = styleHref;
21
22
// Add same katex style to root document.
23
// This is needed for the font to be loaded correctly inside the shadow dom.
24
//
25
// Seems like https://bugs.chromium.org/p/chromium/issues/detail?id=336876
26
const linkHead = document.createElement('link');
27
linkHead.rel = 'stylesheet';
28
linkHead.href = styleHref;
29
document.head.appendChild(linkHead);
30
31
const style = document.createElement('style');
32
style.textContent = `
33
.katex-error {
34
color: var(--vscode-editorError-foreground);
35
}
36
.katex-block {
37
counter-reset: katexEqnNo mmlEqnNo;
38
}
39
`;
40
41
// Put Everything into a template
42
const styleTemplate = document.createElement('template');
43
styleTemplate.classList.add('markdown-style');
44
styleTemplate.content.appendChild(style);
45
styleTemplate.content.appendChild(link);
46
document.head.appendChild(styleTemplate);
47
48
const katex = require('@vscode/markdown-it-katex').default;
49
const macros = {};
50
markdownItRenderer.extendMarkdownIt((md: markdownIt.MarkdownIt) => {
51
return md.use(katex, {
52
globalGroup: true,
53
enableBareBlocks: true,
54
enableFencedBlocks: true,
55
macros,
56
});
57
});
58
}
59
60