Compare commits

..
13 changed files with 192 additions and 17 deletions

No files matched your search

@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
Reinstalling an npm plugin at the version pinned in `quartz.lock.json` (after a fresh clone, or on a CI runner) no longer rewrites its `installedAt`, so `npx quartz plugin install` leaves the lockfile unchanged, as it does for Git plugins.
+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
Popovers show next to their link with Obsidian themes (quartz-themes-core) too: its Obsidian base styles made them `position: absolute`, so on a scrolled page they appeared a scroll's height above the link, often off screen — links to headings and an embed's "link to original" seemed to show no popover. A popover for a link to a heading now also scrolls the heading to the top of the popover when the theme positions the page's content box.
+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
Links resolve as in Obsidian in more cases (`@quartz-community/utils` 1.3.5): `[[./index|index]]` in a subfolder links to that folder's index page instead of the home page; when several notes share a name, a link prefers the one under the linking note's folder, then the shortest path, instead of pointing to a page that doesn't exist (for example `[Status](current.md)` in a vault where several folders have a `current.md`); a `../` link that names no note resolves from the folder it leads to; and a link starting with `/` is a path from the root.
+4 -4
View File
@@ -97,7 +97,7 @@
"devDependencies": {
"@changesets/cli": "^3.0.3",
"@quartz-community/types": "^1.3.1",
"@quartz-community/utils": "^1.3.4",
"@quartz-community/utils": "^1.3.5",
"@types/hast": "^3.0.4",
"@types/node": "^26.5.1",
"@types/pretty-time": "^1.1.5",
@@ -3230,9 +3230,9 @@
}
},
"node_modules/@quartz-community/utils": {
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/@quartz-community/utils/-/utils-1.3.4.tgz",
"integrity": "sha512-S1zlszf1jcHXcWXnlow/VxW3kKlTEWWNGQmWP07b8vAOB8lAIkeYRR5KoUA0TdjZPgN5dRaFHaAhv6sC2g/4Gw==",
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/@quartz-community/utils/-/utils-1.3.5.tgz",
"integrity": "sha512-ZN8nlrjrwnrGlOfCArOPqOhAJNiKqhpPp6sqsN31ZoOjCQkExqd5f1+vMpy48SezjFup+k9XJ+yPHRkaGjtPIg==",
"license": "MIT",
"dependencies": {
"@quartz-community/types": "^1.0.0"
+1 -1
View File
@@ -124,7 +124,7 @@
"devDependencies": {
"@changesets/cli": "^3.0.3",
"@quartz-community/types": "^1.3.1",
"@quartz-community/utils": "^1.3.4",
"@quartz-community/utils": "^1.3.5",
"@types/hast": "^3.0.4",
"@types/node": "^26.5.1",
"@types/pretty-time": "^1.1.5",
+5
View File
@@ -34,6 +34,11 @@ export function installNpmPlugin(
},
): NpmInstallResult
export function removeNpmPlugin(name: string, pluginsDir: string): void
export function nextNpmLockEntry(
lockEntry: unknown,
source: unknown,
result: NpmInstallResult,
): NpmLockEntry
export function ensureNpmPlugin(
name: string,
options: {
+17
View File
@@ -254,6 +254,23 @@ export function removeNpmPlugin(name, pluginsDir) {
}
}
/**
* The lock entry for an npm plugin just installed from `source`. Reinstalling the pinned version
* (a fresh clone, `npm ci`, a CI runner) keeps `lockEntry` as it is, `installedAt` included, so
* restoring plugins leaves quartz.lock.json unchanged, as it does for Git plugins.
*/
export function nextNpmLockEntry(lockEntry, source, result) {
if (
isNpmLockEntry(lockEntry) &&
lockEntry.source === source &&
lockEntry.version === result.version &&
(!lockEntry.integrity || !result.integrity || lockEntry.integrity === result.integrity)
) {
return lockEntry
}
return { source, ...result, installedAt: new Date().toISOString() }
}
/**
* Make sure the pinned npm plugin `name` is installed as `lockEntry` pins it (installed at the
* locked version, else at `fallbackVersion`). Returns the lock entry fields when it installed
+36
View File
@@ -11,6 +11,7 @@ import {
isNpmLockEntry,
isPinnedNpmPluginInstalled,
listPluginDirNames,
nextNpmLockEntry,
npmPackageName,
npmPluginDir,
readNpmLockEntry,
@@ -241,3 +242,38 @@ describe("ensureNpmPlugin", () => {
assert.strictEqual(ensureNpmPlugin("@me/plugin", { pluginsDir }), null)
})
})
describe("nextNpmLockEntry", () => {
const pinned = {
source: "@me/plugin",
version: "1.4.2",
integrity: "sha512-good",
resolved: "https://registry.npmjs.org/@me/plugin/-/plugin-1.4.2.tgz",
installedAt: "2026-01-01T00:00:00.000Z",
}
const result = { version: "1.4.2", integrity: "sha512-good", resolved: pinned.resolved }
test("reinstalling the pinned version keeps the lock entry, installedAt included", () => {
assert.strictEqual(nextNpmLockEntry(pinned, "@me/plugin", result), pinned)
})
test("a new version, source or package is a new entry", () => {
const updated = nextNpmLockEntry(pinned, "@me/plugin", { ...result, version: "2.0.0" })
assert.strictEqual(updated.version, "2.0.0")
assert.notStrictEqual(updated.installedAt, pinned.installedAt)
assert.notStrictEqual(nextNpmLockEntry(pinned, "@me/plugin@^1", result), pinned)
assert.notStrictEqual(
nextNpmLockEntry(pinned, "@me/plugin", { ...result, integrity: "sha512-other" }),
pinned,
)
const added = nextNpmLockEntry(undefined, "@me/plugin", result)
assert.deepStrictEqual(
{ ...added, installedAt: undefined },
{
source: "@me/plugin",
...result,
installedAt: undefined,
},
)
})
})
+5 -5
View File
@@ -61,6 +61,7 @@ import {
installNpmPlugin,
isHostDependency,
isNpmLockEntry,
nextNpmLockEntry,
latestNpmVersion,
latestNpmVersionAsync,
listPluginDirNames,
@@ -614,12 +615,11 @@ async function syncNpmPlugins({ pluginsJson, names, fromConfig, latest, dryRun }
pluginsDir: PLUGINS_DIR,
})
linkPeerPlugins(npmPluginDir(pkg, PLUGINS_DIR))
lockfile.plugins[pkg] = {
source: entry.source,
...result,
installedAt: new Date().toISOString(),
const next = nextNpmLockEntry(lockEntry, entry.source, result)
if (next !== lockEntry) {
lockfile.plugins[pkg] = next
changed = true
}
changed = true
installedAny = true
console.log(styleText("green", `✓ Installed ${pkg}@${result.version}`))
} catch (err) {
@@ -0,0 +1,37 @@
/**
* Where a popover goes, and how far its content scrolls to show a heading. Kept out of
* popover.inline.ts so tests can import them.
*/
/**
* The popover's inline style for a position from floating-ui's `fixed` strategy. The
* position is set here, not left to stylesheets: Obsidian's app.css, which quartz-themes-core
* bundles for Obsidian themes, makes `.popover` `position: absolute`, and the viewport
* coordinates then landed the popover a page's scroll above its link.
*/
export function popoverPlacement(x: number, y: number) {
return {
position: "fixed",
top: "0",
left: "0",
transform: `translate(${x.toFixed()}px, ${y.toFixed()}px)`,
}
}
interface Box {
getBoundingClientRect(): { top: number }
}
/**
* The scrollTop that shows `target` `buffer` px below the top of `inner`, measured on screen:
* `offsetTop` is relative to the nearest positioned ancestor, which a theme can put inside the
* popover (Obsidian themes position `.markdown-preview-view`).
*/
export function scrollTopFor(inner: Box & { scrollTop: number }, target: Box, buffer = 12) {
return (
target.getBoundingClientRect().top -
inner.getBoundingClientRect().top +
inner.scrollTop -
buffer
)
}
+3 -4
View File
@@ -1,6 +1,7 @@
import { computePosition, flip, inline, shift } from "@floating-ui/dom"
import { normalizeRelativeURLs } from "../../util/path"
import { fetchCanonical } from "./util"
import { popoverPlacement, scrollTopFor } from "./popover-layout"
const p = new DOMParser()
let activeAnchor: HTMLAnchorElement | null = null
@@ -19,9 +20,7 @@ async function mouseEnterHandler(
strategy: "fixed",
middleware: [inline({ x: clientX, y: clientY }), shift(), flip()],
})
Object.assign(popoverElement.style, {
transform: `translate(${x.toFixed()}px, ${y.toFixed()}px)`,
})
Object.assign(popoverElement.style, popoverPlacement(x, y))
}
function showPopover(popoverElement: HTMLElement) {
@@ -37,7 +36,7 @@ async function mouseEnterHandler(
const heading = inner.querySelector(targetAnchor) as HTMLElement | null
if (heading) {
// leave ~12px of buffer when scrolling to a heading
inner.scroll({ top: heading.offsetTop - 12, behavior: "instant" })
inner.scroll({ top: scrollTopFor(inner, heading), behavior: "instant" })
}
}
}
+31 -2
View File
@@ -1,10 +1,15 @@
import test, { describe } from "node:test"
import assert from "node:assert"
import { popoverPlacement, scrollTopFor } from "./popover-layout"
type ScrollArg = { top: number; behavior?: ScrollBehavior }
// a heading `offsetTop` px from the top of the popover's content (not scrolled)
type FakeHeading = { offsetTop: number }
const box = (top: number) => ({ getBoundingClientRect: () => ({ top }) })
interface FakePopoverInner {
scrollTop: number
getBoundingClientRect: () => { top: number }
scroll: (arg: ScrollArg) => void
querySelector: (sel: string) => FakeHeading | null
_scrolled: ScrollArg | null
@@ -25,6 +30,8 @@ interface FakePopoverElement {
function makeInner(heading: FakeHeading | null = null): FakePopoverInner {
const inner: FakePopoverInner = {
scrollTop: 0,
getBoundingClientRect: () => ({ top: 0 }),
scroll(arg) {
this._scrolled = arg
},
@@ -84,7 +91,7 @@ function fixedShowPopover(
const targetAnchor = `#popover-internal-${hash.slice(1)}`
const heading = inner.querySelector(targetAnchor)
if (heading) {
inner.scroll({ top: heading.offsetTop - 12, behavior: "instant" })
inner.scroll({ top: scrollTopFor(inner, box(heading.offsetTop)), behavior: "instant" })
}
}
}
@@ -107,7 +114,7 @@ describe("showPopover on cache-hit with hash", () => {
assert.deepStrictEqual(
inner._scrolled,
{ top: 200 - 12, behavior: "instant" },
"scroll must target heading.offsetTop - 12",
"scroll must show the heading 12px below the top",
)
assert.deepStrictEqual(inner._selectorsQueried, ["#popover-internal-plugins"])
})
@@ -221,3 +228,25 @@ describe("bfcache popover cleanup", () => {
assert.strictEqual(queried, false)
})
})
describe("popover layout", () => {
test("the popover is placed with position: fixed, which its coordinates are for", () => {
// Obsidian's app.css (quartz-themes-core) sets .popover { position: absolute }
assert.deepStrictEqual(popoverPlacement(764.4, -4.6), {
position: "fixed",
top: "0",
left: "0",
transform: "translate(764px, -5px)",
})
})
test("scrolls to a heading by its position on screen, not its offsetTop", () => {
// the content box starts 100px into the popover (an Obsidian theme positions
// .markdown-preview-view, so the heading's offsetTop counts from there)
const inner = { scrollTop: 0, ...box(200) }
assert.strictEqual(scrollTopFor(inner, box(200 + 100 + 5944)), 100 + 5944 - 12)
// already scrolled: the same target
const scrolled = { scrollTop: 3000, ...box(200) }
assert.strictEqual(scrollTopFor(scrolled, box(200 + 100 + 5944 - 3000)), 100 + 5944 - 12)
})
})
+38 -1
View File
@@ -435,6 +435,42 @@ describe("links built-in", () => {
assert.deepStrictEqual(file.data.links?.sort(), ["folder/sibling", "t"])
})
// #2725: resolved as Obsidian does (utils 1.3.5)
test("a ./ link is in the page's folder, under every strategy", () => {
for (const markdownLinkResolution of ["shortest", "absolute", "relative"]) {
const { els, file } = run(
[a("./index", "index"), a("./sibling")],
"folder/special",
{ markdownLinkResolution },
["index", "folder/index", "folder/special", "folder/sibling", "sibling"],
)
const [index, sibling] = els.filter((e) => e.tagName === "a")
assert.strictEqual(index.properties.href, "./", markdownLinkResolution)
assert.strictEqual(index.properties["data-slug"], "folder/index", markdownLinkResolution)
assert.strictEqual(sibling.properties["data-slug"], "folder/sibling", markdownLinkResolution)
// the folder page, not the home page
assert.ok(file.data.links?.includes("folder/" as SimpleSlug), markdownLinkResolution)
}
})
test("of several pages with a name, the one under the page's folder, else the shortest path", () => {
const allSlugs = [
"index",
"projects/alpha/index",
"projects/alpha/current",
"projects/beta/index",
"projects/beta/current",
]
const opts = { markdownLinkResolution: "shortest" }
const slugOf = (src: string, target: string) =>
run([a(target)], src, opts, allSlugs).els[0]!.properties["data-slug"]
// relative-mode vaults write a note in the same folder as a bare name
assert.strictEqual(slugOf("projects/alpha/index", "current.md"), "projects/alpha/current")
assert.strictEqual(slugOf("projects/beta/index", "current"), "projects/beta/current")
// from elsewhere, the shortest path (Obsidian sorts by path length)
assert.strictEqual(slugOf("index", "current"), "projects/beta/current")
})
test("slugs.stripDiacritics: link targets are stripped like the slugs they point at (#2410)", () => {
setSlugOptions({ stripDiacritics: true })
try {
@@ -451,7 +487,8 @@ describe("links built-in", () => {
["ceske/page", "ceske/cestina", "jine/prilis", "ceske/obrazek.png"],
)
const [cestina, prilis] = els.filter((e) => e.tagName === "a")
assert.strictEqual(cestina.properties.href, "../ceske/cestina#český-nadpis")
// ./ is relative to ceske/page (utils 1.3.5): the same page as ../ceske/cestina
assert.strictEqual(cestina.properties.href, "./cestina#český-nadpis")
assert.ok(!(cestina.properties.className as string[]).includes("broken"))
assert.strictEqual(prilis.properties.href, "../jine/prilis")
assert.strictEqual(