Skip to content

feat(admin): optional link on portable-text image blocks - #3167

Merged
ascorbic merged 10 commits into
emdash-cms:mainfrom
kwmr:image-link
Sep 25, 2026
Merged

ascorbic merged 10 commits into
emdash-cms:mainfrom
kwmr:image-link

Conversation

@kwmr

@kwmr kwmr commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Supersedes #704 (@drudge's implementation, re-ported onto current main per the handoff in that PR). Approved in Discussion #2741.

Adds an optional link: { href: string; blank?: boolean } to Portable Text image blocks, round-tripped through the PT ↔ ProseMirror converters.

  • Editor: the existing Link buttons (toolbar and bubble menu) now act on an image selection — the popover is pre-filled with the image's current link; apply/remove updates the image node's attrs. ImageDetailPanel gains a "Link URL" input and an "Open in new tab" checkbox as a secondary surface.
  • Renderer: Image.astro wraps the image in <a> when link.href is set, via sanitizeHref(); blank adds target="_blank" rel="noopener noreferrer".
  • Converters: half-populated links (empty/whitespace href) are dropped on save so { blank: true } never leaks into Portable Text.

Relative to #704: re-ported onto current main (the original no longer applied cleanly — identityAttrs/attrsWithPortableTextKey, image alignment and Kumo imports had landed on the same lines); new UI strings are wrapped with Lingui per the review; and the case flagged on #704 where editing an image link's URL from the toolbar/bubble menu dropped an existing blank: true is fixed, with tests.

