Add macros entry to markdown-math configuration (#180458)

section.
This commit is contained in:
AlbertHilb 2023-06-20 23:31:26 +02:00 committed by GitHub
parent ffe64dab3c
commit 2929ec4996
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View file

@ -80,6 +80,13 @@
"type": "boolean",
"default": true,
"description": "%config.markdown.math.enabled%"
},
"markdown.math.macros": {
"type": "object",
"additionalProperties": { "type": "string" },
"default": {},
"description": "%config.markdown.math.macros%",
"scope": "resource"
}
}
}

View file

@ -1,5 +1,6 @@
{
"displayName": "Markdown Math",
"description": "Adds math support to Markdown in notebooks.",
"config.markdown.math.enabled": "Enable/disable rendering math in the built-in Markdown preview."
"config.markdown.math.enabled": "Enable/disable rendering math in the built-in Markdown preview.",
"config.markdown.math.macros": "A collection of custom macros. Each macro is a key-value pair where the key is a new command name and the value is the expansion of the macro."
}

View file

@ -6,7 +6,7 @@ import * as vscode from 'vscode';
declare function require(path: string): any;
const enabledSetting = 'markdown.math.enabled';
const markdownMathSetting = 'markdown.math';
export function activate(context: vscode.ExtensionContext) {
function isEnabled(): boolean {
@ -14,8 +14,13 @@ export function activate(context: vscode.ExtensionContext) {
return config.get<boolean>('math.enabled', true);
}
function getMacros(): { [key: string]: string } {
const config = vscode.workspace.getConfiguration('markdown');
return config.get<{ [key: string]: string }>('math.macros', {});
}
vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(enabledSetting)) {
if (e.affectsConfiguration(markdownMathSetting)) {
vscode.commands.executeCommand('markdown.api.reloadPlugins');
}
}, undefined, context.subscriptions);
@ -24,8 +29,11 @@ export function activate(context: vscode.ExtensionContext) {
extendMarkdownIt(md: any) {
if (isEnabled()) {
const katex = require('@vscode/markdown-it-katex');
const options = { globalGroup: true, macros: {} };
md.core.ruler.push('reset-katex-macros', () => { options.macros = {}; });
const settingsMacros = getMacros();
const options = { globalGroup: true, macros: { ...settingsMacros } };
md.core.ruler.push('reset-katex-macros', () => {
options.macros = { ...settingsMacros };
});
return md.use(katex, options);
}
return md;