diff --git a/.changeset/heading-alias-transclusion.md b/.changeset/heading-alias-transclusion.md new file mode 100644 index 000000000..9069bd0b4 --- /dev/null +++ b/.changeset/heading-alias-transclusion.md @@ -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.0.1 or later, which marks those anchors on the heading. diff --git a/quartz/components/renderPage.test.ts b/quartz/components/renderPage.test.ts index bce6eeaad..429ba7877 100644 --- a/quartz/components/renderPage.test.ts +++ b/quartz/components/renderPage.test.ts @@ -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(["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") diff --git a/quartz/components/renderPage.tsx b/quartz/components/renderPage.tsx index 87638407c..ccb8ebc9e 100644 --- a/quartz/components/renderPage.tsx +++ b/quartz/components/renderPage.tsx @@ -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 }