mirror of
https://github.com/ZetaKebab/quartz.git
synced 2025-06-22 00:34:37 +00:00
fix: cleanup a href link construction, global shared trie, breadcrumbs use trie
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
import { QuartzConfig } from "../cfg"
|
||||
import { QuartzPluginData } from "../plugins/vfile"
|
||||
import { FileTrieNode } from "./fileTrie"
|
||||
import { FilePath, FullSlug } from "./path"
|
||||
|
||||
export interface Argv {
|
||||
@ -13,13 +15,36 @@ export interface Argv {
|
||||
concurrency?: number
|
||||
}
|
||||
|
||||
export type BuildTimeTrieData = QuartzPluginData & {
|
||||
slug: string
|
||||
title: string
|
||||
filePath: string
|
||||
}
|
||||
|
||||
export interface BuildCtx {
|
||||
buildId: string
|
||||
argv: Argv
|
||||
cfg: QuartzConfig
|
||||
allSlugs: FullSlug[]
|
||||
allFiles: FilePath[]
|
||||
trie?: FileTrieNode<BuildTimeTrieData>
|
||||
incremental: boolean
|
||||
}
|
||||
|
||||
export type WorkerSerializableBuildCtx = Omit<BuildCtx, "cfg">
|
||||
export function trieFromAllFiles(allFiles: QuartzPluginData[]): FileTrieNode<BuildTimeTrieData> {
|
||||
const trie = new FileTrieNode<BuildTimeTrieData>([])
|
||||
allFiles.forEach((file) => {
|
||||
if (file.frontmatter) {
|
||||
trie.add({
|
||||
...file,
|
||||
slug: file.slug!,
|
||||
title: file.frontmatter.title,
|
||||
filePath: file.filePath!,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return trie
|
||||
}
|
||||
|
||||
export type WorkerSerializableBuildCtx = Omit<BuildCtx, "cfg" | "trie">
|
||||
|
@ -330,4 +330,86 @@ describe("FileTrie", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("pathToNode", () => {
|
||||
test("should return root node for empty path", () => {
|
||||
const data = { title: "Root", slug: "index", filePath: "index.md" }
|
||||
trie.add(data)
|
||||
const path = trie.ancestryChain([])
|
||||
assert.deepStrictEqual(path, [trie])
|
||||
})
|
||||
|
||||
test("should return root node for index path", () => {
|
||||
const data = { title: "Root", slug: "index", filePath: "index.md" }
|
||||
trie.add(data)
|
||||
const path = trie.ancestryChain(["index"])
|
||||
assert.deepStrictEqual(path, [trie])
|
||||
})
|
||||
|
||||
test("should return path to first level node", () => {
|
||||
const data = { title: "Test", slug: "test", filePath: "test.md" }
|
||||
trie.add(data)
|
||||
const path = trie.ancestryChain(["test"])
|
||||
assert.deepStrictEqual(path, [trie, trie.children[0]])
|
||||
})
|
||||
|
||||
test("should return path to nested node", () => {
|
||||
const data = {
|
||||
title: "Nested",
|
||||
slug: "folder/subfolder/test",
|
||||
filePath: "folder/subfolder/test.md",
|
||||
}
|
||||
trie.add(data)
|
||||
const path = trie.ancestryChain(["folder", "subfolder", "test"])
|
||||
assert.deepStrictEqual(path, [
|
||||
trie,
|
||||
trie.children[0],
|
||||
trie.children[0].children[0],
|
||||
trie.children[0].children[0].children[0],
|
||||
])
|
||||
})
|
||||
|
||||
test("should return undefined for non-existent path", () => {
|
||||
const data = { title: "Test", slug: "test", filePath: "test.md" }
|
||||
trie.add(data)
|
||||
const path = trie.ancestryChain(["nonexistent"])
|
||||
assert.strictEqual(path, undefined)
|
||||
})
|
||||
|
||||
test("should return file data for intermediate folders", () => {
|
||||
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: "folder/index",
|
||||
filePath: "folder/index.md",
|
||||
}
|
||||
|
||||
trie.add(data1)
|
||||
trie.add(data2)
|
||||
trie.add(data3)
|
||||
const path = trie.ancestryChain(["folder", "subfolder"])
|
||||
assert.deepStrictEqual(path, [trie, trie.children[0], trie.children[0].children[0]])
|
||||
assert.strictEqual(path[1].data, data3)
|
||||
})
|
||||
|
||||
test("should return path for partial path", () => {
|
||||
const data = {
|
||||
title: "Nested",
|
||||
slug: "folder/subfolder/test",
|
||||
filePath: "folder/subfolder/test.md",
|
||||
}
|
||||
trie.add(data)
|
||||
const path = trie.ancestryChain(["folder"])
|
||||
assert.deepStrictEqual(path, [trie, trie.children[0]])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -97,6 +97,24 @@ export class FileTrieNode<T extends FileTrieData = ContentDetails> {
|
||||
return this.children.find((c) => c.slugSegment === path[0])?.findNode(path.slice(1))
|
||||
}
|
||||
|
||||
ancestryChain(path: string[]): Array<FileTrieNode<T>> | undefined {
|
||||
if (path.length === 0 || (path.length === 1 && path[0] === "index")) {
|
||||
return [this]
|
||||
}
|
||||
|
||||
const child = this.children.find((c) => c.slugSegment === path[0])
|
||||
if (!child) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const childPath = child.ancestryChain(path.slice(1))
|
||||
if (!childPath) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return [this, ...childPath]
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter trie nodes. Behaves similar to `Array.prototype.filter()`, but modifies tree in place
|
||||
*/
|
||||
|
Reference in New Issue
Block a user