Fix notebook links to other docs in edited markdown cells (#153052)

Fixes #148199

This makes us resolve links in notebooks relative to the notebook document instead of relaitve to the cell
This commit is contained in:
Matt Bierner 2022-06-23 17:55:08 -07:00 committed by GitHub
parent ab7bc9fb0b
commit 9b7696cc9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 5 deletions

View file

@ -38,7 +38,7 @@ export interface ReferenceHref {
export type LinkHref = ExternalHref | InternalHref | ReferenceHref;
function parseLink(
function resolveLink(
document: ITextDocument,
link: string,
): ExternalHref | InternalHref | undefined {
@ -85,6 +85,16 @@ function parseLink(
return undefined;
}
// If we are in a notebook cell, resolve relative to notebook instead
if (resourceUri.scheme === Schemes.notebookCell) {
const notebook = vscode.workspace.notebookDocuments
.find(notebook => notebook.getCells().some(cell => cell.document === document));
if (notebook) {
resourceUri = resourceUri.with({ scheme: notebook.uri.scheme });
}
}
return {
kind: 'internal',
path: resourceUri.with({ fragment: '' }),
@ -144,7 +154,7 @@ function extractDocumentLink(
const linkStart = document.positionAt(offset);
const linkEnd = document.positionAt(offset + link.length);
try {
const linkTarget = parseLink(document, link);
const linkTarget = resolveLink(document, link);
if (!linkTarget) {
return undefined;
}
@ -323,7 +333,7 @@ export class MdLinkComputer {
for (const match of text.matchAll(autoLinkPattern)) {
const link = match[1];
const linkTarget = parseLink(document, link);
const linkTarget = resolveLink(document, link);
if (linkTarget) {
const offset = (match.index ?? 0) + 1;
const linkStart = document.positionAt(offset);
@ -426,7 +436,7 @@ export class MdLinkComputer {
if (noLinkRanges.contains(hrefRange.start)) {
continue;
}
const target = parseLink(document, text);
const target = resolveLink(document, text);
if (target) {
yield {
kind: 'definition',

View file

@ -10,6 +10,7 @@ import { githubSlugifier, Slug, Slugifier } from './slugify';
import { ITextDocument } from './types/textDocument';
import { Disposable } from './util/dispose';
import { isMarkdownFile } from './util/file';
import { Schemes } from './util/schemes';
import { MdDocumentInfoCache } from './util/workspaceCache';
import { IMdWorkspace } from './workspace';
@ -71,7 +72,7 @@ export class TableOfContents {
}
public static async createForDocumentOrNotebook(parser: IMdParser, document: ITextDocument): Promise<TableOfContents> {
if (document.uri.scheme === 'vscode-notebook-cell') {
if (document.uri.scheme === Schemes.notebookCell) {
const notebook = vscode.workspace.notebookDocuments
.find(notebook => notebook.getCells().some(cell => cell.document === document));

View file

@ -14,6 +14,7 @@ export const Schemes = {
data: 'data:',
vscode: 'vscode:',
'vscode-insiders': 'vscode-insiders:',
notebookCell: 'vscode-notebook-cell',
};
const knownSchemes = [