···2727 folderClickBehavior: "collapse", // what happens when you click a folder ("link" to navigate to folder page on click or "collapse" to collapse folder on click)
2828 folderDefaultState: "collapsed", // default state of folders ("collapsed" or "open")
2929 useSavedState: true, // whether to use local storage to save "state" (which folders are opened) of explorer
3030- // Sort order: folders first, then files. Sort folders and files alphabetically
3131- sortFn: (a, b) => {
3232- ... // default implementation shown later
3333- },
3434- filterFn: filterFn: (node) => node.name !== "tags", // filters out 'tags' folder
3535- mapFn: undefined,
3030+ // omitted but shown later
3131+ sortFn: ...,
3232+ filterFn: ...,
3333+ mapFn: ...,
3634 // what order to apply functions in
3735 order: ["filter", "map", "sort"],
3836})
···5452## Advanced customization
55535654This component allows you to fully customize all of its behavior. You can pass a custom `sort`, `filter` and `map` function.
5757-All functions you can pass work with the `FileNode` class, which has the following properties:
5555+All functions you can pass work with the `FileTrieNode` class, which has the following properties:
58565959-```ts title="quartz/components/ExplorerNode.tsx" {2-5}
6060-export class FileNode {
6161- children: FileNode[] // children of current node
6262- name: string // last part of slug
6363- displayName: string // what actually should be displayed in the explorer
6464- file: QuartzPluginData | null // if node is a file, this is the file's metadata. See `QuartzPluginData` for more detail
6565- depth: number // depth of current node
5757+```ts title="quartz/components/Explorer.tsx"
5858+class FileTrieNode {
5959+ isFolder: boolean
6060+ children: Array<FileTrieNode>
6161+ data: ContentDetails | null
6262+}
6363+```
66646767- ... // rest of implementation
6565+```ts title="quartz/plugins/emitters/contentIndex.tsx"
6666+export type ContentDetails = {
6767+ slug: FullSlug
6868+ title: string
6969+ links: SimpleSlug[]
7070+ tags: string[]
7171+ content: string
6872}
6973```
7074···7478// Sort order: folders first, then files. Sort folders and files alphabetically
7579Component.Explorer({
7680 sortFn: (a, b) => {
7777- if ((!a.file && !b.file) || (a.file && b.file)) {
7878- // sensitivity: "base": Only strings that differ in base letters compare as unequal. Examples: a โ b, a = รก, a = A
7979- // numeric: true: Whether numeric collation should be used, such that "1" < "2" < "10"
8181+ if ((!a.isFolder && !b.isFolder) || (a.isFolder && b.isFolder)) {
8082 return a.displayName.localeCompare(b.displayName, undefined, {
8183 numeric: true,
8284 sensitivity: "base",
8385 })
8486 }
8585- if (a.file && !b.file) {
8787+8888+ if (!a.isFolder && b.isFolder) {
8689 return 1
8790 } else {
8891 return -1
···100103Type definitions look like this:
101104102105```ts
103103-sortFn: (a: FileNode, b: FileNode) => number
104104-filterFn: (node: FileNode) => boolean
105105-mapFn: (node: FileNode) => void
106106+type SortFn = (a: FileTrieNode, b: FileTrieNode) => number
107107+type FilterFn = (node: FileTrieNode) => boolean
108108+type MapFn = (node: FileTrieNode) => void
106109```
107110108108-> [!tip]
109109-> You can check if a `FileNode` is a folder or a file like this:
110110->
111111-> ```ts
112112-> if (node.file) {
113113-> // node is a file
114114-> } else {
115115-> // node is a folder
116116-> }
117117-> ```
118118-119111## Basic examples
120112121113These examples show the basic usage of `sort`, `map` and `filter`.
122114123115### Use `sort` to put files first
124116125125-Using this example, the explorer will alphabetically sort everything, but put all **files** above all **folders**.
117117+Using this example, the explorer will alphabetically sort everything.
126118127119```ts title="quartz.layout.ts"
128120Component.Explorer({
129121 sortFn: (a, b) => {
130130- if ((!a.file && !b.file) || (a.file && b.file)) {
131131- return a.displayName.localeCompare(b.displayName)
132132- }
133133- if (a.file && !b.file) {
134134- return -1
135135- } else {
136136- return 1
137137- }
122122+ return a.displayName.localeCompare(b.displayName)
138123 },
139124})
140125```
···146131```ts title="quartz.layout.ts"
147132Component.Explorer({
148133 mapFn: (node) => {
149149- node.displayName = node.displayName.toUpperCase()
134134+ return (node.displayName = node.displayName.toUpperCase())
150135 },
151136})
152137```
153138154139### Remove list of elements (`filter`)
155140156156-Using this example, you can remove elements from your explorer by providing an array of folders/files using the `omit` set.
141141+Using this example, you can remove elements from your explorer by providing an array of folders/files to exclude.
142142+Note that this example filters on the title but you can also do it via slug or any other field available on `FileTrieNode`.
157143158144```ts title="quartz.layout.ts"
159145Component.Explorer({
160146 filterFn: (node) => {
161147 // set containing names of everything you want to filter out
162148 const omit = new Set(["authoring content", "tags", "hosting"])
163163- return !omit.has(node.name.toLowerCase())
149149+ return !omit.has(node.data.title.toLowerCase())
164150 },
165151})
166152```
167153168168-You can customize this by changing the entries of the `omit` set. Simply add all folder or file names you want to remove.
169169-170154### Remove files by tag
171155172172-You can access the frontmatter of a file by `node.file?.frontmatter?`. This allows you to filter out files based on their frontmatter, for example by their tags.
156156+You can access the tags of a file by `node.data.tags`.
173157174158```ts title="quartz.layout.ts"
175159Component.Explorer({
176160 filterFn: (node) => {
177161 // exclude files with the tag "explorerexclude"
178178- return node.file?.frontmatter?.tags?.includes("explorerexclude") !== true
162162+ return node.data.tags.includes("explorerexclude") !== true
179163 },
180164})
181165```
182166183167### Show every element in explorer
184168185185-To override the default filter function that removes the `tags` folder from the explorer, you can set the filter function to `undefined`.
169169+By default, the explorer will filter out the `tags` folder.
170170+To override the default filter function, you can set the filter function to `undefined`.
186171187172```ts title="quartz.layout.ts"
188173Component.Explorer({
···194179195180> [!tip]
196181> When writing more complicated functions, the `layout` file can start to look very cramped.
197197-> You can fix this by defining your functions in another file.
182182+> You can fix this by defining your sort functions outside of the component
183183+> and passing it in.
198184>
199199-> ```ts title="functions.ts"
185185+> ```ts title="quartz.layout.ts"
200186> import { Options } from "./quartz/components/ExplorerNode"
187187+>
201188> export const mapFn: Options["mapFn"] = (node) => {
202189> // implement your function here
203190> }
···207194> export const sortFn: Options["sortFn"] = (a, b) => {
208195> // implement your function here
209196> }
210210-> ```
211211->
212212-> You can then import them like this:
213197>
214214-> ```ts title="quartz.layout.ts"
215215-> import { mapFn, filterFn, sortFn } from "./functions.ts"
216198> Component.Explorer({
217217-> mapFn: mapFn,
218218-> filterFn: filterFn,
219219-> sortFn: sortFn,
199199+> // ... your other options
200200+> mapFn,
201201+> filterFn,
202202+> sortFn,
220203> })
221204> ```
222205···227210```ts title="quartz.layout.ts"
228211Component.Explorer({
229212 mapFn: (node) => {
230230- // dont change name of root node
231231- if (node.depth > 0) {
232232- // set emoji for file/folder
233233- if (node.file) {
234234- node.displayName = "๐ " + node.displayName
235235- } else {
236236- node.displayName = "๐ " + node.displayName
237237- }
238238- }
239239- },
240240-})
241241-```
242242-243243-### Putting it all together
244244-245245-In this example, we're going to customize the explorer by using functions from examples above to [[#Add emoji prefix | add emoji prefixes]], [[#remove-list-of-elements-filter| filter out some folders]] and [[#use-sort-to-put-files-first | sort with files above folders]].
246246-247247-```ts title="quartz.layout.ts"
248248-Component.Explorer({
249249- filterFn: sampleFilterFn,
250250- mapFn: sampleMapFn,
251251- sortFn: sampleSortFn,
252252- order: ["filter", "sort", "map"],
253253-})
254254-```
255255-256256-Notice how we customized the `order` array here. This is done because the default order applies the `sort` function last. While this normally works well, it would cause unintended behavior here, since we changed the first characters of all display names. In our example, `sort` would be applied based off the emoji prefix instead of the first _real_ character.
257257-258258-To fix this, we just changed around the order and apply the `sort` function before changing the display names in the `map` function.
259259-260260-### Use `sort` with pre-defined sort order
261261-262262-Here's another example where a map containing file/folder names (as slugs) is used to define the sort order of the explorer in quartz. All files/folders that aren't listed inside of `nameOrderMap` will appear at the top of that folders hierarchy level.
263263-264264-It's also worth mentioning, that the smaller the number set in `nameOrderMap`, the higher up the entry will be in the explorer. Incrementing every folder/file by 100, makes ordering files in their folders a lot easier. Lastly, this example still allows you to use a `mapFn` or frontmatter titles to change display names, as it uses slugs for `nameOrderMap` (which is unaffected by display name changes).
265265-266266-```ts title="quartz.layout.ts"
267267-Component.Explorer({
268268- sortFn: (a, b) => {
269269- const nameOrderMap: Record<string, number> = {
270270- "poetry-folder": 100,
271271- "essay-folder": 200,
272272- "research-paper-file": 201,
273273- "dinosaur-fossils-file": 300,
274274- "other-folder": 400,
275275- }
276276-277277- let orderA = 0
278278- let orderB = 0
279279-280280- if (a.file && a.file.slug) {
281281- orderA = nameOrderMap[a.file.slug] || 0
282282- } else if (a.name) {
283283- orderA = nameOrderMap[a.name] || 0
284284- }
285285-286286- if (b.file && b.file.slug) {
287287- orderB = nameOrderMap[b.file.slug] || 0
288288- } else if (b.name) {
289289- orderB = nameOrderMap[b.name] || 0
213213+ if (node.isFolder) {
214214+ node.displayName = "๐ " + node.displayName
215215+ } else {
216216+ node.displayName = "๐ " + node.displayName
290217 }
291291-292292- return orderA - orderB
293218 },
294219})
295220```
296296-297297-For reference, this is how the quartz explorer window would look like with that example:
298298-299299-```
300300-๐ Poetry Folder
301301-๐ Essay Folder
302302- โ๏ธ Research Paper File
303303-๐ฆด Dinosaur Fossils File
304304-๐ฎ Other Folder
305305-```
306306-307307-And this is how the file structure would look like:
308308-309309-```
310310-index.md
311311-poetry-folder
312312- index.md
313313-essay-folder
314314- index.md
315315- research-paper-file.md
316316-dinosaur-fossils-file.md
317317-other-folder
318318- index.md
319319-```