Skip to content
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
26 changes: 17 additions & 9 deletions extensions/markdown-language-features/src/tableOfContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,23 @@ export class TableOfContents {
.find(notebook => notebook.getCells().some(cell => cell.document === document));

if (notebook) {
const entries: TocEntry[] = [];
return TableOfContents.createForNotebook(parser, notebook);
}
}

for (const cell of notebook.getCells()) {
if (cell.kind === vscode.NotebookCellKind.Markup && isMarkdownFile(cell.document)) {
entries.push(...(await this.buildToc(parser, cell.document)));
}
}
return this.create(parser, document);
}

return new TableOfContents(entries, parser.slugifier);
public static async createForNotebook(parser: IMdParser, notebook: vscode.NotebookDocument): Promise<TableOfContents> {
const entries: TocEntry[] = [];

for (const cell of notebook.getCells()) {
if (cell.kind === vscode.NotebookCellKind.Markup && isMarkdownFile(cell.document)) {
entries.push(...(await this.buildToc(parser, cell.document)));
}
}

return this.create(parser, document);
return new TableOfContents(entries, parser.slugifier);
}

private static async buildToc(parser: IMdParser, document: ITextDocument): Promise<TocEntry[]> {
Expand Down Expand Up @@ -184,7 +188,7 @@ export class MdTableOfContentsProvider extends Disposable {
private readonly _cache: MdDocumentInfoCache<TableOfContents>;

constructor(
parser: IMdParser,
private readonly parser: IMdParser,
workspace: IMdWorkspace,
private readonly logger: ILogger,
) {
Expand All @@ -202,4 +206,8 @@ export class MdTableOfContentsProvider extends Disposable {
public getForDocument(doc: ITextDocument): Promise<TableOfContents> {
return this._cache.getForDocument(doc);
}

public createForNotebook(notebook: vscode.NotebookDocument): Promise<TableOfContents> {
return TableOfContents.createForNotebook(this.parser, notebook);
}
}
26 changes: 26 additions & 0 deletions extensions/markdown-language-features/src/util/openDocumentLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ async function tryOpenMdFile(tocProvider: MdTableOfContentsProvider, resource: v
}

async function tryNavigateToFragmentInActiveEditor(tocProvider: MdTableOfContentsProvider, resource: vscode.Uri): Promise<boolean> {
const notebookEditor = vscode.window.activeNotebookEditor;
if (notebookEditor?.notebook.uri.fsPath === resource.fsPath) {
if (await tryRevealLineInNotebook(tocProvider, notebookEditor, resource.fragment)) {
return true;
}
}

const activeEditor = vscode.window.activeTextEditor;
if (activeEditor?.document.uri.fsPath === resource.fsPath) {
if (isMarkdownFile(activeEditor.document)) {
Expand All @@ -87,6 +94,7 @@ async function tryNavigateToFragmentInActiveEditor(tocProvider: MdTableOfContent
tryRevealLineUsingLineFragment(activeEditor, resource.fragment);
return true;
}

return false;
}

Expand All @@ -102,6 +110,24 @@ function getViewColumn(resource: vscode.Uri): vscode.ViewColumn {
}
}

async function tryRevealLineInNotebook(tocProvider: MdTableOfContentsProvider, editor: vscode.NotebookEditor, fragment: string): Promise<boolean> {
const toc = await tocProvider.createForNotebook(editor.notebook);
const entry = toc.lookup(fragment);
if (!entry) {
return false;
}

const cell = editor.notebook.getCells().find(cell => cell.document.uri.toString() === entry.sectionLocation.uri.toString());
if (!cell) {
return false;
}

const range = new vscode.NotebookRange(cell.index, cell.index);
editor.selection = range;
editor.revealRange(range);
return true;
}

async function tryRevealLineUsingTocFragment(tocProvider: MdTableOfContentsProvider, editor: vscode.TextEditor, fragment: string): Promise<boolean> {
const toc = await tocProvider.getForDocument(editor.document);
const entry = toc.lookup(fragment);
Expand Down