diff --git a/build/gulpfile.extensions.js b/build/gulpfile.extensions.js index cbb7c486b13..60e41d2b020 100644 --- a/build/gulpfile.extensions.js +++ b/build/gulpfile.extensions.js @@ -50,6 +50,7 @@ const compilations = [ 'json-language-features/server/tsconfig.json', 'markdown-language-features/preview-src/tsconfig.json', 'markdown-language-features/tsconfig.json', + 'markdown-math/tsconfig.json', 'merge-conflict/tsconfig.json', 'microsoft-authentication/tsconfig.json', 'npm/tsconfig.json', diff --git a/build/npm/dirs.js b/build/npm/dirs.js index b014dcd2578..4e1b46179f5 100644 --- a/build/npm/dirs.js +++ b/build/npm/dirs.js @@ -28,9 +28,9 @@ exports.dirs = [ 'extensions/json-language-features', 'extensions/json-language-features/server', 'extensions/markdown-language-features', + 'extensions/markdown-math', 'extensions/merge-conflict', 'extensions/microsoft-authentication', - 'extensions/markdown-math', 'extensions/npm', 'extensions/php-language-features', 'extensions/search-result', diff --git a/extensions/markdown-language-features/package.json b/extensions/markdown-language-features/package.json index ec72a0d5344..61d2a848089 100644 --- a/extensions/markdown-language-features/package.json +++ b/extensions/markdown-language-features/package.json @@ -26,6 +26,7 @@ "onCommand:markdown.showSource", "onCommand:markdown.showPreviewSecuritySelector", "onCommand:markdown.api.render", + "onCommand:markdown.api.reloadPlugins", "onWebviewPanel:markdown.preview", "onCustomEditor:vscode.markdown.preview.editor" ], diff --git a/extensions/markdown-language-features/src/commands/index.ts b/extensions/markdown-language-features/src/commands/index.ts index 68aff7ffcf5..d810fab4931 100644 --- a/extensions/markdown-language-features/src/commands/index.ts +++ b/extensions/markdown-language-features/src/commands/index.ts @@ -3,11 +3,13 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -export { OpenDocumentLinkCommand } from './openDocumentLink'; -export { ShowPreviewCommand, ShowPreviewToSideCommand, ShowLockedPreviewToSideCommand } from './showPreview'; -export { ShowSourceCommand } from './showSource'; -export { RefreshPreviewCommand } from './refreshPreview'; -export { ShowPreviewSecuritySelectorCommand } from './showPreviewSecuritySelector'; export { MoveCursorToPositionCommand } from './moveCursorToPosition'; -export { ToggleLockCommand } from './toggleLock'; +export { OpenDocumentLinkCommand } from './openDocumentLink'; +export { RefreshPreviewCommand } from './refreshPreview'; +export { ReloadPlugins } from './reloadPlugins'; export { RenderDocument } from './renderDocument'; +export { ShowLockedPreviewToSideCommand, ShowPreviewCommand, ShowPreviewToSideCommand } from './showPreview'; +export { ShowPreviewSecuritySelectorCommand } from './showPreviewSecuritySelector'; +export { ShowSourceCommand } from './showSource'; +export { ToggleLockCommand } from './toggleLock'; + diff --git a/extensions/markdown-language-features/src/commands/reloadPlugins.ts b/extensions/markdown-language-features/src/commands/reloadPlugins.ts new file mode 100644 index 00000000000..f712a41d438 --- /dev/null +++ b/extensions/markdown-language-features/src/commands/reloadPlugins.ts @@ -0,0 +1,23 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { Command } from '../commandManager'; +import { MarkdownPreviewManager } from '../features/previewManager'; +import { MarkdownEngine } from '../markdownEngine'; + +export class ReloadPlugins implements Command { + public readonly id = 'markdown.api.reloadPlugins'; + + public constructor( + private readonly webviewManager: MarkdownPreviewManager, + private readonly engine: MarkdownEngine, + ) { } + + public execute(): void { + this.engine.reloadPlugins(); + this.engine.cleanCache(); + this.webviewManager.refresh(); + } +} diff --git a/extensions/markdown-language-features/src/extension.ts b/extensions/markdown-language-features/src/extension.ts index a3479c6cd77..030015aa055 100644 --- a/extensions/markdown-language-features/src/extension.ts +++ b/extensions/markdown-language-features/src/extension.ts @@ -80,6 +80,7 @@ function registerMarkdownCommands( commandManager.register(new commands.OpenDocumentLinkCommand(engine)); commandManager.register(new commands.ToggleLockCommand(previewManager)); commandManager.register(new commands.RenderDocument(engine)); + commandManager.register(new commands.ReloadPlugins(previewManager, engine)); return commandManager; } diff --git a/extensions/markdown-language-features/src/markdownEngine.ts b/extensions/markdown-language-features/src/markdownEngine.ts index 346547f3da9..83d7e82da95 100644 --- a/extensions/markdown-language-features/src/markdownEngine.ts +++ b/extensions/markdown-language-features/src/markdownEngine.ts @@ -67,6 +67,7 @@ interface RenderEnv { } export class MarkdownEngine { + private md?: Promise; private _slugCount = new Map(); @@ -129,6 +130,10 @@ export class MarkdownEngine { return md; } + public reloadPlugins() { + this.md = undefined; + } + private tokenizeDocument( document: SkinnyTextDocument, config: MarkdownItConfig, diff --git a/extensions/markdown-math/README.md b/extensions/markdown-math/README.md index cc1d7f6fc4f..64d18d01be3 100644 --- a/extensions/markdown-math/README.md +++ b/extensions/markdown-math/README.md @@ -1,3 +1,3 @@ -# Markdown Notebook Math support +# Markdown Math support **Notice:** This extension is bundled with Visual Studio Code. It can be disabled but not uninstalled. diff --git a/extensions/markdown-math/extension-browser.webpack.config.js b/extensions/markdown-math/extension-browser.webpack.config.js new file mode 100644 index 00000000000..7fcc53a728b --- /dev/null +++ b/extensions/markdown-math/extension-browser.webpack.config.js @@ -0,0 +1,17 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +//@ts-check + +'use strict'; + +const withBrowserDefaults = require('../shared.webpack.config').browser; + +module.exports = withBrowserDefaults({ + context: __dirname, + entry: { + extension: './src/extension.ts' + } +}); diff --git a/extensions/markdown-math/extension.webpack.config.js b/extensions/markdown-math/extension.webpack.config.js new file mode 100644 index 00000000000..de88398eca0 --- /dev/null +++ b/extensions/markdown-math/extension.webpack.config.js @@ -0,0 +1,20 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +//@ts-check + +'use strict'; + +const withDefaults = require('../shared.webpack.config'); + +module.exports = withDefaults({ + context: __dirname, + resolve: { + mainFields: ['module', 'main'] + }, + entry: { + extension: './src/extension.ts', + } +}); diff --git a/extensions/markdown-math/package.json b/extensions/markdown-math/package.json index 541d7b77534..98c1c6e4552 100644 --- a/extensions/markdown-math/package.json +++ b/extensions/markdown-math/package.json @@ -5,7 +5,6 @@ "version": "1.0.0", "icon": "icon.png", "publisher": "vscode", - "enableProposedApi": true, "license": "MIT", "aiKey": "AIF-d9b70cd4-b9f9-4d70-929b-a071c400b217", "engines": { @@ -20,16 +19,36 @@ "supported": true } }, + "main": "./out/extension", + "browser": "./dist/browser/extension", + "activationEvents": [], "contributes": { "notebookRenderer": [ { "id": "markdownItRenderer-katex", - "displayName": "Markdown it katex renderer", + "displayName": "Markdown it KaTeX renderer", "entrypoint": { "extends": "markdownItRenderer", "path": "./notebook-out/katex.js" } } + ], + "markdown.markdownItPlugins": true, + "markdown.previewStyles": [ + "./node_modules/katex/dist/katex.min.css", + "./preview-styles/index.css" + ], + "configuration": [ + { + "title": "Markdown", + "properties": { + "markdown.math.enabled": { + "type": "boolean", + "default": true, + "description": "%config.markdown.math.enabled%" + } + } + } ] }, "scripts": { @@ -37,10 +56,11 @@ "watch": "npm run build-notebook", "build-notebook": "node ./esbuild" }, + "dependencies": { + "@iktakahiro/markdown-it-katex": "https://github.com/mjbvz/markdown-it-katex.git" + }, "devDependencies": { - "@iktakahiro/markdown-it-katex": "https://github.com/mjbvz/markdown-it-katex.git", - "@types/markdown-it": "^0.0.0", - "markdown-it": "^12.0.4" + "@types/markdown-it": "^0.0.0" }, "repository": { "type": "git", diff --git a/extensions/markdown-math/package.nls.json b/extensions/markdown-math/package.nls.json index 169fe707966..5fb6a52005a 100644 --- a/extensions/markdown-math/package.nls.json +++ b/extensions/markdown-math/package.nls.json @@ -1,4 +1,5 @@ { - "displayName": "Markdown Notebook math", - "description": "Adds math support to markdown in notebooks." + "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." } diff --git a/extensions/markdown-math/preview-styles/index.css b/extensions/markdown-math/preview-styles/index.css new file mode 100644 index 00000000000..183ac334790 --- /dev/null +++ b/extensions/markdown-math/preview-styles/index.css @@ -0,0 +1,8 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +.katex-error { + color: var(--vscode-editorError-foreground); +} diff --git a/extensions/markdown-math/src/extension.ts b/extensions/markdown-math/src/extension.ts new file mode 100644 index 00000000000..196964dd459 --- /dev/null +++ b/extensions/markdown-math/src/extension.ts @@ -0,0 +1,31 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +import * as vscode from 'vscode'; + +const enabledSetting = 'markdown.math.enabled'; + +export function activate(context: vscode.ExtensionContext) { + function isEnabled(): boolean { + const config = vscode.workspace.getConfiguration('markdown'); + console.log(config.get('math.enabled', true)); + return config.get('math.enabled', true); + } + + vscode.workspace.onDidChangeConfiguration(e => { + if (e.affectsConfiguration(enabledSetting)) { + vscode.commands.executeCommand('markdown.api.reloadPlugins'); + } + }, undefined, context.subscriptions); + + return { + extendMarkdownIt(md: any) { + if (isEnabled()) { + const katex = require('@iktakahiro/markdown-it-katex'); + return md.use(katex); + } + return md; + } + }; +} diff --git a/extensions/markdown-math/src/types.d.ts b/extensions/markdown-math/src/types.d.ts new file mode 100644 index 00000000000..711cf9c84be --- /dev/null +++ b/extensions/markdown-math/src/types.d.ts @@ -0,0 +1 @@ +/// diff --git a/extensions/markdown-math/tsconfig.json b/extensions/markdown-math/tsconfig.json new file mode 100644 index 00000000000..1decd91e333 --- /dev/null +++ b/extensions/markdown-math/tsconfig.json @@ -0,0 +1,17 @@ +{ + "extends": "../tsconfig.base.json", + "compilerOptions": { + "outDir": "./out", + "experimentalDecorators": true, + "lib": [ + "es6", + "es2015.promise", + "es2019.array", + "es2020.string", + "dom" + ] + }, + "include": [ + "src/**/*" + ] +} diff --git a/extensions/markdown-math/yarn.lock b/extensions/markdown-math/yarn.lock index 6e45bbe0e7b..f402ca890b7 100644 --- a/extensions/markdown-math/yarn.lock +++ b/extensions/markdown-math/yarn.lock @@ -13,52 +13,14 @@ resolved "https://registry.yarnpkg.com/@types/markdown-it/-/markdown-it-0.0.0.tgz#8f6acaa5e3245e275f684e95deb3e518d1c6ab16" integrity sha1-j2rKpeMkXidfaE6V3rPlGNHGqxY= -argparse@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38" - integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q== - commander@^6.0.0: version "6.2.1" resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c" integrity sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA== -entities@~2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/entities/-/entities-2.1.0.tgz#992d3129cf7df6870b96c57858c249a120f8b8b5" - integrity sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w== - katex@^0.13.0: version "0.13.0" resolved "https://registry.yarnpkg.com/katex/-/katex-0.13.0.tgz#62900e56c1ad8fdf7da23399e50d7a7b690b39ab" integrity sha512-6cHbzbegYgS9vvVGuH8UA+o97X+ZshtboSqJJCdq7trBYzuD75JNwr7Ef606xkUjecPPhFnyB+afx1dVafielg== dependencies: commander "^6.0.0" - -linkify-it@^3.0.1: - version "3.0.2" - resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-3.0.2.tgz#f55eeb8bc1d3ae754049e124ab3bb56d97797fb8" - integrity sha512-gDBO4aHNZS6coiZCKVhSNh43F9ioIL4JwRjLZPkoLIY4yZFwg264Y5lu2x6rb1Js42Gh6Yqm2f6L2AJcnkzinQ== - dependencies: - uc.micro "^1.0.1" - -markdown-it@^12.0.4: - version "12.0.4" - resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-12.0.4.tgz#eec8247d296327eac3ba9746bdeec9cfcc751e33" - integrity sha512-34RwOXZT8kyuOJy25oJNJoulO8L0bTHYWXcdZBYZqFnjIy3NgjeoM3FmPXIOFQ26/lSHYMr8oc62B6adxXcb3Q== - dependencies: - argparse "^2.0.1" - entities "~2.1.0" - linkify-it "^3.0.1" - mdurl "^1.0.1" - uc.micro "^1.0.5" - -mdurl@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e" - integrity sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4= - -uc.micro@^1.0.1, uc.micro@^1.0.5: - version "1.0.6" - resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac" - integrity sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==