Skip to content

Commit 7403245

Browse files
authored
Show Warning When a markdown.styles fails to load (#27105)
Fixes #8488 **Bug** Currently there is no indication in the markdown preview when a `markdown.styles` element fails to load **Fix** Show an alert then a stylesheet does not load
1 parent edbab0e commit 7403245

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

extensions/markdown/media/loading.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
6+
'use strict';
7+
8+
(function () {
9+
const unloadedStyles = [];
10+
11+
window.onStyleLoadError = (event) => {
12+
const source = event.target.dataset.source;
13+
unloadedStyles.push(source);
14+
};
15+
16+
window.addEventListener('load', () => {
17+
if (!unloadedStyles.length) {
18+
return;
19+
}
20+
const args = [unloadedStyles];
21+
window.parent.postMessage({
22+
command: 'did-click-link',
23+
data: `command:_markdown.onPreviewStyleLoadError?${encodeURIComponent(JSON.stringify(args))}`
24+
}, 'file://');
25+
});
26+
}());

extensions/markdown/src/extension.ts

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { ExtensionContentSecurityPolicyArbiter, PreviewSecuritySelector } from '
1515
import { MDDocumentContentProvider, getMarkdownUri, isMarkdownFile } from './previewContentProvider';
1616
import { TableOfContentsProvider } from './tableOfContentsProvider';
1717
import { Logger } from "./logger";
18+
import * as nls from 'vscode-nls';
19+
const localize = nls.loadMessageBundle();
1820

1921
interface IPackageInfo {
2022
name: string;
@@ -166,6 +168,10 @@ export function activate(context: vscode.ExtensionContext) {
166168
previewSecuritySelector.showSecutitySelectorForWorkspace(resource ? vscode.Uri.parse(resource).query : undefined);
167169
}));
168170

171+
context.subscriptions.push(vscode.commands.registerCommand('_markdown.onPreviewStyleLoadError', (resources: string[]) => {
172+
vscode.window.showWarningMessage(localize('onPreviewStyleLoadError', "Could not load 'markdown.styles': {0}", resources.join(', ')));
173+
}));
174+
169175
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
170176
if (isMarkdownFile(document)) {
171177
const uri = getMarkdownUri(document.uri);

extensions/markdown/src/previewContentProvider.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class MDDocumentContentProvider implements vscode.TextDocumentContentProv
166166
private computeCustomStyleSheetIncludes(uri: vscode.Uri): string {
167167
if (this.config.styles && Array.isArray(this.config.styles)) {
168168
return this.config.styles.map((style) => {
169-
return `<link rel="stylesheet" href="${this.fixHref(uri, style)}" type="text/css" media="screen">`;
169+
return `<link rel="stylesheet" data-source="${style.replace(/"/g, '&quot;')}" onerror="onStyleLoadError(event)" href="${this.fixHref(uri, style)}" type="text/css" media="screen">`;
170170
}).join('\n');
171171
}
172172
return '';
@@ -238,6 +238,7 @@ export class MDDocumentContentProvider implements vscode.TextDocumentContentProv
238238
${csp}
239239
<meta id="vscode-markdown-preview-data" data-settings="${JSON.stringify(initialData).replace(/"/g, '&quot;')}" data-strings="${JSON.stringify(previewStrings).replace(/"/g, '&quot;')}">
240240
<script src="${this.getMediaPath('csp.js')}" nonce="${nonce}"></script>
241+
<script src="${this.getMediaPath('loading.js')}" nonce="${nonce}"></script>
241242
${this.getStyles(uri, nonce)}
242243
<base href="${document.uri.toString(true)}">
243244
</head>

0 commit comments

Comments
 (0)