Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💄 remove setting renderer knowledge from markdown document renderer #214955

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ export class ExtensionEditor extends EditorPane {
return '';
}

const content = await renderMarkdownDocument(contents, this.extensionService, this.languageService, extension.type !== ExtensionType.System, false, token);
const content = await renderMarkdownDocument(contents, this.extensionService, this.languageService, { shouldSanitize: extension.type !== ExtensionType.System, token });
if (token?.isCancellationRequested) {
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { ILanguageService } from 'vs/editor/common/languages/language';
import { tokenizeToString } from 'vs/editor/common/languages/textToHtmlTokenizer';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { escape } from 'vs/base/common/strings';
import { SimpleSettingRenderer } from 'vs/workbench/contrib/markdown/browser/markdownSettingRenderer';

export const DEFAULT_MARKDOWN_STYLES = `
body {
Expand Down Expand Up @@ -184,6 +183,13 @@ function sanitize(documentContent: string, allowUnknownProtocols: boolean): stri
}
}

interface IRenderMarkdownDocumentOptions {
readonly shouldSanitize?: boolean;
readonly allowUnknownProtocols?: boolean;
readonly renderer?: marked.Renderer;
readonly token?: CancellationToken;
}

/**
* Renders a string of markdown as a document.
*
Expand All @@ -193,10 +199,7 @@ export async function renderMarkdownDocument(
text: string,
extensionService: IExtensionService,
languageService: ILanguageService,
shouldSanitize: boolean = true,
allowUnknownProtocols: boolean = false,
token?: CancellationToken,
settingRenderer?: SimpleSettingRenderer
options?: IRenderMarkdownDocumentOptions
): Promise<string> {

const highlight = (code: string, lang: string | undefined, callback: ((error: any, code: string) => void) | undefined): any => {
Expand All @@ -210,7 +213,7 @@ export async function renderMarkdownDocument(
}

extensionService.whenInstalledExtensionsRegistered().then(async () => {
if (token?.isCancellationRequested) {
if (options?.token?.isCancellationRequested) {
callback(null, '');
return;
}
Expand All @@ -222,16 +225,11 @@ export async function renderMarkdownDocument(
return '';
};

const renderer = new marked.Renderer();
if (settingRenderer) {
renderer.html = settingRenderer.getHtmlRenderer();
}

return new Promise<string>((resolve, reject) => {
marked(text, { highlight, renderer }, (err, value) => err ? reject(err) : resolve(value));
marked(text, { highlight, renderer: options?.renderer }, (err, value) => err ? reject(err) : resolve(value));
}).then(raw => {
if (shouldSanitize) {
return sanitize(raw, allowUnknownProtocols);
if (options?.shouldSanitize ?? true) {
return sanitize(raw, options?.allowUnknownProtocols ?? false);
} else {
return raw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { SimpleSettingRenderer } from 'vs/workbench/contrib/markdown/browser/mar
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { Schemas } from 'vs/base/common/network';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { marked } from 'vs/base/common/marked/marked';

export class ReleaseNotesManager {
private readonly _simpleSettingRenderer: SimpleSettingRenderer;
Expand Down Expand Up @@ -249,7 +250,10 @@ export class ReleaseNotesManager {

private async renderBody(text: string) {
const nonce = generateUuid();
const content = await renderMarkdownDocument(text, this._extensionService, this._languageService, false, undefined, undefined, this._simpleSettingRenderer);
const renderer = new marked.Renderer();
renderer.html = this._simpleSettingRenderer.getHtmlRenderer();

const content = await renderMarkdownDocument(text, this._extensionService, this._languageService, { shouldSanitize: false, renderer });
const colorMap = TokenizationRegistry.getColorMap();
const css = colorMap ? generateTokensCSSForColorMap(colorMap) : '';
const showReleaseNotes = Boolean(this._configurationService.getValue<boolean>('update.showReleaseNotes'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class GettingStartedDetailsRenderer {
private async readAndCacheStepMarkdown(path: URI, base: URI): Promise<string> {
if (!this.mdCache.has(path)) {
const contents = await this.readContentsOfPath(path);
const markdownContents = await renderMarkdownDocument(transformUris(contents, base), this.extensionService, this.languageService, true, true);
const markdownContents = await renderMarkdownDocument(transformUris(contents, base), this.extensionService, this.languageService, { allowUnknownProtocols: true });
this.mdCache.set(path, markdownContents);
}
return assertIsDefined(this.mdCache.get(path));
Expand Down
Loading