Merge pull request #2671 from jackyzha0/dev-mode/obsidian-embed-markup

feat: Obsidian's embed markup for transclusions
This commit is contained in:
Emile Bangma authored and GitHub committed 2026-11-26 01:05:18 +01:00
commit ed5912a53b
4 files changed
+159 -61

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": minor
---
Transclusions use Obsidian's embed markup: a `div.markdown-embed-title` (the note's title, for a whole-note embed; it was an `h1`), then the content in `div.markdown-embed-content > div.markdown-preview-view.markdown-rendered`, then the link to the original. Obsidian themes' embed styles apply (embed titles, padding, maximum height). The title looks as before without a theme. Obsidian's icon link to the note (`a.markdown-embed-link`, Lucide's maximize-2) follows; Quartz hides it, and Obsidian themes, through @quartz-themes/core, show it in the embed's corner.
+36
View File
@@ -49,6 +49,42 @@ function makeComponentData(
}
describe("renderTranscludes", () => {
test("wraps transcluded content in Obsidian's embed markup", () => {
const root: Root = { type: "root", children: [makeTranscludeBlockquote("target")] }
const targetHtml: Root = {
type: "root",
children: [
{
type: "element",
tagName: "p",
properties: {},
children: [{ type: "text", value: "Target content" }],
},
],
}
renderTranscludes(
root,
cfg,
"current" as FullSlug,
makeComponentData([makePageData("target", targetHtml)]) as QuartzComponentProps,
new Set<FullSlug>(["current" as FullSlug]),
)
const [title, content, link, embedLink] = (root.children[0] as Element).children as Element[]
assert.deepStrictEqual(title.properties.className, ["embed-title", "markdown-embed-title"])
assert.deepStrictEqual(content.properties.className, ["markdown-embed-content"])
const view = content.children[0] as Element
assert.deepStrictEqual(view.properties.className, [
"markdown-preview-view",
"markdown-rendered",
])
assert.strictEqual((view.children[0] as Element).tagName, "p")
assert.deepStrictEqual(link.properties.class, ["internal", "internal-link", "transclude-src"])
// Obsidian's icon link to the note, after Quartz's (hidden unless a theme shows it).
assert.deepStrictEqual(embedLink.properties.className, ["markdown-embed-link"])
assert.strictEqual(embedLink.properties.href, link.properties.href)
assert.strictEqual((embedLink.children[0] as Element).tagName, "svg")
})
test("resolves a single page transclusion", () => {
const root: Root = {
type: "root",
+98 -61
View File
@@ -139,6 +139,73 @@ export function pageResources(
return resources
}
/** Lucide's maximize-2, Obsidian's icon for opening an embedded note. */
const MAXIMIZE_ICON: Element = {
type: "element",
tagName: "svg",
properties: {
xmlns: "http://www.w3.org/2000/svg",
width: "24",
height: "24",
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: "2",
strokeLinecap: "round",
strokeLinejoin: "round",
className: ["svg-icon", "lucide-maximize-2"],
},
children: ["M15 3h6v6", "m21 3-7 7", "m3 21 7-7", "M9 21H3v-6"].map((d) => ({
type: "element",
tagName: "path",
properties: { d },
children: [],
})),
}
/**
* Obsidian's embed markup around transcluded content, so Obsidian themes' embed
* styles apply: the title (the note's, for a whole-note embed), then the content in a
* rendered view, then Quartz's link to the original, then Obsidian's icon link to it
* (`.markdown-embed-link`), which Quartz hides and Obsidian's styles show.
*/
function obsidianEmbed(title: string, content: ElementContent[], link: Element): ElementContent[] {
const embedLink: Element = {
type: "element",
tagName: "a",
properties: {
href: link.properties.href,
className: ["markdown-embed-link"],
ariaLabel: (link.children[0] as { value?: string } | undefined)?.value,
dataNoPopover: true,
},
children: [structuredClone(MAXIMIZE_ICON)],
}
return [
{
type: "element",
tagName: "div",
properties: { className: ["embed-title", "markdown-embed-title"] },
children: title ? [{ type: "text", value: title }] : [],
},
{
type: "element",
tagName: "div",
properties: { className: ["markdown-embed-content"] },
children: [
{
type: "element",
tagName: "div",
properties: { className: ["markdown-preview-view", "markdown-rendered"] },
children: content,
},
],
},
link,
embedLink,
]
}
/** @internal Exported for testing only. */
export function renderTranscludes(
root: Root,
@@ -172,6 +239,15 @@ export function renderTranscludes(
}
const inner = el.children[0] as Element
const linkToOriginal = (): Element => ({
type: "element",
tagName: "a",
properties: {
href: inner.properties?.href,
class: ["internal", "internal-link", "transclude-src"],
},
children: [{ type: "text", value: i18n(cfg.locale).components.transcludes.linkToOriginal }],
})
const transcludeTarget = (inner.properties["data-slug"] ?? slug) as FullSlug
if (visited.has(transcludeTarget)) {
console.warn(
@@ -227,20 +303,11 @@ export function renderTranscludes(
}
}
el.children = [
normalizeHastElement(blockNode, slug, transcludeTarget),
{
type: "element",
tagName: "a",
properties: {
href: inner.properties?.href,
class: ["internal", "internal-link", "transclude-src"],
},
children: [
{ type: "text", value: i18n(cfg.locale).components.transcludes.linkToOriginal },
],
},
]
el.children = obsidianEmbed(
"",
[normalizeHastElement(blockNode, slug, transcludeTarget)],
linkToOriginal(),
)
}
} else if (blockRef?.startsWith("#") && page.htmlAst) {
// header transclude
@@ -268,55 +335,25 @@ export function renderTranscludes(
continue
}
el.children = [
...sliceWithFootnotes(page.htmlAst.children as ElementContent[], startIdx, endIdx).map(
(c) => normalizeHastElement(c as Element, slug, transcludeTarget),
),
{
type: "element",
tagName: "a",
properties: {
href: inner.properties?.href,
class: ["internal", "internal-link", "transclude-src"],
},
children: [
{ type: "text", value: i18n(cfg.locale).components.transcludes.linkToOriginal },
],
},
]
} else if (page.htmlAst) {
// page transclude
el.children = [
{
type: "element",
tagName: "h1",
properties: {},
children: [
{
type: "text",
value:
page.frontmatter?.title ??
i18n(cfg.locale).components.transcludes.transcludeOf({
targetSlug: page.slug!,
}),
},
],
},
...(page.htmlAst.children as ElementContent[]).map((c) =>
el.children = obsidianEmbed(
"",
sliceWithFootnotes(page.htmlAst.children as ElementContent[], startIdx, endIdx).map((c) =>
normalizeHastElement(c as Element, slug, transcludeTarget),
),
{
type: "element",
tagName: "a",
properties: {
href: inner.properties?.href,
class: ["internal", "internal-link", "transclude-src"],
},
children: [
{ type: "text", value: i18n(cfg.locale).components.transcludes.linkToOriginal },
],
},
]
linkToOriginal(),
)
} else if (page.htmlAst) {
// page transclude
el.children = obsidianEmbed(
page.frontmatter?.title ??
i18n(cfg.locale).components.transcludes.transcludeOf({
targetSlug: page.slug!,
}),
(page.htmlAst.children as ElementContent[]).map((c) =>
normalizeHastElement(c as Element, slug, transcludeTarget),
),
linkToOriginal(),
)
}
// Recurse into the replaced children to resolve nested transclusions,
+20
View File
@@ -720,6 +720,26 @@ ol.overflow {
ul {
padding-left: 1rem;
}
// Obsidian's embed title (the note's title, for a whole-note embed), styled as the
// heading it replaces
> .markdown-embed-title {
font-family: var(--headerFont);
color: var(--dark);
font-size: 1.75rem;
font-weight: bold;
margin: 2.25rem 0 1rem;
&:empty {
display: none;
}
}
// Obsidian's icon link to the embedded note, for Obsidian themes (whose styles show
// it); Quartz links to the original below the content instead
> .markdown-embed-link {
display: none;
}
}
.katex-display {