Skip to content

Commit 270a5dc

Browse files
committed
fix(explorer): show file name instead of slug if no file data (closes #1822)
1 parent bfa938c commit 270a5dc

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

quartz.layout.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ export const defaultListPageLayout: PageLayout = {
4949
left: [
5050
Component.PageTitle(),
5151
Component.MobileOnly(Component.Spacer()),
52-
Component.Search(),
53-
Component.Darkmode(),
52+
Component.Flex({
53+
components: [
54+
{
55+
Component: Component.Search(),
56+
grow: true,
57+
},
58+
{ Component: Component.Darkmode() },
59+
],
60+
}),
5461
Component.Explorer(),
5562
],
5663
right: [],

quartz/plugins/emitters/contentIndex.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import DepGraph from "../../depgraph"
1212
export type ContentIndexMap = Map<FullSlug, ContentDetails>
1313
export type ContentDetails = {
1414
slug: FullSlug
15+
filePath: FilePath
1516
title: string
1617
links: SimpleSlug[]
1718
tags: string[]
@@ -126,6 +127,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
126127
if (opts?.includeEmptyFiles || (file.data.text && file.data.text !== "")) {
127128
linkIndex.set(slug, {
128129
slug,
130+
filePath: file.data.filePath!,
129131
title: file.data.frontmatter?.title!,
130132
links: file.data.links ?? [],
131133
tags: file.data.frontmatter?.tags ?? [],

quartz/util/fileTrie.test.ts

+32-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FileTrieNode } from "./fileTrie"
55
interface TestData {
66
title: string
77
slug: string
8+
filePath: string
89
}
910

1011
describe("FileTrie", () => {
@@ -26,6 +27,7 @@ describe("FileTrie", () => {
2627
const data = {
2728
title: "Test Title",
2829
slug: "test",
30+
filePath: "test.md",
2931
}
3032

3133
trie.add(data)
@@ -36,6 +38,7 @@ describe("FileTrie", () => {
3638
const data = {
3739
title: "Test Title",
3840
slug: "test",
41+
filePath: "test.md",
3942
}
4043

4144
trie.add(data)
@@ -49,6 +52,7 @@ describe("FileTrie", () => {
4952
const data = {
5053
title: "Test",
5154
slug: "test",
55+
filePath: "test.md",
5256
}
5357

5458
trie.add(data)
@@ -61,6 +65,7 @@ describe("FileTrie", () => {
6165
const data = {
6266
title: "Index",
6367
slug: "index",
68+
filePath: "index.md",
6469
}
6570

6671
trie.add(data)
@@ -72,11 +77,13 @@ describe("FileTrie", () => {
7277
const data1 = {
7378
title: "Nested",
7479
slug: "folder/test",
80+
filePath: "folder/test.md",
7581
}
7682

7783
const data2 = {
7884
title: "Really nested index",
7985
slug: "a/b/c/index",
86+
filePath: "a/b/c/index.md",
8087
}
8188

8289
trie.add(data1)
@@ -103,8 +110,8 @@ describe("FileTrie", () => {
103110

104111
describe("filter", () => {
105112
test("should filter nodes based on condition", () => {
106-
const data1 = { title: "Test1", slug: "test1" }
107-
const data2 = { title: "Test2", slug: "test2" }
113+
const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
114+
const data2 = { title: "Test2", slug: "test2", filePath: "test2.md" }
108115

109116
trie.add(data1)
110117
trie.add(data2)
@@ -117,8 +124,8 @@ describe("FileTrie", () => {
117124

118125
describe("map", () => {
119126
test("should apply function to all nodes", () => {
120-
const data1 = { title: "Test1", slug: "test1" }
121-
const data2 = { title: "Test2", slug: "test2" }
127+
const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
128+
const data2 = { title: "Test2", slug: "test2", filePath: "test2.md" }
122129

123130
trie.add(data1)
124131
trie.add(data2)
@@ -134,8 +141,12 @@ describe("FileTrie", () => {
134141
})
135142

136143
test("map over folders should work", () => {
137-
const data1 = { title: "Test1", slug: "test1" }
138-
const data2 = { title: "Test2", slug: "a/b/test2" }
144+
const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
145+
const data2 = {
146+
title: "Test2",
147+
slug: "a/b-with-space/test2",
148+
filePath: "a/b with space/test2.md",
149+
}
139150

140151
trie.add(data1)
141152
trie.add(data2)
@@ -150,15 +161,19 @@ describe("FileTrie", () => {
150161

151162
assert.strictEqual(trie.children[0].displayName, "File: Test1")
152163
assert.strictEqual(trie.children[1].displayName, "Folder: a")
153-
assert.strictEqual(trie.children[1].children[0].displayName, "Folder: b")
164+
assert.strictEqual(trie.children[1].children[0].displayName, "Folder: b with space")
154165
assert.strictEqual(trie.children[1].children[0].children[0].displayName, "File: Test2")
155166
})
156167
})
157168

158169
describe("entries", () => {
159170
test("should return all entries", () => {
160-
const data1 = { title: "Test1", slug: "test1" }
161-
const data2 = { title: "Test2", slug: "a/b/test2" }
171+
const data1 = { title: "Test1", slug: "test1", filePath: "test1.md" }
172+
const data2 = {
173+
title: "Test2",
174+
slug: "a/b-with-space/test2",
175+
filePath: "a/b with space/test2.md",
176+
}
162177

163178
trie.add(data1)
164179
trie.add(data2)
@@ -170,8 +185,8 @@ describe("FileTrie", () => {
170185
["index", trie.data],
171186
["test1", data1],
172187
["a/index", null],
173-
["a/b/index", null],
174-
["a/b/test2", data2],
188+
["a/b-with-space/index", null],
189+
["a/b-with-space/test2", data2],
175190
],
176191
)
177192
})
@@ -182,14 +197,17 @@ describe("FileTrie", () => {
182197
const data1 = {
183198
title: "Root",
184199
slug: "index",
200+
filePath: "index.md",
185201
}
186202
const data2 = {
187203
title: "Test",
188204
slug: "folder/subfolder/test",
205+
filePath: "folder/subfolder/test.md",
189206
}
190207
const data3 = {
191208
title: "Folder Index",
192209
slug: "abc/index",
210+
filePath: "abc/index.md",
193211
}
194212

195213
trie.add(data1)
@@ -208,9 +226,9 @@ describe("FileTrie", () => {
208226

209227
describe("sort", () => {
210228
test("should sort nodes according to sort function", () => {
211-
const data1 = { title: "A", slug: "a" }
212-
const data2 = { title: "B", slug: "b" }
213-
const data3 = { title: "C", slug: "c" }
229+
const data1 = { title: "A", slug: "a", filePath: "a.md" }
230+
const data2 = { title: "B", slug: "b", filePath: "b.md" }
231+
const data3 = { title: "C", slug: "c", filePath: "c.md" }
214232

215233
trie.add(data3)
216234
trie.add(data1)

quartz/util/fileTrie.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import { FullSlug, joinSegments } from "./path"
44
interface FileTrieData {
55
slug: string
66
title: string
7+
filePath: string
78
}
89

910
export class FileTrieNode<T extends FileTrieData = ContentDetails> {
1011
isFolder: boolean
1112
children: Array<FileTrieNode<T>>
1213

1314
private slugSegments: string[]
15+
// prefer showing the file path segment over the slug segment
16+
// so that folders that dont have index files can be shown as is
17+
// without dashes in the slug
18+
private fileSegmentHint?: string
1419
private displayNameOverride?: string
1520
data: T | null
1621

@@ -23,7 +28,10 @@ export class FileTrieNode<T extends FileTrieData = ContentDetails> {
2328
}
2429

2530
get displayName(): string {
26-
return this.displayNameOverride ?? this.data?.title ?? this.slugSegment ?? ""
31+
const nonIndexTitle = this.data?.title === "index" ? undefined : this.data?.title
32+
return (
33+
this.displayNameOverride ?? nonIndexTitle ?? this.fileSegmentHint ?? this.slugSegment ?? ""
34+
)
2735
}
2836

2937
set displayName(name: string) {
@@ -69,6 +77,9 @@ export class FileTrieNode<T extends FileTrieData = ContentDetails> {
6977
// recursive case, we are not at the end of the path
7078
const child =
7179
this.children.find((c) => c.slugSegment === segment) ?? this.makeChild(path, undefined)
80+
81+
const fileParts = file.filePath.split("/")
82+
child.fileSegmentHint = fileParts.at(-path.length)
7283
child.insert(path.slice(1), file)
7384
}
7485
}

0 commit comments

Comments
 (0)