mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
feat: Obsidian's embed markup for transclusions (embed title, content, rendered view)
This commit is contained in:
4 files changed
+112
-61
No files matched your search
@@ -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.
|
||||
@@ -49,6 +49,38 @@ 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] = (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"])
|
||||
})
|
||||
|
||||
test("resolves a single page transclusion", () => {
|
||||
const root: Root = {
|
||||
type: "root",
|
||||
|
||||
@@ -139,6 +139,36 @@ export function pageResources(
|
||||
return resources
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
function obsidianEmbed(title: string, content: ElementContent[], link: Element): ElementContent[] {
|
||||
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,
|
||||
]
|
||||
}
|
||||
|
||||
/** @internal Exported for testing only. */
|
||||
export function renderTranscludes(
|
||||
root: Root,
|
||||
@@ -172,6 +202,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 +266,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 +298,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,
|
||||
|
||||
@@ -720,6 +720,20 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.katex-display {
|
||||
|
||||
Reference in new issue
Block a user