Move toc expanding for notebooks logic into toc

This commit is contained in:
Matt Bierner
2022-01-19 13:13:53 -08:00
parent a2d7dfaf35
commit 280658327f
2 changed files with 27 additions and 26 deletions

View File

@@ -6,6 +6,7 @@
import * as vscode from 'vscode';
import { MarkdownEngine } from './markdownEngine';
import { githubSlugifier, Slug } from './slugify';
import { isMarkdownFile } from './util/file';
export interface TocEntry {
readonly slug: Slug;
@@ -29,11 +30,33 @@ export interface SkinnyTextDocument {
}
export class TableOfContents {
public static async create(engine: MarkdownEngine, document: SkinnyTextDocument): Promise<TableOfContents> {
public static async create(engine: MarkdownEngine, document: SkinnyTextDocument,): Promise<TableOfContents> {
const entries = await this.buildToc(engine, document);
return new TableOfContents(entries);
}
public static async createForDocumentOrNotebook(engine: MarkdownEngine, document: SkinnyTextDocument): Promise<TableOfContents> {
if (document.uri.scheme === 'vscode-notebook-cell') {
const notebook = vscode.workspace.notebookDocuments
.find(notebook => notebook.getCells().some(cell => cell.document === document));
if (notebook) {
const entries: TocEntry[] = [];
for (const cell of notebook.getCells()) {
if (cell.kind === vscode.NotebookCellKind.Markup && isMarkdownFile(cell.document)) {
entries.push(...(await this.buildToc(engine, cell.document)));
}
}
return new TableOfContents(entries);
}
}
return this.create(engine, document);
}
private constructor(
public readonly entries: readonly TocEntry[],
) { }