You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Summarize the whole topic in less than 300 characters for SEO purpose
7
-
MetaDescription: Learn how to extend Visual Studio Code's built-in Markdown preview.
7
+
MetaDescription: Extend Visual Studio CodeMarkdown features with preview styles, scripts, markdown-it plugins, and interactive code block editors.
8
8
---
9
9
10
10
# Markdown Extension
11
11
12
-
Markdown extensions allow you to extend and enhance Visual Studio Code's built-in Markdown preview. This includes changing the look of the preview or adding support for new Markdown syntax.
12
+
Markdown extensions allow you to extend and enhance Visual Studio Code's built-in Markdown support. You can change the look of the Markdown preview, add support for new Markdown syntax, and contribute interactive editors for fenced code blocks.
13
13
14
14
## Changing the look of the Markdown preview with CSS
15
15
@@ -85,3 +85,250 @@ For advanced functionality, extensions may contribute scripts that are executed
85
85
Contributed scripts are loaded asynchronously and reloaded on every content change.
86
86
87
87
The [Markdown Preview Mermaid Support](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-mermaid) extension demonstrates using scripts to add [Mermaid](https://mermaid.js.org) diagrams and flowchart support to the markdown preview. You can review the Mermaid extension's source code on [GitHub](https://github.com/mjbvz/vscode-markdown-mermaid).
88
+
89
+
## Add code block editors (Experimental)
90
+
91
+
Extensions can replace fenced code blocks with interactive, iframe-based editors in the Markdown editor. For example, an extension can provide a form editor for JSON, a diagram editor, or a task progress view. The code block remains the canonical document content and stays synchronized with the contributed editor.
92
+
93
+
> [!NOTE]
94
+
> Markdown code block editors are experimental and apply to the Markdown editor, not the standard Markdown preview. The contribution point and extension export API might change.
95
+
96
+
Code block editors only load in a [trusted workspace](/docs/editing/workspaces/workspace-trust.md). In Restricted Mode, the Markdown editor does not load contributed code block editors or their resources.
97
+
98
+
### Register a static code block editor
99
+
100
+
Use the `markdown.codeBlockEditorProviders` contribution point to select fenced code blocks and provide an HTML entry point:
101
+
102
+
```json
103
+
{
104
+
"contributes": {
105
+
"markdown.codeBlockEditorProviders": [
106
+
{
107
+
"id": "taskProgress",
108
+
"selector": {
109
+
"language": "task-progress"
110
+
},
111
+
"source": {
112
+
"kind": "static",
113
+
"entrypoint": "./editor/index.html"
114
+
},
115
+
"runtimeKey": "task-progress-v1",
116
+
"contentType": "text",
117
+
"initialHeight": 80
118
+
}
119
+
]
120
+
}
121
+
}
122
+
```
123
+
124
+
This contribution replaces fenced code blocks whose info string is exactly `task-progress`:
125
+
126
+
````markdown
127
+
```task-progress
128
+
- [x] Create the extension
129
+
- [ ] Publish the extension
130
+
```
131
+
````
132
+
133
+
The provider supports these properties:
134
+
135
+
| Property | Required | Description |
136
+
| --- | --- | --- |
137
+
|`id`| Yes | Identifies the provider within the extension. The value must not be empty. |
138
+
|`selector`| Yes | Selects an exact info string with `language`, or all info strings that start with a value by using `languagePrefix`. Specify one selector type. Values must not be empty. |
139
+
|`source`| Yes | Uses an extension-relative HTML `entrypoint` for a `static` provider, or an extension export API for an `exportApi` provider. |
140
+
|`runtimeKey`| No | Identifies compatible iframe runtimes that can be reused. The value must contain 1 to 256 characters. |
141
+
|`contentType`| No | Represents code block content as `text` or `json`. The default is `text`. |
142
+
|`initialHeight`| No | Reserves a positive height in pixels until the editor reports its measured height. |
143
+
|`sandbox`| No | Sets the maximum optional iframe permissions the provider can request. Supported properties are `forms`, `downloads`, `pointerLock`, and `clipboardWrite`. Each permission defaults to `false`. |
144
+
145
+
The `entrypoint` path is relative to the extension root. Relative scripts, stylesheets, and other assets in the HTML resolve from the entry point's directory.
146
+
147
+
### Connect the iframe editor
148
+
149
+
Install and bundle the experimental `@vscode/web-editors` package into the iframe application. The package synchronizes content, read-only state, and sizing between the code block and the iframe.
150
+
151
+
The HTML entry point can load a bundled JavaScript module:
Use an `exportApi` source when the extension host needs to choose the HTML dynamically or communicate with an iframe runtime. Register the source with API version 2:
219
+
220
+
```json
221
+
{
222
+
"contributes": {
223
+
"markdown.codeBlockEditorProviders": [
224
+
{
225
+
"id": "taskProgress",
226
+
"selector": {
227
+
"languagePrefix": "task-progress"
228
+
},
229
+
"source": {
230
+
"kind": "exportApi",
231
+
"apiVersion": 2
232
+
},
233
+
"runtimeKey": "task-progress-v1",
234
+
"contentType": "text",
235
+
"initialHeight": 80
236
+
}
237
+
]
238
+
}
239
+
}
240
+
```
241
+
242
+
Return the `markdownCodeBlockEditors.apiV2` API from the extension's `activate` function. The provider ID returned by `getProvider` must match the contribution's `id`.
function isReadyMessage(message:unknown):messageis { type: 'ready' } {
303
+
returntypeofmessage==='object'
304
+
&&message!==null
305
+
&&'type'inmessage
306
+
&&message.type==='ready';
307
+
}
308
+
```
309
+
310
+
The `resolve` method receives the provider ID, full fenced code block info string, and Markdown document URI. It can return HTML directly with `content.html` and an optional `content.baseUri`, or return an HTML file with `content.uri`. Returned resources must be within the extension or workspace.
311
+
312
+
The resolved editor can also override `runtimeKey`, `contentType`, `initialHeight`, and `sandbox`. A returned `runtimeKey` must also contain 1 to 256 characters. A returned sandbox permission is granted only when the contribution also permits it.
313
+
314
+
API version 2 providers can implement `createHostTransport` for bidirectional notifications between the extension host and an iframe runtime. In the iframe, use the optional `WebEditorClient.hostTransport`:
315
+
316
+
```ts
317
+
const transport =client.hostTransport;
318
+
if (transport) {
319
+
transport.onMessage(message=> {
320
+
console.log('Message from the extension host', message);
321
+
});
322
+
transport.sendMessage({ type: 'ready' });
323
+
}
324
+
```
325
+
326
+
The host buffers messages while `createHostTransport` initializes. Return a `vscode.Disposable` to clean up listeners and resources when the iframe runtime is disposed. Use `transport.onDidDispose` when the extension must react immediately to runtime disposal.
327
+
328
+
### Reuse iframe runtimes
329
+
330
+
The Markdown editor virtualizes and pools physical iframes. Editors with the same `runtimeKey` can reuse an iframe runtime as code blocks enter and leave the viewport. Use the same key only when the editors have compatible HTML, scripts, and runtime behavior.
331
+
332
+
For a static provider, the default runtime key combines the provider ID and extension version. For an exported provider, the resolved `runtimeKey` takes precedence over the contribution value. If neither is present, the Markdown editor derives a key from the provider, extension version, and language.
333
+
334
+
Runtime reuse preserves the iframe application and host transport, but the web editor protocol updates the code block content and read-only state for each logical editor. Do not store code block-specific state outside the synchronized content unless you reset that state when the content changes.
|[Custom Data Extension](https://code.visualstudio.com/api/extension-guides/custom-data-extension)| contributes.html.customData<br>contributes.css.customData |
0 commit comments