fix: heading transclusions look through Obsidian's per-block wrappers

This commit is contained in:
saberzero1 committed 2026-11-25 19:07:18 +01:00
1 parent 2386362d57
commit 7714325740
5 files changed
+100 -4

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
Heading transclusions and their footnotes look through Obsidian's per-block wrappers (`div.el-h2 > h2`), which `@quartz-community/rehype-obsidian` now renders around each of a note's top-level blocks. `blockOf()` (in `components/transcludeScope.ts`) returns the block inside a wrapper, for other code that walks a page's top-level blocks.
+37
View File
@@ -184,6 +184,43 @@ describe("renderTranscludes", () => {
assert.ok(!secondText.includes("Circular transclusion"), "should not show circular warning")
})
test("a header transclusion finds headings inside Obsidian's block wrappers", () => {
const root: Root = {
type: "root",
children: [makeTranscludeBlockquote("target", "#details")],
}
const wrap = (tagName: string, properties: Element["properties"], value: string): Element => ({
type: "element",
tagName: "div",
properties: { className: [`el-${tagName}`] },
children: [{ type: "element", tagName, properties, children: [{ type: "text", value }] }],
})
const targetHtml: Root = {
type: "root",
children: [
wrap("h2", { id: "intro" }, "Intro"),
wrap("p", {}, "Intro text"),
wrap("h2", { id: "details" }, "Details"),
wrap("p", {}, "Details text"),
wrap("h2", { id: "after" }, "After"),
wrap("p", {}, "After text"),
],
}
renderTranscludes(
root,
cfg,
"current" as FullSlug,
makeComponentData([makePageData("target", targetHtml)]) as QuartzComponentProps,
new Set<FullSlug>(["current" as FullSlug]),
)
const html = toHtml(root.children[0] as Element)
assert.ok(html.includes('<div class="el-p"><p>Details text</p></div>'), html)
assert.ok(!html.includes("Intro text"), "the slice starts at its heading")
assert.ok(!html.includes("After text"), "the slice ends at the next heading")
})
test("a header transclusion matches a heading's Obsidian-anchor alias", () => {
const root: Root = {
type: "root",
+4 -2
View File
@@ -15,7 +15,7 @@ import { i18n } from "../i18n"
import { styleText } from "util"
import { resolveFrame } from "./frames"
import type { PageTreeTransforms, TreeTransform } from "../plugins/types"
import { createIdScopes, scopeTranscludedIds, sliceWithFootnotes } from "./transcludeScope"
import { blockOf, createIdScopes, scopeTranscludedIds, sliceWithFootnotes } from "./transcludeScope"
import type { BuildCtx } from "../util/ctx"
interface RenderComponents {
@@ -248,7 +248,9 @@ export function renderTranscludes(
let startIdx = undefined
let startDepth = undefined
let endIdx = undefined
for (const [i, htmlEl] of page.htmlAst.children.entries()) {
for (const [i, child] of page.htmlAst.children.entries()) {
// The heading, inside its block wrapper if it has one (`div.el-h2 > h2`).
const htmlEl = blockOf(child as ElementContent)
if (!(htmlEl.type === "element" && htmlEl.tagName.match(headerRegex))) continue
const depth = Number(htmlEl.tagName.substring(1))
+38 -1
View File
@@ -1,7 +1,7 @@
import test, { describe } from "node:test"
import assert from "node:assert"
import type { Element, ElementContent } from "hast"
import { createIdScopes, scopeTranscludedIds, sliceWithFootnotes } from "./transcludeScope"
import { blockOf, createIdScopes, scopeTranscludedIds, sliceWithFootnotes } from "./transcludeScope"
const h = (
tagName: string,
@@ -113,3 +113,40 @@ describe("sliceWithFootnotes", () => {
assert.deepStrictEqual(footnoteIds(sliceWithFootnotes(plain, 0, undefined)), [])
})
})
describe("block wrappers", () => {
const wrap = (block: Element) => h("div", { className: [`el-${block.tagName}`] }, [block])
const page: ElementContent[] = [
wrap(h("h2", { id: "a" }, [text("A")])),
text("\n"),
wrap(h("p", {}, [h("a", { href: "#user-content-fn-1", dataFootnoteRef: "" }, [text("1")])])),
text("\n"),
wrap(
h("section", { dataFootnotes: "", className: ["footnotes"] }, [
h("ol", {}, [h("li", { id: "user-content-fn-1" }), h("li", { id: "user-content-fn-2" })]),
]),
),
]
test("blockOf looks through Obsidian's per-block wrapper", () => {
assert.strictEqual((blockOf(page[0]!) as Element).tagName, "h2")
const plain = h("p")
assert.strictEqual(blockOf(plain), plain)
const callout = h("div", { className: ["callout"] }, [h("p")])
assert.strictEqual(blockOf(callout), callout)
})
test("a heading slice gets its footnotes in the section's wrapper", () => {
const slice = sliceWithFootnotes(page, 0, undefined)
const wrappers = slice.filter((n): n is Element => n.type === "element")
assert.deepStrictEqual(
wrappers.map((w) => w.properties.className),
[["el-h2"], ["el-p"], ["el-section"]],
)
const section = wrappers[2]!.children[0] as Element
const ids = ((section.children[0] as Element).children as Element[]).map(
(li) => li.properties.id,
)
assert.deepStrictEqual(ids, ["user-content-fn-1"])
})
})
+16 -1
View File
@@ -91,7 +91,21 @@ export function scopeTranscludedIds(el: Element, prefix: string, sourceHref?: st
})
}
/**
* The block inside Obsidian's per-block wrapper (`div.el-p > p`, `div.el-h2 > h2`), which
* `@quartz-community/rehype-obsidian` renders around each of a note's top-level blocks; any other
* node is its own block. Code walking a page's top-level blocks looks through the wrappers.
*/
export function blockOf(node: ElementContent): ElementContent {
if (node.type !== "element" || node.tagName !== "div") return node
const classes = ([] as unknown[]).concat(node.properties?.className ?? [])
if (!classes.some((c) => /^el-[a-z0-9]+$/.test(String(c)))) return node
const inner = node.children.filter((c) => c.type === "element")
return inner.length === 1 ? inner[0]! : node
}
function isFootnotesSection(node: ElementContent): node is Element {
node = blockOf(node)
if (node.type !== "element" || node.tagName !== "section") return false
const props = node.properties ?? {}
const classes = (props.className ?? []) as string[]
@@ -101,7 +115,8 @@ function isFootnotesSection(node: ElementContent): node is Element {
/**
* A heading transclusion: the slice of the page from the heading to the next one of the same or
* a higher level. The page's footnotes section is not part of the section; the footnotes the
* slice references are added after it (in a copy of the section holding only those).
* slice references are added after it (in a copy of the section holding only those, in its block
* wrapper if it has one).
*/
export function sliceWithFootnotes(
pageChildren: ElementContent[],