feat(layout): is-index and is-folder-index conditions, not-<condition> negation (#2465)

This commit is contained in:
saberzero1 committed 2026-11-26 15:53:06 +01:00
1 parent 773e2c7fe1
commit 2209ee6d7d
6 files changed
+105 -15

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": minor
---
Layout conditions: add `is-index` (only on the home page) and `is-folder-index` (only on folder index pages), and let `not-<condition>` negate any condition, built-in or registered (`not-has-tags`, `not-has-backlinks`, …).
+11 -6
View File
@@ -170,12 +170,17 @@ plugins:
Available built-in conditions:
| Condition | Description |
| --------------- | ----------------------------------------------------- |
| `not-index` | Only render when the page is not the root `index.md` |
| `has-tags` | Only render when the page has tags in its frontmatter |
| `has-backlinks` | Only render when the page has backlinks |
| `has-toc` | Only render when the page has a table of contents |
| Condition | Description |
| ----------------- | --------------------------------------------------------------------------- |
| `is-index` | Only render on the root `index.md` (the home page) |
| `not-index` | Only render when the page is not the root `index.md` |
| `is-folder-index` | Only render on a folder's index page (`folder/index.md` or a generated one) |
| `has-tags` | Only render when the page has tags in its frontmatter |
| `has-backlinks` | Only render when the page has backlinks |
| `has-toc` | Only render when the page has a table of contents |
| `not-<condition>` | Negates any other condition, e.g. `not-has-tags`, `not-has-backlinks` |
The `not-` prefix also works for conditions registered by plugins or in `quartz.ts`. To change the layout of a whole page type (folder pages, tag pages, …) rather than single pages, use `layout.byPageType` (see [[layout#Per-page-type overrides]]).
### TS Override
+9 -6
View File
@@ -132,12 +132,15 @@ plugins:
Available conditions:
| Condition | Effect |
| --------------- | ------------------------------------------------------------------------ |
| `not-index` | Hidden on the root index page, shown everywhere else |
| `has-tags` | Only shown on pages that have tags in frontmatter |
| `has-backlinks` | Only shown on pages that another (listed) page links to |
| `has-toc` | Only shown on pages with a table of contents (needs `table-of-contents`) |
| Condition | Effect |
| ----------------- | ---------------------------------------------------------------------------- |
| `is-index` | Only shown on the root index page (the home page) |
| `not-index` | Hidden on the root index page, shown everywhere else |
| `is-folder-index` | Only shown on folder index pages (`folder/index.md` or a generated one) |
| `has-tags` | Only shown on pages that have tags in frontmatter |
| `has-backlinks` | Only shown on pages that another (listed) page links to |
| `has-toc` | Only shown on pages with a table of contents (needs `table-of-contents`) |
| `not-<condition>` | Negates any condition, built-in or registered: `not-has-tags`, `not-has-toc` |
Plugins can register additional conditions. An unknown condition name prints a warning at build time and the component is always shown.
+59 -1
View File
@@ -1,6 +1,6 @@
import test, { describe } from "node:test"
import assert from "node:assert"
import { getCondition, hasBacklinks } from "./conditions"
import { getCondition, hasBacklinks, registerCondition } from "./conditions"
import type { QuartzComponentProps } from "../../components/types"
type Props = Pick<QuartzComponentProps, "fileData" | "allFiles">
@@ -51,3 +51,61 @@ describe("has-backlinks condition", () => {
assert.strictEqual(hasBacklinks({ fileData: { slug: "a" } } as unknown as Props), false)
})
})
describe("index conditions (#2465)", () => {
const run = (name: string, slug: string | undefined) =>
getCondition(name)!({ fileData: { slug }, allFiles: [] } as unknown as QuartzComponentProps)
test("is-index is true only on the root index page", () => {
assert.strictEqual(run("is-index", "index"), true)
assert.strictEqual(run("is-index", "folder/index"), false)
assert.strictEqual(run("is-index", "page"), false)
assert.strictEqual(run("is-index", undefined), false)
})
test("is-index and not-index are complements", () => {
for (const slug of ["index", "folder/index", "a/b", undefined]) {
assert.strictEqual(run("is-index", slug), !run("not-index", slug))
}
})
test("is-folder-index matches folder index pages, not the root index", () => {
assert.strictEqual(run("is-folder-index", "folder/index"), true)
assert.strictEqual(run("is-folder-index", "a/b/index"), true)
assert.strictEqual(run("is-folder-index", "index"), false)
assert.strictEqual(run("is-folder-index", "folder/indexes"), false)
assert.strictEqual(run("is-folder-index", undefined), false)
})
})
describe("not-<condition> negation", () => {
const withTags = { fileData: { slug: "a", frontmatter: { tags: ["x"] } }, allFiles: [] }
const noTags = { fileData: { slug: "a", frontmatter: {} }, allFiles: [] }
const call = (name: string, p: unknown) => getCondition(name)!(p as QuartzComponentProps)
test("not-has-tags negates has-tags", () => {
assert.strictEqual(call("not-has-tags", withTags), false)
assert.strictEqual(call("not-has-tags", noTags), true)
})
test("negates custom conditions too", () => {
registerCondition("is-draft-test", (p) => p.fileData.slug === "draft")
assert.strictEqual(call("not-is-draft-test", { fileData: { slug: "draft" } }), false)
assert.strictEqual(call("not-is-draft-test", { fileData: { slug: "x" } }), true)
})
test("an exact name wins over negation (not-index is its own built-in)", () => {
assert.strictEqual(call("not-index", { fileData: { slug: "index" } }), false)
assert.strictEqual(call("not-index", { fileData: { slug: "a" } }), true)
})
test("unknown names stay unknown", () => {
assert.strictEqual(getCondition("not-nonexistent"), undefined)
assert.strictEqual(getCondition("not-"), undefined)
assert.strictEqual(getCondition("nonexistent"), undefined)
})
test("double negation works", () => {
assert.strictEqual(call("not-not-has-tags", withTags), true)
})
})
+20 -1
View File
@@ -4,7 +4,13 @@ import { FullSlug, simplifySlug } from "../../util/path"
export type ConditionPredicate = (props: QuartzComponentProps) => boolean
const builtinConditions: Record<string, ConditionPredicate> = {
"is-index": (props) => props.fileData.slug === "index",
"not-index": (props) => props.fileData.slug !== "index",
// A folder's index page: `folder/index.md` or a generated folder page, not the root index.
"is-folder-index": (props) => {
const slug = props.fileData.slug
return typeof slug === "string" && slug.endsWith("/index")
},
"has-tags": (props) => {
const tags = props.fileData.frontmatter?.tags
return Array.isArray(tags) && tags.length > 0
@@ -36,8 +42,21 @@ export function registerCondition(name: string, predicate: ConditionPredicate):
customConditions.set(name, predicate)
}
const NEGATION_PREFIX = "not-"
/**
* Look up a condition. Besides the registered names, `not-<name>` negates any other
* condition (`not-has-tags`, `not-is-folder-index`, a custom `not-<custom>`), unless a
* condition with that exact name exists (`not-index` is its own built-in).
*/
export function getCondition(name: string): ConditionPredicate | undefined {
return customConditions.get(name) ?? builtinConditions[name]
const direct = customConditions.get(name) ?? builtinConditions[name]
if (direct) return direct
if (name.startsWith(NEGATION_PREFIX)) {
const inner = getCondition(name.slice(NEGATION_PREFIX.length))
if (inner) return (props) => !inner(props)
}
return undefined
}
export function getAllConditionNames(): string[] {
+1 -1
View File
@@ -432,7 +432,7 @@
},
"condition": {
"type": "string",
"description": "Render only when this condition holds. Built-in: not-index, has-tags, has-backlinks, has-toc; plugins may register more."
"description": "Render only when this condition holds. Built-in: is-index, not-index, is-folder-index, has-tags, has-backlinks, has-toc; plugins may register more. Prefix any condition with not- to negate it (not-has-tags)."
},
"group": {
"type": "string",