fix(explorer): show file name instead of slug if no file data (closes #1822)
| | |
| | | left: [ |
| | | Component.PageTitle(), |
| | | Component.MobileOnly(Component.Spacer()), |
| | | Component.Search(), |
| | | Component.Darkmode(), |
| | | Component.Flex({ |
| | | components: [ |
| | | { |
| | | Component: Component.Search(), |
| | | grow: true, |
| | | }, |
| | | { Component: Component.Darkmode() }, |
| | | ], |
| | | }), |
| | | Component.Explorer(), |
| | | ], |
| | | right: [], |
| | |
| | | export type ContentIndexMap = Map<FullSlug, ContentDetails> |
| | | export type ContentDetails = { |
| | | slug: FullSlug |
| | | filePath: FilePath |
| | | title: string |
| | | links: SimpleSlug[] |
| | | tags: string[] |
| | |
| | | if (opts?.includeEmptyFiles || (file.data.text && file.data.text !== "")) { |
| | | linkIndex.set(slug, { |
| | | slug, |
| | | filePath: file.data.filePath!, |
| | | title: file.data.frontmatter?.title!, |
| | | links: file.data.links ?? [], |
| | | tags: file.data.frontmatter?.tags ?? [], |
| | |
| | | interface TestData { |
| | | title: string |
| | | slug: string |
| | | filePath: string |
| | | } |
| | | |
| | | describe("FileTrie", () => { |
| | |
| | | const data = { |
| | | title: "Test Title", |
| | | slug: "test", |
| | | filePath: "test.md", |
| | | } |
| | | |
| | | trie.add(data) |
| | |
| | | const data = { |
| | | title: "Test Title", |
| | | slug: "test", |
| | | filePath: "test.md", |
| | | } |
| | | |
| | | trie.add(data) |
| | |
| | | const data = { |
| | | title: "Test", |
| | | slug: "test", |
| | | filePath: "test.md", |
| | | } |
| | | |
| | | trie.add(data) |
| | |
| | | const data = { |
| | | title: "Index", |
| | | slug: "index", |
| | | filePath: "index.md", |
| | | } |
| | | |
| | | trie.add(data) |
| | |
| | | const data1 = { |
| | | title: "Nested", |
| | | slug: "folder/test", |
| | | filePath: "folder/test.md", |
| | | } |
| | | |
| | | const data2 = { |
| | | title: "Really nested index", |
| | | slug: "a/b/c/index", |
| | | filePath: "a/b/c/index.md", |
| | | } |
| | | |
| | | trie.add(data1) |
| | |
| | | |
| | | describe("filter", () => { |
| | | test("should filter nodes based on condition", () => { |
| | | const data1 = { title: "Test1", slug: "test1" } |
| | | const data2 = { title: "Test2", slug: "test2" } |
| | | const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" } |
| | | const data2 = { title: "Test2", slug: "test2", filePath: "test2.md" } |
| | | |
| | | trie.add(data1) |
| | | trie.add(data2) |
| | |
| | | |
| | | describe("map", () => { |
| | | test("should apply function to all nodes", () => { |
| | | const data1 = { title: "Test1", slug: "test1" } |
| | | const data2 = { title: "Test2", slug: "test2" } |
| | | const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" } |
| | | const data2 = { title: "Test2", slug: "test2", filePath: "test2.md" } |
| | | |
| | | trie.add(data1) |
| | | trie.add(data2) |
| | |
| | | }) |
| | | |
| | | test("map over folders should work", () => { |
| | | const data1 = { title: "Test1", slug: "test1" } |
| | | const data2 = { title: "Test2", slug: "a/b/test2" } |
| | | const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" } |
| | | const data2 = { |
| | | title: "Test2", |
| | | slug: "a/b-with-space/test2", |
| | | filePath: "a/b with space/test2.md", |
| | | } |
| | | |
| | | trie.add(data1) |
| | | trie.add(data2) |
| | |
| | | |
| | | assert.strictEqual(trie.children[0].displayName, "File: Test1") |
| | | assert.strictEqual(trie.children[1].displayName, "Folder: a") |
| | | assert.strictEqual(trie.children[1].children[0].displayName, "Folder: b") |
| | | assert.strictEqual(trie.children[1].children[0].displayName, "Folder: b with space") |
| | | assert.strictEqual(trie.children[1].children[0].children[0].displayName, "File: Test2") |
| | | }) |
| | | }) |
| | | |
| | | describe("entries", () => { |
| | | test("should return all entries", () => { |
| | | const data1 = { title: "Test1", slug: "test1" } |
| | | const data2 = { title: "Test2", slug: "a/b/test2" } |
| | | const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" } |
| | | const data2 = { |
| | | title: "Test2", |
| | | slug: "a/b-with-space/test2", |
| | | filePath: "a/b with space/test2.md", |
| | | } |
| | | |
| | | trie.add(data1) |
| | | trie.add(data2) |
| | |
| | | ["index", trie.data], |
| | | ["test1", data1], |
| | | ["a/index", null], |
| | | ["a/b/index", null], |
| | | ["a/b/test2", data2], |
| | | ["a/b-with-space/index", null], |
| | | ["a/b-with-space/test2", data2], |
| | | ], |
| | | ) |
| | | }) |
| | |
| | | const data1 = { |
| | | title: "Root", |
| | | slug: "index", |
| | | filePath: "index.md", |
| | | } |
| | | const data2 = { |
| | | title: "Test", |
| | | slug: "folder/subfolder/test", |
| | | filePath: "folder/subfolder/test.md", |
| | | } |
| | | const data3 = { |
| | | title: "Folder Index", |
| | | slug: "abc/index", |
| | | filePath: "abc/index.md", |
| | | } |
| | | |
| | | trie.add(data1) |
| | |
| | | |
| | | describe("sort", () => { |
| | | test("should sort nodes according to sort function", () => { |
| | | const data1 = { title: "A", slug: "a" } |
| | | const data2 = { title: "B", slug: "b" } |
| | | const data3 = { title: "C", slug: "c" } |
| | | const data1 = { title: "A", slug: "a", filePath: "a.md" } |
| | | const data2 = { title: "B", slug: "b", filePath: "b.md" } |
| | | const data3 = { title: "C", slug: "c", filePath: "c.md" } |
| | | |
| | | trie.add(data3) |
| | | trie.add(data1) |
| | |
| | | interface FileTrieData { |
| | | slug: string |
| | | title: string |
| | | filePath: string |
| | | } |
| | | |
| | | export class FileTrieNode<T extends FileTrieData = ContentDetails> { |
| | |
| | | children: Array<FileTrieNode<T>> |
| | | |
| | | private slugSegments: string[] |
| | | // prefer showing the file path segment over the slug segment |
| | | // so that folders that dont have index files can be shown as is |
| | | // without dashes in the slug |
| | | private fileSegmentHint?: string |
| | | private displayNameOverride?: string |
| | | data: T | null |
| | | |
| | |
| | | } |
| | | |
| | | get displayName(): string { |
| | | return this.displayNameOverride ?? this.data?.title ?? this.slugSegment ?? "" |
| | | const nonIndexTitle = this.data?.title === "index" ? undefined : this.data?.title |
| | | return ( |
| | | this.displayNameOverride ?? nonIndexTitle ?? this.fileSegmentHint ?? this.slugSegment ?? "" |
| | | ) |
| | | } |
| | | |
| | | set displayName(name: string) { |
| | |
| | | // recursive case, we are not at the end of the path |
| | | const child = |
| | | this.children.find((c) => c.slugSegment === segment) ?? this.makeChild(path, undefined) |
| | | |
| | | const fileParts = file.filePath.split("/") |
| | | child.fileSegmentHint = fileParts.at(-path.length) |
| | | child.insert(path.slice(1), file) |
| | | } |
| | | } |