mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-30 22:44:41 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60c8318b34 | ||
|
|
2ef60c6be5 |
No files matched your search
@@ -1,5 +0,0 @@
|
||||
---
|
||||
"@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.
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": patch
|
||||
---
|
||||
|
||||
Embedding a section by the anchor Obsidian writes for its heading works when that anchor differs from the heading's id: `![[note#Colon unspaced]]` now embeds `## Colon:unspaced`. It needs GitHubFlavoredMarkdown 2.1.0 or later, which marks those anchors on the heading.
|
||||
@@ -183,6 +183,46 @@ describe("renderTranscludes", () => {
|
||||
assert.ok(!secondText.includes("Circular transclusion"), "should not show circular warning")
|
||||
})
|
||||
|
||||
test("a header transclusion matches a heading's Obsidian-anchor alias", () => {
|
||||
const root: Root = {
|
||||
type: "root",
|
||||
children: [makeTranscludeBlockquote("target", "#colon-unspaced")],
|
||||
}
|
||||
const targetHtml: Root = {
|
||||
type: "root",
|
||||
children: [
|
||||
{
|
||||
type: "element",
|
||||
tagName: "h2",
|
||||
properties: { id: "colonunspaced" },
|
||||
children: [
|
||||
{
|
||||
type: "element",
|
||||
tagName: "span",
|
||||
properties: { id: "colon-unspaced", className: ["heading-alias"] },
|
||||
children: [],
|
||||
},
|
||||
{ type: "text", value: "Colon:unspaced" },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "element",
|
||||
tagName: "p",
|
||||
properties: {},
|
||||
children: [{ type: "text", value: "Section text" }],
|
||||
},
|
||||
],
|
||||
}
|
||||
renderTranscludes(
|
||||
root,
|
||||
cfg,
|
||||
"current" as FullSlug,
|
||||
makeComponentData([makePageData("target", targetHtml)]) as QuartzComponentProps,
|
||||
new Set<FullSlug>(["current" as FullSlug]),
|
||||
)
|
||||
assert.ok(JSON.stringify((root.children[0] as Element).children).includes("Section text"))
|
||||
})
|
||||
|
||||
test("detects actual circular transclusion (A -> B -> A)", () => {
|
||||
// Page A embeds B, and B's htmlAst contains a transclusion of A
|
||||
const bTranscludesA = makeTranscludeBlockquote("pageA")
|
||||
|
||||
@@ -31,6 +31,25 @@ interface RenderComponents {
|
||||
}
|
||||
|
||||
const headerRegex = new RegExp(/h[1-6]/)
|
||||
|
||||
/**
|
||||
* Whether a heading answers to `id`: its own id, or the id of a `span.heading-alias` inside it.
|
||||
* GitHubFlavoredMarkdown adds that alias when the anchor Obsidian writes for a heading (`:`, `|`,
|
||||
* `^`, `#` and `\` become spaces) slugs differently, e.g. `![[note#Colon unspaced]]` for
|
||||
* `## Colon:unspaced`.
|
||||
*/
|
||||
function headingHasId(heading: Element, id: string): boolean {
|
||||
if (heading.properties?.id === id) return true
|
||||
const hasAlias = (el: Element): boolean =>
|
||||
el.children.some(
|
||||
(child) =>
|
||||
child.type === "element" &&
|
||||
((child.properties?.id === id &&
|
||||
([] as unknown[]).concat(child.properties?.className ?? []).includes("heading-alias")) ||
|
||||
hasAlias(child)),
|
||||
)
|
||||
return hasAlias(heading)
|
||||
}
|
||||
export function pageResources(
|
||||
baseDir: FullSlug | RelativeURL,
|
||||
staticResources: StaticResources,
|
||||
@@ -231,7 +250,7 @@ export function renderTranscludes(
|
||||
const depth = Number(htmlEl.tagName.substring(1))
|
||||
|
||||
if (startIdx === undefined || startDepth === undefined) {
|
||||
if (htmlEl.properties?.id === blockRef) {
|
||||
if (headingHasId(htmlEl, blockRef)) {
|
||||
startIdx = i
|
||||
startDepth = depth
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import os from "os"
|
||||
import path from "path"
|
||||
import {
|
||||
collectComponents,
|
||||
folderTrie,
|
||||
generateVirtualPages,
|
||||
pageOutputPath,
|
||||
removeStalePageOutput,
|
||||
@@ -15,7 +14,6 @@ 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
|
||||
@@ -279,89 +277,3 @@ 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)
|
||||
})
|
||||
})
|
||||
@@ -143,11 +143,6 @@ 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
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,7 +164,6 @@ 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
|
||||
@@ -182,34 +176,12 @@ export async function generateVirtualPages(
|
||||
if (vpSlug !== "404") {
|
||||
ctx.virtualPages.push([tree, vfile])
|
||||
}
|
||||
virtualEntries.push({ tree, vfile, layout, vpSlug, fileBacked })
|
||||
virtualEntries.push({ tree, vfile, layout, vpSlug })
|
||||
}
|
||||
}
|
||||
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
|
||||
@@ -278,10 +250,6 @@ 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).
|
||||
@@ -366,8 +334,6 @@ 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
|
||||
|
||||
+1
-6
@@ -42,12 +42,7 @@ 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).
|
||||
* 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).
|
||||
*/
|
||||
/** Virtual pages generated by page type plugins (e.g. tag pages, folder pages, bases pages) */
|
||||
virtualPages: ProcessedContent[]
|
||||
/** Content-hashed asset filenames, populated by ComponentResources emitter */
|
||||
hashedResourceNames?: HashedResourceNames
|
||||
|
||||
Reference in new issue
Block a user