mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
fix(transclude): find blocks by their lowercased name; docs for #^id block anchors
This commit is contained in:
5 files changed
+53
-3
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": patch
|
||||
---
|
||||
|
||||
Block anchors are `#^id`: with @quartz-community/utils 1.3.2 and rehype-obsidian 1.6.0, blocks get the id `^id` and links to them `#^id` (was `#id`), as in Obsidian, so block `^para-1` and heading `## Para 1` no longer share an id. A block transclusion finds its block whatever the case of its `^id` (`![[note#^Para-1]]`), and the popover scrolls to a block it links to.
|
||||
@@ -65,5 +65,6 @@ Heading ids follow the GitHub rule (`github-slugger`, as `rehype-slug` and the t
|
||||
- `slugHeading(text)`: the id of a heading with this text;
|
||||
- `HeadingSlugger`: slugs all headings of one page in order and numbers repeats (`foo`, `foo-1`, …) — create one per page;
|
||||
- `obsidianHeadingAnchor(text)`: the anchor Obsidian writes for a heading (it replaces `:`, `|`, `^`, `#` and `\` with spaces); slug it with `slugHeading` and add it as an extra id when it differs;
|
||||
- `resolveAnchorHref(href, ids)`: points a same-page `#anchor` at an id that exists on the page (an id as written, a block reference `#^name`, or a heading's text);
|
||||
- `resolveAnchorHref(href, ids)`: points a same-page `#anchor` at an id that exists on the page (an id as written, or a heading's text); a block reference `#^name` always points at the block (`#^name`), never at a heading;
|
||||
- `blockAnchor(name)`: the anchor of the block `^name`, `#^name` (lowercased). Blocks have the id `^name`, so they never share an id with a heading;
|
||||
- `slugTag(tag)`: the slug of a tag, as used for tag pages.
|
||||
@@ -39,6 +39,7 @@ npx quartz plugin install --latest
|
||||
|
||||
### Changes you may notice
|
||||
|
||||
- **Block anchors**: a block's id is now `^id` and links to it are `#^id` (`[[note#^para-1]]` → `note#^para-1`, was `note#para-1`), as in Obsidian, where blocks and headings are separate namespaces: block `^para-1` and heading `## Para 1` no longer share an id, so `[[#^para-1]]` and `[[#Para 1]]` each land on their own target. Links inside the site are regenerated; old `#id` URLs of blocks from outside the site now go to the top of the page (or to a heading of that name). CSS or scripts that select a block by id need `#\^para-1` (or `getElementById`).
|
||||
- **Unresolved links**: `configuration.links.disableBrokenWikilinks` is now on by default. Links to pages that don't exist get the `broken` class and are styled as unresolved, as in Obsidian. Links to pages a filter removes (drafts, unpublished notes) are marked the same way, with `data-filtered="<filter>"`. Set `links.disableBrokenWikilinks: false` to style them as before.
|
||||
- **Mermaid diagrams** render with [Pele](https://pele.run) by default, as in Obsidian, inside a `<div class="mermaid">` rather than a code block. Set `diagramRenderer: mermaid` on [[ObsidianFlavoredMarkdown]] to keep Mermaid.js. See [[Mermaid diagrams]].
|
||||
- **Block wrappers**: each top-level block of a note is wrapped in the `div` Obsidian puts it in (`div.el-p > p`, `div.el-h2 > h2`). Custom CSS that selects a page's blocks as direct children (`article > p`) no longer matches. Select them without `>`, or set `blockWrappers: false` on ObsidianFlavoredMarkdown.
|
||||
|
||||
@@ -120,6 +120,48 @@ describe("renderTranscludes", () => {
|
||||
assert.ok(texts.includes("Target content"), "transcluded content should be inlined")
|
||||
})
|
||||
|
||||
test("a block transclusion finds the block by its name and scopes its ^id", () => {
|
||||
const block: Element = {
|
||||
type: "element",
|
||||
tagName: "p",
|
||||
properties: { id: "^para-1" },
|
||||
children: [
|
||||
{ type: "text", value: "Block text " },
|
||||
{
|
||||
type: "element",
|
||||
tagName: "a",
|
||||
properties: { href: "#^para-1" },
|
||||
children: [{ type: "text", value: "self" }],
|
||||
},
|
||||
],
|
||||
}
|
||||
const heading: Element = {
|
||||
type: "element",
|
||||
tagName: "h2",
|
||||
properties: { id: "para-1" },
|
||||
children: [{ type: "text", value: "Para 1" }],
|
||||
}
|
||||
const targetHtml: Root = { type: "root", children: [heading, block] }
|
||||
const allFiles = [makePageData("target", targetHtml, { blocks: { "para-1": block } })]
|
||||
const root: Root = {
|
||||
type: "root",
|
||||
children: [makeTranscludeBlockquote("target", "#^Para-1")],
|
||||
}
|
||||
renderTranscludes(
|
||||
root,
|
||||
cfg,
|
||||
"current" as FullSlug,
|
||||
makeComponentData(allFiles) as QuartzComponentProps,
|
||||
new Set<FullSlug>(["current" as FullSlug]),
|
||||
)
|
||||
const html = toHtml(root)
|
||||
assert.ok(html.includes("Block text"), html)
|
||||
assert.ok(!html.includes("Para 1"), "the heading of the same name isn't transcluded")
|
||||
const id = /<p id="([^"]+)"/.exec(html)?.[1]
|
||||
assert.ok(id?.endsWith("^para-1") && id !== "^para-1", `scoped block id, got ${id}`)
|
||||
assert.ok(html.includes(`href="#${id}"`), "the link inside points at the scoped id")
|
||||
})
|
||||
|
||||
test("allows the same page to be embedded twice as siblings", () => {
|
||||
const root: Root = {
|
||||
type: "root",
|
||||
|
||||
@@ -308,8 +308,9 @@ export function renderTranscludes(
|
||||
|
||||
let blockRef = el.properties.dataBlock as string | undefined
|
||||
if (blockRef?.startsWith("#^")) {
|
||||
// block transclude
|
||||
blockRef = blockRef.slice("#^".length)
|
||||
// block transclude: `data.blocks` is keyed by the block's name, lowercased (`para-1`); the
|
||||
// block element's id is `^para-1` (rehype-obsidian), its anchor `#^para-1`
|
||||
blockRef = blockRef.slice("#^".length).toLowerCase()
|
||||
let blockNode = page.blocks?.[blockRef]
|
||||
if (blockNode) {
|
||||
if (blockNode.tagName === "li") {
|
||||
|
||||
Reference in new issue
Block a user