Compare commits

..
6 changed files with 148 additions and 16 deletions

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
The folder hierarchy used by folder pages and breadcrumbs now includes the pages of page types that render files (`.base`, `.canvas`, …): folder pages list them, and a folder that holds only such pages has its own node (#2576). Documented that while a page type's `generate` runs, `ctx.virtualPages` holds the virtual pages of the page types before it.
+13 -13
View File
@@ -37,7 +37,7 @@
"@quartz-community/graph": "^1.0.0",
"@quartz-community/hard-line-breaks": "^1.0.0",
"@quartz-community/latex": "^1.0.0",
"@quartz-community/note-properties": "^1.0.0",
"@quartz-community/note-properties": "^1.1.1",
"@quartz-community/obsidian-flavored-markdown": "^2.0.0",
"@quartz-community/og-image": "^1.0.0",
"@quartz-community/ox-hugo": "^1.0.0",
@@ -2892,9 +2892,9 @@
}
},
"node_modules/@quartz-community/note-properties": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@quartz-community/note-properties/-/note-properties-1.1.0.tgz",
"integrity": "sha512-TYpEDTO9Im3zZBqKpsQHFbSABj/FPswPSBBhle9FSiNUQpIOSZk6lj6Yw3LHz2mpa01w3Cvag5MGtugYe+HYEQ==",
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@quartz-community/note-properties/-/note-properties-1.1.1.tgz",
"integrity": "sha512-kURVT2PTQZTcRZxiT7o/gerI/8n0EhVogLaMznviJXYKSSvenE+8uKlPxJtZr1nbTJOn73UNBr5iNMhQIycQtg==",
"license": "MIT",
"dependencies": {
"@quartz-community/types": "^1.0.0",
@@ -2918,9 +2918,9 @@
}
},
"node_modules/@quartz-community/obsidian-flavored-markdown": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@quartz-community/obsidian-flavored-markdown/-/obsidian-flavored-markdown-2.0.0.tgz",
"integrity": "sha512-B8Q52yWmtvuNXpmRuNRHZSTgjAcdzT2N0eQiZhYfykUC6dQwMNs/mHIzxBK8T7A8xr3KjkGrtqzEM1s49dYjgw==",
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@quartz-community/obsidian-flavored-markdown/-/obsidian-flavored-markdown-2.1.0.tgz",
"integrity": "sha512-yOLBsYrJ5IVjxhl/ppZmXQAHianKkeIGMvDT7mwNOaalsah19p9luHrPYg0eEsn1o6IkHFP8h1LaY2WT+wIABw==",
"license": "MIT",
"dependencies": {
"@quartz-community/rehype-obsidian": "^1.2.0",
@@ -3296,9 +3296,9 @@
}
},
"node_modules/@quartz-community/types": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@quartz-community/types/-/types-1.1.0.tgz",
"integrity": "sha512-FedvwWskypTuEd/ZNqV7o/HjSE+F9rQGpBxy35Ia/hUitUA92tJsoxOtX+OAOjsB0kEq679e60zCXoyoIq8QRA==",
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@quartz-community/types/-/types-1.2.0.tgz",
"integrity": "sha512-82L+hallWn9LCMw9kFUWJhAGwlTd4rT2KxxL8B7IAWTEvj/20StWsuC01Mbfk5hLhd8jAHGgj1WHb0u/cdUXEA==",
"license": "MIT",
"dependencies": {
"unified": "^11.0.5",
@@ -7604,9 +7604,9 @@
"license": "ISC"
},
"node_modules/ws": {
"version": "8.22.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.22.1.tgz",
"integrity": "sha512-oyL6OPYme/CmiFvNuGzeoxCJ4nOrZE2O1FwM9MWM1AsGri6nLXB2pg+lNA8aLeXMSav0J6eIVS1j6vUPzUtK9g==",
"version": "8.22.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.22.2.tgz",
"integrity": "sha512-/rEZ2ejown2N5gxVTHUu9bVVC7ttu0/Tgzf5hbTOixR3Fs7mbbXgn2DbdbcKgJOmpQMtrIUWivtYuVMnp/W/yA==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
+1 -1
View File
@@ -67,7 +67,7 @@
"@quartz-community/graph": "^1.0.0",
"@quartz-community/hard-line-breaks": "^1.0.0",
"@quartz-community/latex": "^1.0.0",
"@quartz-community/note-properties": "^1.0.0",
"@quartz-community/note-properties": "^1.1.1",
"@quartz-community/obsidian-flavored-markdown": "^2.0.0",
"@quartz-community/og-image": "^1.0.0",
"@quartz-community/ox-hugo": "^1.0.0",
@@ -5,6 +5,7 @@ import os from "os"
import path from "path"
import {
collectComponents,
folderTrie,
generateVirtualPages,
pageOutputPath,
removeStalePageOutput,
@@ -14,6 +15,7 @@ import { QuartzPageTypePluginInstance } from "../types"
import { QuartzComponent } from "../../components/types"
import { FilePath, FullSlug } from "../../util/path"
import { BuildCtx } from "../../util/ctx"
import { defaultProcessedContent } from "../vfile"
const StubA: QuartzComponent = (() => null) as unknown as QuartzComponent
const StubB: QuartzComponent = (() => null) as unknown as QuartzComponent
@@ -277,3 +279,89 @@ describe("generateVirtualPages", () => {
)
})
})
describe("folders of virtual pages (#2576)", () => {
const makeCtx = () => ({ cfg: { configuration: {} }, virtualPages: [] }) as unknown as BuildCtx
const page = (slug: string) => ({ slug: slug as FullSlug, title: slug, data: {} })
const note = (slug: string) =>
defaultProcessedContent({
slug: slug as FullSlug,
relativePath: `${slug}.md` as FilePath,
filePath: `content/${slug}.md` as FilePath,
frontmatter: { title: slug },
})[1].data
test("a page type sees the virtual pages of higher-priority page types in ctx.virtualPages", async () => {
const ctx = makeCtx()
let seen: string[] = []
await generateVirtualPages(
[
makePageType({ name: "bases", generate: () => [page("trips/map.base")] }),
makePageType({
name: "folders",
generate: ({ ctx }) => {
seen = ctx.virtualPages.map(([, vfile]) => vfile.data.slug!)
return []
},
}),
],
[],
ctx,
{ head: StubHead },
{},
)
assert.deepStrictEqual(seen, ["trips/map.base"])
})
test("only pages of page types that render files are file-backed", async () => {
const entries = await generateVirtualPages(
[
makePageType({
name: "bases",
fileExtensions: [".base"],
generate: () => [page("trips/map.base")],
}),
makePageType({ name: "tags", generate: () => [page("tags/a")] }),
],
[],
makeCtx(),
{ head: StubHead },
{},
)
assert.deepStrictEqual(
entries.map((e) => [e.vpSlug, e.fileBacked]),
[
["trips/map.base", true],
["tags/a", false],
],
)
})
test("the folder trie holds file-backed virtual pages, not tag or folder pages", async () => {
const entries = await generateVirtualPages(
[
makePageType({
name: "bases",
fileExtensions: [".base"],
generate: () => [page("trips/map.base"), page("mixed/board.base")],
}),
makePageType({ name: "tags", generate: () => [page("tags/a")] }),
makePageType({ name: "folders", generate: () => [page("trips/index")] }),
],
[],
makeCtx(),
{ head: StubHead },
{},
)
const trie = folderTrie([note("mixed/note")], entries)
const children = (folder: string) =>
trie
.findNode([folder, "index"])
?.children.map((c) => c.slug)
.sort()
assert.deepStrictEqual(children("trips"), ["trips/map.base"])
assert.deepStrictEqual(children("mixed"), ["mixed/board.base", "mixed/note"])
assert.strictEqual(trie.findNode(["tags", "index"]), undefined)
assert.strictEqual(trie.findNode(["trips", "index"])?.data, null)
})
})
+35 -1
View File
@@ -143,6 +143,11 @@ export type VirtualEntry = {
vfile: ProcessedContent[1]
layout: FullPageLayout
vpSlug: FullSlug
/**
* Generated by a page type that renders source files (it declares `fileExtensions`, e.g. a
* `.base` or `.canvas` file): the page stands for a file and belongs in the folder hierarchy.
*/
fileBacked: boolean
}
/**
@@ -164,6 +169,7 @@ export async function generateVirtualPages(
if (!pt.generate) continue
const virtualPages = await pt.generate({ content, cfg, ctx })
const layout = resolveLayout(pt, defaults, byPageType)
const fileBacked = (pt.fileExtensions?.length ?? 0) > 0
for (const vp of virtualPages) {
const vpSlug = vp.slug as FullSlug
const vpRelativePath = (vpSlug + ".md") as FilePath
@@ -176,12 +182,34 @@ export async function generateVirtualPages(
if (vpSlug !== "404") {
ctx.virtualPages.push([tree, vfile])
}
virtualEntries.push({ tree, vfile, layout, vpSlug })
virtualEntries.push({ tree, vfile, layout, vpSlug, fileBacked })
}
}
return virtualEntries
}
/**
* @internal Exported for testing only.
*
* The folder hierarchy (`ctx.trie`, used by folder pages and breadcrumbs): the source files plus
* the virtual pages that stand for files (`.base`, `.canvas`, …), so a folder holding only such
* files still has a node with them as children (jackyzha0/quartz#2576). Tag and folder pages
* are left out — they are listings, not files.
*/
export function folderTrie(
allFiles: ProcessedContent[1]["data"][],
virtualEntries: VirtualEntry[],
) {
const fileBacked = virtualEntries
.filter((ve) => ve.fileBacked)
// the trie takes folder display names from file paths; a virtual page has only relativePath
.map((ve) => ({
...ve.vfile.data,
filePath: ve.vfile.data.filePath ?? ve.vfile.data.relativePath,
}))
return trieFromAllFiles([...allFiles, ...fileBacked])
}
/**
* Render each virtual page's Body component to HTML and parse it to a hast tree,
* populating both the ProcessedContent tree and vfile.data.htmlAst so that
@@ -250,6 +278,10 @@ export const PageTypeDispatcher: QuartzEmitterPlugin<Partial<DispatcherOptions>>
byPageType,
)
if (virtualEntries.some((ve) => ve.fileBacked)) {
ctx.trie = folderTrie(allFiles, virtualEntries)
}
// Merge virtual page data into allFiles before populating htmlAst so that
// Body components rendered during populateVirtualPageHtmlAst can resolve
// cross-virtual-page embeds (e.g. a .base file embedded in a .canvas file).
@@ -334,6 +366,8 @@ export const PageTypeDispatcher: QuartzEmitterPlugin<Partial<DispatcherOptions>>
byPageType,
)
ctx.trie = folderTrie(allFiles, virtualEntries)
const allFilesWithVirtual = [...allFiles, ...virtualEntries.map((ve) => ve.vfile.data)]
// Render Body components to populate htmlAst for transclusion
+6 -1
View File
@@ -42,7 +42,12 @@ export interface BuildCtx {
permalinks?: Record<string, FullSlug>
trie?: FileTrieNode<BuildTimeTrieData>
incremental: boolean
/** Virtual pages generated by page type plugins (e.g. tag pages, folder pages, bases pages) */
/**
* Virtual pages generated by page type plugins (e.g. tag pages, folder pages, bases pages).
* Page types generate in priority order, so while a page type's `generate` runs this holds the
* virtual pages of the page types before it (folder pages use it to find folders that hold only
* `.base` / `.canvas` pages).
*/
virtualPages: ProcessedContent[]
/** Content-hashed asset filenames, populated by ComponentResources emitter */
hashedResourceNames?: HashedResourceNames