mirror of
https://github.com/ZetaKebab/quartz.git
synced 2025-06-22 00:34:37 +00:00
fix(folder): use memoized trie instead of handrolled path solution (closes #1767)
This commit is contained in:
@ -229,6 +229,58 @@ describe("FileTrie", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("findNode", () => {
|
||||
test("should find root node with empty path", () => {
|
||||
const data = { title: "Root", slug: "index", filePath: "index.md" }
|
||||
trie.add(data)
|
||||
const found = trie.findNode([])
|
||||
assert.strictEqual(found, trie)
|
||||
})
|
||||
|
||||
test("should find node at first level", () => {
|
||||
const data = { title: "Test", slug: "test", filePath: "test.md" }
|
||||
trie.add(data)
|
||||
const found = trie.findNode(["test"])
|
||||
assert.strictEqual(found?.data, data)
|
||||
})
|
||||
|
||||
test("should find nested node", () => {
|
||||
const data = {
|
||||
title: "Nested",
|
||||
slug: "folder/subfolder/test",
|
||||
filePath: "folder/subfolder/test.md",
|
||||
}
|
||||
trie.add(data)
|
||||
const found = trie.findNode(["folder", "subfolder", "test"])
|
||||
assert.strictEqual(found?.data, data)
|
||||
|
||||
// should find the folder and subfolder indexes too
|
||||
assert.strictEqual(
|
||||
trie.findNode(["folder", "subfolder", "index"]),
|
||||
trie.children[0].children[0],
|
||||
)
|
||||
assert.strictEqual(trie.findNode(["folder", "index"]), trie.children[0])
|
||||
})
|
||||
|
||||
test("should return undefined for non-existent path", () => {
|
||||
const data = { title: "Test", slug: "test", filePath: "test.md" }
|
||||
trie.add(data)
|
||||
const found = trie.findNode(["nonexistent"])
|
||||
assert.strictEqual(found, undefined)
|
||||
})
|
||||
|
||||
test("should return undefined for partial path", () => {
|
||||
const data = {
|
||||
title: "Nested",
|
||||
slug: "folder/subfolder/test",
|
||||
filePath: "folder/subfolder/test.md",
|
||||
}
|
||||
trie.add(data)
|
||||
const found = trie.findNode(["folder"])
|
||||
assert.strictEqual(found?.data, null)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getFolderPaths", () => {
|
||||
test("should return all folder paths", () => {
|
||||
const data1 = {
|
||||
|
@ -89,6 +89,14 @@ export class FileTrieNode<T extends FileTrieData = ContentDetails> {
|
||||
this.insert(file.slug.split("/"), file)
|
||||
}
|
||||
|
||||
findNode(path: string[]): FileTrieNode<T> | undefined {
|
||||
if (path.length === 0 || (path.length === 1 && path[0] === "index")) {
|
||||
return this
|
||||
}
|
||||
|
||||
return this.children.find((c) => c.slugSegment === path[0])?.findNode(path.slice(1))
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter trie nodes. Behaves similar to `Array.prototype.filter()`, but modifies tree in place
|
||||
*/
|
||||
|
@ -247,7 +247,7 @@ export function transformLink(src: FullSlug, target: string, opts: TransformOpti
|
||||
}
|
||||
|
||||
// path helpers
|
||||
function isFolderPath(fplike: string): boolean {
|
||||
export function isFolderPath(fplike: string): boolean {
|
||||
return (
|
||||
fplike.endsWith("/") ||
|
||||
endsWith(fplike, "index") ||
|
||||
|
Reference in New Issue
Block a user