Fix opening encoded markdown fragment in preview (#178465)

Fixes #178340
This commit is contained in:
Matt Bierner 2023-03-27 23:59:30 -07:00 committed by GitHub
parent b931935042
commit 87a6bda310
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View file

@ -77,10 +77,16 @@ onceDocumentLoaded(() => {
doAfterImagesLoaded(() => { doAfterImagesLoaded(() => {
// Try to scroll to fragment if available // Try to scroll to fragment if available
if (settings.settings.fragment) { if (settings.settings.fragment) {
let fragment: string;
try {
fragment = encodeURIComponent(settings.settings.fragment);
} catch {
fragment = settings.settings.fragment;
}
state.fragment = undefined; state.fragment = undefined;
vscode.setState(state); vscode.setState(state);
const element = getLineElementForFragment(settings.settings.fragment, documentVersion); const element = getLineElementForFragment(fragment, documentVersion);
if (element) { if (element) {
scrollDisabledCount += 1; scrollDisabledCount += 1;
scrollToRevealSourceLine(element.line, documentVersion, settings); scrollToRevealSourceLine(element.line, documentVersion, settings);

View file

@ -37,7 +37,7 @@ export class PreviewDocumentVersion {
interface MarkdownPreviewDelegate { interface MarkdownPreviewDelegate {
getTitle?(resource: vscode.Uri): string; getTitle?(resource: vscode.Uri): string;
getAdditionalState(): {}; getAdditionalState(): {};
openPreviewLinkToMarkdownFile(markdownLink: vscode.Uri, fragment: string): void; openPreviewLinkToMarkdownFile(markdownLink: vscode.Uri, fragment: string | undefined): void;
} }
class MarkdownPreview extends Disposable implements WebviewResourceProvider { class MarkdownPreview extends Disposable implements WebviewResourceProvider {
@ -410,7 +410,7 @@ class MarkdownPreview extends Disposable implements WebviewResourceProvider {
try { try {
const doc = await vscode.workspace.openTextDocument(vscode.Uri.from(resolved.uri)); const doc = await vscode.workspace.openTextDocument(vscode.Uri.from(resolved.uri));
if (isMarkdownFile(doc)) { if (isMarkdownFile(doc)) {
return this._delegate.openPreviewLinkToMarkdownFile(doc.uri, resolved.fragment ?? ''); return this._delegate.openPreviewLinkToMarkdownFile(doc.uri, resolved.fragment ? decodeURIComponent(resolved.fragment) : undefined);
} }
} catch { } catch {
// Noop // Noop