Compare commits

...
9 changed files with 144 additions and 11 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
@@ -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)
})
})