Closes #2497

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation. No messages.po changes included.
  • I have added and reviewed the user-facing changeset
  • New features link to an approved Discussion: Optional link on image blocks (picking up PR #704) #2741
  • I have included screenshots below

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Code (Claude Fable 5.1 (this PR); an earlier Claude Code session did the Aug 27 port)

Screenshots / test output

Image settings panel with the new Link URL field and Open in new tab checkbox

Targeted tests run locally: packages/core converter suites (45 passing) and packages/admin browser tests for ImageDetailPanel, toolbar, bubble menu, image selection and alignment (169 passing).

@changeset-bot

changeset-bot Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bc5025f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Minor
@emdash-cms/admin Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/cloudflare Minor
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
create-emdash Minor
@emdash-cms/x402 Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@kwmr

kwmr commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@github-actions

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Sep 19, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3167

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3167

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3167

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3167

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3167

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3167

emdash

npm i https://pkg.pr.new/emdash@3167

create-emdash

npm i https://pkg.pr.new/create-emdash@3167

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3167

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3167

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3167

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3167

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3167

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3167

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3167

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3167

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3167

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3167

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3167

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3167

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3167

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3167

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3167

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3167

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3167

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3167

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3167

commit: bc5025f

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a well-scoped, approved feature (Discussion #2741) that adds a wrapped link to Portable Text image blocks consistently across the admin editor and the Astro renderer. The implementation is mostly careful: half-populated { blank: true } objects are dropped on save, Lingui wrapping is present, and tests cover the blank-preservation regression from #704.

I found three issues worth addressing before merge:

  1. Legacy image-link shape collision. The WordPress/Gutenberg converter has been emitting image links as link: "url" (a plain string). The new core/admin converters and Image.astro treat link as { href, blank? }. Existing imported posts with string links will silently drop their link metadata the first time they are edited. The PT→PM converters, renderer, and ideally the Gutenberg converter + WordPress URL rewriter should normalize both shapes.
  2. ImageDetailPanel state-sync omission. The existing useEffect that resets form state for a new attribute snapshot does not reset linkHref/linkBlank, leaving stale link state if the panel is ever updated in place.
  3. Same-page anchor handling. Link.astro forces same-tab behavior for #section anchors even when blank is true; Image.astro should mirror that behavior for consistency.

Fixing #1 is the most important — it prevents data loss for any site that has imported WordPress content with linked images.


Findings

  • [needs fixing] packages/core/src/content/converters/portable-text-to-prosemirror.ts:665

    convertImage dereferences block.link.href/blank, assuming link is already the new object shape. Existing WordPress/Gutenberg imports store image links as link: "url" strings, so loading such a post silently drops the link here and loses it on the next save. Normalize both legacy strings and the new object before building the ProseMirror attrs.

    			link: normalizeImageLink(block.link),
    

    Add a small helper in this file (or in converters/types.ts):

    function normalizeImageLink(link: unknown): { href: string; blank?: boolean } | null {
    	if (!link) return null;
    	if (typeof link === "string") {
    		const href = link.trim();
    		return href ? { href } : null;
    	}
    	if (typeof link === "object" && "href" in link) {
    		const href =
    			typeof (link as Record<string, unknown>).href === "string"
    				? ((link as Record<string, unknown>).href as string).trim()
    				: "";
    		if (!href) return null;
    		return (link as Record<string, unknown>).blank === true
    			? { href, blank: true }
    			: { href };
    	}
    	return null;
    }
  • [needs fixing] packages/admin/src/components/PortableTextEditor.tsx:1242

    The admin PT→PM conversion makes the same object-only assumption as the core converter. Existing imported content with link: "url" will lose its link when opened in the editor. Use the same normalization so both legacy strings and the new object shape round-trip.

    					link: normalizeImageLink(imageBlock.link),
    

    (Define normalizeImageLink locally or import a shared helper from the core converters.)

  • [needs fixing] packages/core/src/components/Image.astro:83

    The renderer only reads link.href, so a legacy link: "url" from an existing WordPress import would not render as a link even after the converters normalize it. Accept either shape in the render path.

    const rawHref = typeof link === "string" ? link : link?.href;
    const linkHref = rawHref ? sanitizeHref(rawHref) : null;
    
  • [needs fixing] packages/gutenberg-to-portable-text/src/index.ts:236

    The Gutenberg converter is still emitting the old link: string shape. Now that the canonical Portable Text image block uses { href, blank? }, new imports should produce the same shape so downstream code does not need to carry two shapes forever.

    							link: linkUrl ? { href: linkUrl } : undefined,
    

    Also update packages/gutenberg-to-portable-text/src/types.ts and packages/core/src/astro/routes/api/import/wordpress/rewrite-url-helpers.ts so the type and the WordPress URL rewriter operate on the new object shape.

  • [needs fixing] packages/core/src/astro/routes/api/import/wordpress/rewrite-url-helpers.ts:120

    After moving the Gutenberg converter to the object shape, this rewriter will miss imported image links because it reads block.link as a string. Handle both legacy strings and the new object.

    		if (block._type === "image" && block.link) {
    			const href = typeof block.link === "string" ? block.link : block.link.href;
    			const newUrl = findMatchingUrl(href, exactMap, baseMap);
    			if (newUrl) {
    				if (typeof block.link === "string") {
    					block.link = newUrl;
    				} else {
    					block.link.href = newUrl;
    				}
    				changed = true;
    				urlsRewritten++;
    			}
    		}
    
  • [needs fixing] packages/admin/src/components/editor/ImageDetailPanel.tsx:153-162

    The existing useEffect resets alt/caption/title/asset/size/alignment when the attribute snapshot changes, but it omits linkHref and linkBlank. If the panel is ever updated in place (or the node-key trigger changes), the link fields stay stale.

    	React.useEffect(() => {
    		setAlt(attributes.alt ?? "");
    		setCaption(attributes.caption ?? "");
    		setTitle(attributes.title ?? "");
    		setAsset(attributes);
    		setHasCustomSize(attributes.displayWidth != null || attributes.displayHeight != null);
    		setDisplayWidth(attributes.displayWidth ?? undefined);
    		setDisplayHeight(attributes.displayHeight ?? undefined);
    		setLockAspectRatio(true);
    		setAlignment(attributes.alignment);
    		setLinkHref(attributes.link?.href ?? "");
    		setLinkBlank(Boolean(attributes.link?.blank));
    		// eslint-disable-next-line react-hooks/exhaustive-deps -- the node token identifies a new attribute snapshot
    	}, [nodeKey]);
    
  • [suggestion] packages/core/src/components/Image.astro:84

    Link.astro suppresses target="_blank" for same-page anchors (#section) because opening an on-page jump in a new tab is undesirable. Image.astro currently honors blank: true unconditionally, so an image linked to #section would open in a new tab. Consider mirroring Link.astro.

    const linkHref = link?.href ? sanitizeHref(link.href) : null;
    const isSamePageAnchor = !!linkHref && linkHref.startsWith("#");
    const linkTarget = link?.blank && !isSamePageAnchor ? "_blank" : undefined;
    const linkRel = link?.blank && !isSamePageAnchor ? "noopener noreferrer" : undefined;
    

@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed bot:review Trigger an emdashbot code review on this PR review/needs-review No maintainer or bot review yet labels Sep 19, 2026
kwmr added a commit to kwmr/emdash that referenced this pull request Sep 19, 2026
…hors same-tab

Addresses the three review findings on emdash-cms#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]>
@github-actions github-actions Bot added review/needs-rereview New commits since the last review size/XL and removed size/L review/awaiting-author Reviewed; waiting on the author to respond labels Sep 19, 2026
@kwmr

kwmr commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All three are addressed in 5aa743b.

  1. Legacy link shape — added normalizeImageLink() in core and routed
    the PT type, Image.astro, the PT→PM converter and the admin editor
    through it, so link: "url" from Gutenberg imports renders correctly
    and is upgraded to { href, blank? } on the next round-trip.
    rewrite-url-helpers now handles both shapes too. Admin has no
    dependency on core, so it carries a small mirror of the helper.
    I left gutenberg-to-portable-text emitting the string shape for
    now to keep this PR contained; happy to switch it to the object
    shape here or in a follow-up, whichever you prefer.

  2. ImageDetailPanel resync — linkHref/linkBlank are now reset
    in the node-switch effect, with the resync test extended to cover
    them.

  3. Same-page anchors — Image.astro now mirrors marks/Link.astro
    and never opens #section links in a new tab.

Tests added for the normaliser, the legacy-string round-trip, URL
rewriting for both shapes, and the three render cases.


Generated by Claude Code

khoinguyenpham04 pushed a commit to kwmr/emdash that referenced this pull request Sep 20, 2026
…hors same-tab

Addresses the three review findings on emdash-cms#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]>
@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Sep 21, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-review confirms that all three issues from the previous review have been addressed:

  1. Legacy link: string shape — normalizeImageLink() now lives in packages/core/src/content/converters/image-link.ts and is used by the core PT↔PM converters, Image.astro, and (via a mirrored local helper) the admin editor. Existing WordPress-imported linked images keep their link and are upgraded to the object shape on the first round-trip.
  2. ImageDetailPanel state sync — the reset useEffect now resets linkHref and linkBlank alongside the other fields, with a regression test.
  3. Same-page anchor handling — Image.astro mirrors Link.astro: #section links never open in a new tab.

The approach is sound: read-time normalization is a defensible way to support legacy strings without touching the Gutenberg package, and the new UI surfaces are properly Lingui-wrapped.

I found one new issue: the PR claims the image link can be edited from the bubble menu, but the bubble menu's shouldShow only allows TextSelection and AllSelection. Images in this editor are selected via NodeSelection, so the bubble menu never appears for an image and the new image-link bubble-menu code is unreachable. Toolbar and sidebar editing still work, but the bubble-menu path is dead.

Fixing that plus a focused regression test for the bubble-menu image-link flow would make this ready to merge.


Findings

  • [needs fixing] packages/admin/src/components/PortableTextEditor.tsx:3974

    The inline bubble menu's shouldShow only allows TextSelection or AllSelection, but images in this editor are selected with a NodeSelection (see ImageNodeView.handlePointerDown which calls editor.commands.setNodeSelection). Because of this, the bubble menu never appears when an image is selected, so the image-link support added to the bubble menu (activeMarks.imageLink, the editor.isActive("image") branches in handleSetLink/handleRemoveLink, etc.) is unreachable. The PR description says the bubble-menu link button works on image selections.

    Allow image NodeSelections in this check (and import NodeSelection so the condition can be type-safe):

    import { AllSelection, NodeSelection, TextSelection } from "@tiptap/pm/state";
    
    			shouldShow={({ editor: activeEditor, element, state, view }) => {
    				const { selection } = state;
    				const isImageNodeSelection =
    					selection instanceof NodeSelection && selection.node.type.name === "image";
    				return (
    					activeEditor.isEditable &&
    					(selection instanceof TextSelection ||
    						selection instanceof AllSelection ||
    						isImageNodeSelection) &&
    					!selection.empty &&
    					(view.hasFocus() || element.contains(document.activeElement))
    				);
    			}}
    

    Once this is wired up, add a browser test that selects an image node and opens/edits the link from the bubble menu so this path doesn't regress again.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Sep 21, 2026
@swissky swissky added the bot:review Trigger an emdashbot code review on this PR label Sep 23, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a focused re-review of the image-link feature. The three blockers from the previous review are resolved: legacy link: string links are normalized by the shared normalizeImageLink() helper, ImageDetailPanel now resets linkHref/linkBlank when the selected node changes, and Image.astro correctly treats #anchor links as same-page jumps. New UI strings are Lingui-wrapped and the changeset is user-facing and proportionate.

I found one remaining functional issue: the PR enables the inline bubble menu for image selections, but the shouldShow predicate still requires !selection.empty. A NodeSelection (the selection type used when an image node is selected) has the same anchor/head position, so selection.empty is true. Because of that guard, the inline bubble menu will still not appear for selected images and the new bubble-menu image-link code remains unreachable. The existing bubble-menu.test.tsx regression tests need this guard adjusted before they can exercise that path.

Aside from that, the PR is in good shape. One small accessibility/usability suggestion is noted.

@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review labels Sep 23, 2026
…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]>
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Sep 24, 2026
@kwmr

kwmr commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the re-review.

On the shouldShow point, I don't think the guard blocks images: a
NodeSelection spans the node (to = from + node.nodeSize), so
selection.empty is false for a selected image. I checked this in
the real editor (the actual ImageExtension, not the test mock) in a
browser: selecting the image by click or with the arrow keys gives a
NodeSelection from 8 to 9 with empty: false, the bubble menu
appears with the link control, and applying a URL from it sets the
image's link. The existing bubble-menu tests for a selected image
cover the same path and pass, so I've left shouldShow as it is.
Happy to add a test against the real ImageExtension if that would
help.

The aria-label point was right: the tooltip already said "Image
link" but the accessible name stayed "Insert Link". Fixed in 3fe197c,
and the tests that open the link control with an image selected now
find the button by the exact name "Image link", so they'll catch the
two drifting apart.


Generated by Claude Code

# Conflicts:
#	packages/core/src/components/Image.astro
#	packages/core/src/content/converters/portable-text-to-prosemirror.ts
@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Sep 25, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right change for #2497 / Discussion #2741: it adds an optional link object to Portable Text image blocks, surfaces it in the admin toolbar, bubble menu, and image detail panel, normalizes the legacy WordPress string shape on read, and has Image.astro render a sanitized anchor. All three blockers from the previous review are resolved:

  • Legacy link: string values are normalized through the shared normalizeImageLink() helper.
  • ImageDetailPanel resets linkHref/linkBlank via the nodeKey effect when the selected node changes.
  • Image.astro correctly treats #anchor links as same-page jumps and never opens them in a new tab.
  • The inline bubble-menu shouldShow guard now allows image NodeSelections even though their selection.empty is true, so the new image-link bubble-menu code is actually reachable.

I read the converter pair, the Astro renderer, the admin editor/link/bubble-menu code, ImageDetailPanel, ImageNode, and the tests. The implementation is clean and the test coverage (converter round-trip, legacy-string upgrade, half-populated drop, render output, toolbar/bubble-menu/panel regression cases) is solid.

The only remaining functional gap is in packages/core/src/import/utils.ts: relativizeContentLinks() still assumes an image block’s link is a bare string. Now that the canonical shape is { href, blank? }, any content carrying the object shape through that helper will silently skip internal-link relativization, leaving absolute WordPress-domain URLs in image blocks. The sibling rewritePortableTextUrls() already handles both shapes; relativizeContentLinks() should do the same.


Findings

  • [needs fixing] packages/core/src/import/utils.ts:348-350

    relativizeContentLinks() passes block.link straight to relativizeUrl(url: string), which only works when the link is the legacy bare string. The canonical image link shape introduced by this PR is { href, blank? }, so object-linked image blocks are not relativized and keep pointing at the old WordPress domain.

    Mirror the shape handling used by rewritePortableTextUrls() and preserve the blank flag when rewriting an object link:

    		case "image": {
    			// asset.url stays absolute (media pass), only the click-through link
    			let href: string | null = null;
    			if (typeof block.link === "string") {
    				href = block.link;
    			} else if (block.link && typeof block.link === "object" && typeof (block.link as { href?: unknown }).href === "string") {
    				href = (block.link as { href: string }).href;
    			}
    			if (href) {
    				const relativized = relativizeUrl(href, sourceHost);
    				if (relativized) {
    					if (typeof block.link === "string") {
    						block.link = relativized;
    					} else {
    						(block.link as { href: string }).href = relativized;
    					}
    				}
    			}
    			break;
    		}
    

    Please also add an object-link case to packages/core/tests/unit/import/relativize-links.test.ts (e.g. link: { href: "${SITE}/gallery/", blank: true } → link: { href: "/gallery/", blank: true }).

@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review labels Sep 25, 2026
@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Sep 25, 2026
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Sep 25, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of #3167 — this is the right, approved change for #2497 / Discussion #2741. Since the previous review, the remaining functional blocker has been fixed and the two earlier inline nits have been addressed.

I checked:

  • packages/core/src/content/converters/image-link.ts and the two converters — the new normalizeImageLink helper correctly upgrades legacy string links and drops half-populated { blank: true } objects on save.
  • packages/core/src/components/Image.astro — now wraps images in a sanitized <a> when link.href is set, ignores target="_blank" for same-page anchors (#section), and handles the legacy bare-string shape.
  • packages/admin/src/components/PortableTextEditor.tsx — the toolbar and bubble menu detect image selections, pre-populate from the image link, preserve the blank flag when only the URL changes, and the bubble-menu shouldShow guard now correctly lets image NodeSelections through.
  • packages/admin/src/components/editor/ImageDetailPanel.tsx — the link URL input and "Open in new tab" checkbox are reset via the nodeKey effect when the selected node changes.
  • packages/core/src/import/utils.ts — relativizeContentLinks() now handles both the legacy bare-string and the canonical { href, blank? } object shape, preserving blank when rewriting.
  • Tests added/updated for converter round-trips, legacy-string upgrades, half-populated link drops, render output, import relativization, and toolbar/bubble/panel regression cases.

All user-facing strings are Lingui-wrapped, the changeset is clear, and there are no new queries on logged-out routes. No remaining issues found.

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review labels Sep 25, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the current head after merging main and resolving the image media/link converter conflicts. The image-selection controls, legacy link normalization, renderer behavior, WordPress link relativization, and shared Portable Text type are covered by focused regressions. Root build, package typecheck, lint, and targeted admin/core tests pass.

@ascorbic
ascorbic enabled auto-merge (squash) September 25, 2026 08:05
@ascorbic
ascorbic merged commit ed51c68 into emdash-cms:main Sep 25, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Image hyperlink applied in the editor is silently dropped on save (Portable Text image block has no link field)

4 participants