Repository navigation
Add content search to the editor link input - #3235
Conversation
The rich text editor's link popovers get a combined destination field: plain text searches entries by title across collections, URL-like text behaves as before. Picking a result inserts the entry's public URL resolved from the collection's urlPattern, including locale prefixes and date tokens. Drafts appear for users with draft access and are marked; entries whose URL needs a publish date can't be picked until published. Supporting core changes: the search API gains an additive scope=title option that restricts FTS5 matching to the collection's title column (operators are always term-quoted there so clauses can't escape the column filter), and the auth middleware resolves the session on /_emdash/api/search so status=draft honors content:read_drafts. Anonymous search requests stay on the zero-query path.
🦋 Changeset detectedLatest commit: 55a4aef The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
This PR implements the approved Discussion #3177 Layer 1 feature well: the editor link popover becomes a combined URL/search field, with additive scope=title and search-auth changes. The approach fits EmDash — additive defaults, no schema or API breaks, tests are included, and the changesets describe user-facing behavior.
I checked the diff, the core search/query/route/auth code, the new admin component, the existing URL helpers, and the new/updated tests. Overall the code is solid and well-localized. I found two real issues that should be fixed before merge:
-
Draft date-token entries can still be linked —
LinkDestinationInputonly rejects entries whosepublishedAtis missing, so a draft that carries a publish date (e.g. a previously-published entry returned to draft, or a scheduled draft that stores the date before publish) can still be inserted. The PR explicitly says such entries can’t be picked until published. -
Misleading middleware comment —
auth.tsclaims “Anonymous requests skip the session lookup entirely,” but the code runshandlePublicRouteAuth/resolveSessionUser(session)for everyGET /_emdash/api/search. Anonymous requests skip the user DB lookup, not the session read. A false claim about the logged-out hot path is the kind of comment that misleads future maintainers.
A smaller suggestion: the domain-like heuristic is broad enough to classify ordinary dotted phrases (e.g. react.js) as URLs, which may surprise authors searching for titles containing file extensions or abbreviations.
Tests are generally good; I’d just add a draft-with-publishedAt regression case once the status guard is in place.
Findings
-
[needs fixing]
packages/admin/src/components/editor/LinkDestinationInput.tsx:158-165The date-token guard only checks whether
publishedAtexists. A draft can still carry a publish date (scheduled posts, or an entry that was published and then reverted to draft), in which case the picker will insert a dated public URL even though the entry is not published. That contradicts the stated behavior that “entries whose URL pattern needs a publish date can’t be picked until they are published.”The fetched entry is already in scope inside the
tryblock, but its status is never inspected. The existing test only coverspublishedAt: null; it should also cover a draft with a publish date.let date: string | null | undefined; let entry: ContentItem | undefined; try { entry = await fetchContent(item.collection, item.id, { locale: item.locale }); date = entry.publishedAt; } catch { return null; } if (entry?.status !== "published" || !date) return null; -
[needs fixing]
packages/core/src/astro/middleware/auth.ts:198-203This comment claims that anonymous requests “skip the session lookup entirely and stay on the zero-query hot path,” but the branch immediately above runs
handlePublicRouteAuth, which always callsresolveSessionUser(session). Anonymous callers skip the user DB lookup (getUserById), not the session read. On a route the AGENTS.md calls part of the logged-out hot path, a false claim about how many lookups happen is a real maintenance hazard.// Search filters drafts by permission, so resolve the session user when // one exists. Bearer tokens are not resolved on public routes; token // callers continue to receive published results only. -
[suggestion]
packages/admin/src/components/editor/LinkDestinationInput.tsx:39DOMAIN_LIKEis broad enough to treat many non-domain dotted strings as URLs, such asreact.jsorconfig.yml, which would be rejected as search queries even when they appear in a page title. Consider narrowing the heuristic (e.g. requiring a more domain-looking shape on the right side of the dot) or documenting that titles containing dots should be URL-escaped/pasted.
Scope checkThis PR changes 1,280 lines across 16 files. Large PRs are harder to review and more likely to be closed without review. If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs. See CONTRIBUTING.md for contribution guidelines. |
A draft can still carry a publish date (scheduled entries, or entries unpublished after a release), so the picker now checks the entry status before resolving a dated URL instead of only checking that a date exists. Also corrects the search middleware comment: anonymous callers skip the user DB lookup, not the session read.
|
Both findings addressed in 96d8c6b:
On the |
There was a problem hiding this comment.
This is a re-review of PR #3235. The two blocking issues from the previous review are now fixed, and the regression test coverage for the date-token draft case is in place.
- Draft date-token guard is now correct.
LinkDestinationInputnow checksentry.status !== "published" || !datebefore resolving a date-token URL, so a draft that still carries apublishedAtcannot be linked. A regression test covers this explicitly. - Middleware comment is now accurate.
auth.tsno longer claims that anonymous requests skip the session lookup entirely; it correctly describes that the user DB lookup is skipped for anonymous callers while the session is still resolved when present.
The approach still fits EmDash well: additive scope/auth changes, no schema break, localized UI strings, RTL-safe classes, tests for the new component and integration paths, and useful user-facing changesets. I read the updated admin component, the middleware/search route changes, the core query changes, and the new/updated tests.
I have one remaining low-confidence suggestion: the looksLikeUrl domain-like heuristic remains broad enough to classify common file-extension phrases (e.g. react.js, config.yml) as URLs, which can surprise authors searching for titles containing those strings. It's a UX edge case, not a blocker.
| import { apiFetch, fetchContent, fetchManifest, throwResponseError } from "../../lib/api"; | ||
| import type { ContentItem } from "../../lib/api"; | ||
| import { useDebouncedValue } from "../../lib/hooks"; | ||
| import { contentUrl } from "../../lib/url.js"; |
There was a problem hiding this comment.
[suggestion] DOMAIN_LIKE is broad enough to treat ordinary dotted phrases as URLs, so titles containing file extensions or dotted abbreviations can be misclassified and won't be searched. For example, react.js or config.yml match /^\S+\.\S{2,}$/ even though an author may be searching for a page with exactly that title.
Consider narrowing the heuristic (e.g. requiring a TLD-like right-hand side) or adding a comment documenting that titles containing dots should be URL-escaped/pasted. If this trade-off is intentional and acceptable, feel free to leave it as-is.
| import { contentUrl } from "../../lib/url.js"; | |
| const DOMAIN_LIKE = /^\S+\.[a-z]{2,}$/i; |
Bare domain-like input (vue.js, example.com) is ambiguous between a pasted host and a dotted title, so it now searches too instead of being classified as a URL outright. For such input no result is pre-highlighted: Enter still applies the text as a link target, while arrow keys or a click select a result explicitly. Scheme- or path-like input keeps search disabled as before.
|
Took the |
There was a problem hiding this comment.
This is a re-review of PR #3235. The approach remains sound: the link picker is a focused, additive UI feature supported by scope=title and the auth middleware fix, with no schema break and good test coverage.
I checked the updated LinkDestinationInput, the auth.ts middleware path for /_emdash/api/search, the core search/query.ts title-scope SQL, the route integration, the new/updated tests, the changesets, and the API docs.
The two previously blocking issues are fixed:
- The date-token draft guard in
LinkDestinationInputnow rejects any entry whosestatus !== "published"or that lacks a publish date, with a regression test covering the stale-date draft case. - The
auth.tscomment accurately describes that anonymous callers skip the user DB lookup; session resolution only triggers the user lookup when a session cookie is present.
Lingui wrapping and logical Tailwind classes are correct in the admin component. The FTS5 title-scope SQL validates the column identifier, parameterizes the query, and disables operators so the column filter can't be escaped. Locale filtering, existing API envelope shape, and changeset quality all look good.
The only concern from the previous review—the broad DOMAIN_LIKE heuristic misclassifying some dotted phrases as URLs—still exists, but the code now documents it as a deliberate trade-off and it is low-impact UX, not a correctness issue. Clean otherwise.
Rebasing onto main brought in the link destination search (emdash-cms#3235), whose `onPick` path sets a link mark directly. With an image selected that is the wrong target: the mark goes nowhere and the image keeps its old link, so picking an entry silently does nothing. Both link controls now route every image link write through one `setSelectedImageLink` helper, including the new pick path. That also removes the duplicated "keep the blank flag" logic the emdash-cms#704 review flagged in the toolbar and the bubble menu. Tests: picking an entry with an image selected sets the image's link and leaves text marks alone, and keeps an existing `blank`. The two existing image link tests now address the destination field by its combobox role, since emdash-cms#3235 replaced the plain URL input. Co-Authored-By: Claude <[email protected]>
* feat(admin): optional link on portable-text image blocks
Adds an optional `link: { href; blank? }` on portable-text image blocks,
round-tripped through the PT ↔ PM converters. The existing editor Link
buttons (main toolbar and bubble menu) now operate on image selection:
selecting an image and clicking Link opens the URL popover pre-populated
with the image's current link, and apply/remove updates the image node's
attrs. `Image.astro` wraps the rendered `<img>` in an `<a>` using
`sanitizeHref()` when `link.href` is set. `ImageDetailPanel` gains a
Link URL input and an "Open in new tab" checkbox as a secondary editing
surface.
Split from #679 per review; see discussion in that PR.
* fix(admin): keep an image link's blank flag when its URL is edited
Follow-up to the #704 port. Editing an image link's URL from the toolbar
or bubble menu replaced the whole `link` object, so an existing
`blank: true` ("open in new tab") was silently dropped. The existing
choice is now carried over.
`ImageDetailPanel` only includes `link` in its update payload when a link
is being set or an existing one cleared, so images without links keep the
same payload as before.
Adds tests for the panel's link fields and for the toolbar edit keeping
`blank`.
Co-Authored-By: Claude <[email protected]>
* fix: normalise legacy image links, resync panel link fields, keep anchors same-tab
Addresses the three review findings on #3167.
Legacy link shape: `gutenberg-to-portable-text` emits `link: "https://…"`
(a bare string) for linked images, so WordPress-imported content carries
that shape while the editor writes `{ href, blank? }`. The converters and
`Image.astro` read `link.href` directly, which silently dropped the link
on imported posts. A shared `normalizeImageLink()` now accepts both
shapes; the PT type, `Image.astro`, the PT→PM converter and the admin
editor go through it, so a legacy string renders and is upgraded to the
object shape on its next round-trip. The WordPress URL rewriter handles
both shapes too (it previously assumed a string). Admin has no dependency
on the core package, so it carries a small mirror of the helper.
Panel resync: `ImageDetailPanel`'s node-switch effect reset every field
except `linkHref`/`linkBlank`, leaving the previous image's link in the
form. Both are now reset with the rest.
Same-page anchors: `Image.astro` honoured `blank` unconditionally, unlike
`marks/Link.astro`, which never opens `#section` links in a new tab. The
image wrapper now mirrors that rule.
Tests: unit coverage for `normalizeImageLink` and the legacy-string
round-trip, URL rewriting for both link shapes, Astro render cases for
new-tab / same-page-anchor / legacy-string links, and the panel resync
test now covers the link fields.
Co-Authored-By: Claude <[email protected]>
* fix(admin): let the inline bubble menu open on a selected image
The bubble menu's `shouldShow` only admitted `TextSelection` and
`AllSelection`. A selected image is a `NodeSelection`, so the image
branches of the menu's link handling (added with the image link
feature) were never reachable: selecting an image never showed the
menu at all.
`shouldShow` now also admits a `NodeSelection` whose node is an image.
When the selection is an image, the menu renders only the link
control; the text-mark buttons (bold, italic, …) are meaningless on an
image and are hidden.
Adds bubble-menu tests for a selected image: the menu appears with the
link control only, applying a URL sets `link`, editing the URL keeps
`blank`, and Remove link clears it.
Co-Authored-By: Claude <[email protected]>
* fix(admin): link the selected image when a search result is picked
Rebasing onto main brought in the link destination search (#3235), whose
`onPick` path sets a link mark directly. With an image selected that is
the wrong target: the mark goes nowhere and the image keeps its old
link, so picking an entry silently does nothing.
Both link controls now route every image link write through one
`setSelectedImageLink` helper, including the new pick path. That also
removes the duplicated "keep the blank flag" logic the #704 review
flagged in the toolbar and the bubble menu.
Tests: picking an entry with an image selected sets the image's link and
leaves text marks alone, and keeps an existing `blank`. The two existing
image link tests now address the destination field by its combobox role,
since #3235 replaced the plain URL input.
Co-Authored-By: Claude <[email protected]>
* fix(admin): name the toolbar link button "Image link" while an image is selected
The toolbar link button already switched its tooltip to "Image link"
when an image is selected, but its accessible name stayed "Insert Link",
so screen reader users heard a different label from the one shown.
The aria-label now follows the same condition as the tooltip.
The three tests that open the link control with an image selected now
locate the button by the exact name "Image link", so they fail if the
two drift apart again.
Co-Authored-By: Claude <[email protected]>
* fix(admin): keep image link controls reachable
* fix(core): relativize portable image link objects
---------
Co-authored-by: Nicholas Penree <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: Matt Kane <[email protected]>
What does this PR do?
Adds an internal link picker to the rich text editor, as proposed in Discussion #3177 (Layer 1). The link popovers (toolbar and bubble menu) replace the plain URL input with a combined destination field, modeled on WordPress's link control:
urlPattern, including locale prefixes and date tokens (the entry is fetched on demand only for date-token patterns). Entries whose URL needs a publish date can't be picked until they are published — the picker explains why instead of baking a broken{year}href into the content.content:read_draftsalso find drafts, marked with the standard status badge.Two additive core changes support this:
search()/searchCollection()helpers gain ascope=titleoption that restricts the FTS5 match to the collection's title column, so body-text matches don't surprise authors picking a link target. Queries containing FTS5 operators are always term-quoted under this scope so no clause can escape the column filter (covered by a regression test).GET /_emdash/api/search, sostatus=drafthonors thecontent:read_draftspermission instead of silently falling back to published results. Anonymous requests skip the session lookup entirely and stay on the zero-query hot path (covered by a unit test asserting no user lookup happens without a session cookie).Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output
Searching "team" in the link popover lists matching entries by title, with the first result pre-highlighted for Enter:
Drafts are listed for editors with draft access and marked with the standard status badge:
Picking a result inserts the entry's resolved public URL as the link: