Skip to content

Commit 47fe8c1

Browse files
committed
Render content migrated from Volto correctly
- Migrate Volto inline code (`type: "code"`) to the Plate `code` mark (`migrateLegacyCodeInValue`), and register it as a somersault migration. - Treat the Volto elements `b`, `i` and `s` like `strong`, `em` and `del`. - Show the image block's `title` and `description` as a caption. - Add a `belowTitle` slot after the page title of the Plate renderer, through a `renderSlot` prop of `PlateRenderer` that `@plone/layout` fills. Assisted-by: Claude Opus 5.5
1 parent f1c0303 commit 47fe8c1

31 files changed

Lines changed: 728 additions & 29 deletions

‎docs/conceptual-guides/slots.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,40 @@ The rendering of a slot component is controlled by the presence or absence of a
3939
You can register multiple slot components with the same name under the same slot, as long as they have different predicates or components.
4040

4141
See {doc}`../how-to-guides/register-slots` for more information about registering slots.
42+
43+
## Slots in the page body
44+
45+
Most slots frame the page: Header, Content Area, Footer.
46+
The following slot renders inside the page body.
47+
48+
(slots-below-title)=
49+
50+
### `belowTitle`
51+
52+
The `belowTitle` slot renders right after the page title of a page whose content is a Plate document, for example to show a byline.
53+
It renders only in the public view, not in the editor.
54+
Its components receive the `content` and `location` props, as the components of the other slots do.
55+
56+
```tsx
57+
import { ContentTypeCondition } from '@plone/layout/helpers';
58+
import type { SlotComponentProps } from '@plone/layout/slots/SlotRenderer';
59+
60+
const Byline = ({ content }: SlotComponentProps) => (
61+
<p className="byline">{content.creators?.join(', ')}</p>
62+
);
63+
64+
config.registerSlotComponent({
65+
slot: 'belowTitle',
66+
name: 'byline',
67+
component: Byline,
68+
predicates: [ContentTypeCondition(['News Item'])],
69+
});
70+
```
71+
72+
When no component applies, the slot renders nothing.
73+
Use predicates to choose the pages that show a component: a component that returns `null` still leaves the slot's wrapper and its gap.
74+
The slot's wrapper, `.slate-below-title`, aligns its content with the title, and takes the gap below the title.
75+
Set `--block-below-title-spacing-before` to change the gap between the title and the slot.
76+
77+
The slot is part of the Plate document, which `@plone/layout` renders.
78+
`@plone/plate` doesn't depend on `@plone/layout`, so the app passes a `renderSlot` function to `PlateRenderer`, and the title renders `renderSlot('belowTitle')`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Documented the `belowTitle` slot in the slots guide. @sneridagh
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Migrated Volto inline code to the Plate `code` mark when a page converts its Volto blocks, with the `somersaultMigrationLegacyCode` utility. @sneridagh

‎packages/aurora/src/app/config/server/content-migrations.server.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,55 @@ describe('content migrations', () => {
7171
});
7272
});
7373

74+
it('migrates legacy inline code and b elements into marks', () => {
75+
config.blocks = {
76+
blocksConfig: {},
77+
} as typeof config.blocks;
78+
installMigrations();
79+
80+
const content: TestContent = {
81+
'@id': 'http://example.com/',
82+
title: 'Page title',
83+
blocks: {
84+
text: {
85+
'@type': 'slate',
86+
value: [
87+
{
88+
type: 'p',
89+
children: [
90+
{ text: 'Run ' },
91+
{ type: 'code', children: [{ text: 'make install' }] },
92+
{ text: ' with ' },
93+
{ type: 'b', children: [{ text: 'care' }] },
94+
],
95+
},
96+
],
97+
},
98+
},
99+
blocks_layout: {
100+
items: ['text'],
101+
},
102+
};
103+
104+
const migrated = migrateContent(content as unknown as Content);
105+
106+
expect(migrated.blocks?.[MAIN_REGION]).toEqual({
107+
'@type': PLATE_TYPE,
108+
value: [
109+
{
110+
blockWidth: 'default',
111+
type: 'p',
112+
children: [
113+
{ text: 'Run ' },
114+
{ text: 'make install', code: true },
115+
{ text: ' with ' },
116+
{ text: 'care', bold: true },
117+
],
118+
},
119+
],
120+
});
121+
});
122+
74123
it('migrates legacy flat blockquotes into blockquote containers', () => {
75124
config.blocks = {
76125
blocksConfig: {},

‎packages/aurora/src/app/config/server/migrations.server.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
migrateLegacyBlockquotesInValue,
99
migrateLegacyBoldInValue,
1010
migrateLegacyBlockWidthsInValue,
11+
migrateLegacyCodeInValue,
1112
migrateLegacyItalicInValue,
1213
migrateLegacyLinksInValueStatic,
1314
migrateLegacyListsInValue,
@@ -162,6 +163,13 @@ export default function install() {
162163
migrateLegacyStrikethroughInValue(value),
163164
});
164165

166+
config.registerUtility({
167+
name: 'somersaultMigrationLegacyCode',
168+
type: 'somersaultMigration',
169+
method: ({ value }: SomersaultMigrationArgs) =>
170+
migrateLegacyCodeInValue(value),
171+
});
172+
165173
config.registerUtility({
166174
name: 'somersaultMigrationLegacyLinks',
167175
type: 'somersaultMigration',

‎packages/blocks/Image/ImageBlockEdit.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getImageBlockSizes,
99
getImageBlockSrc,
1010
} from './utils';
11+
import ImageCaption from './ImageCaption';
1112

1213
const ImageBlockEdit = (props: BlockEditProps) => {
1314
const { block, data, setBlock, selected } = props;
@@ -61,6 +62,13 @@ const ImageBlockEdit = (props: BlockEditProps) => {
6162
objectBrowserPickerType="image"
6263
/>
6364
) : null}
65+
{data.url ? (
66+
<ImageCaption
67+
as="div"
68+
title={data.title}
69+
description={data.description}
70+
/>
71+
) : null}
6472
</div>
6573
);
6674
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { beforeAll, describe, expect, it, vi } from 'vitest';
3+
import config from '@plone/registry';
4+
import type { BlockViewProps } from '@plone/types';
5+
import ImageBlockView from './ImageBlockView';
6+
import { getImageBlockSizes } from './utils';
7+
8+
// The Image component reads the site's image scales from the router; only the
9+
// alt text matters here.
10+
vi.mock('@plone/layout/components/Image/Image', () => ({
11+
default: ({ alt }: { alt: string }) => <img alt={alt} />,
12+
}));
13+
14+
const makeProps = (data: Record<string, unknown>) =>
15+
({ data }) as unknown as BlockViewProps;
16+
17+
const imageData = {
18+
'@type': 'image',
19+
url: '/images/team-photo',
20+
image_field: 'image',
21+
alt: 'The team',
22+
};
23+
24+
describe('ImageBlockView', () => {
25+
beforeAll(() => {
26+
config.registerComponent({
27+
name: 'Link',
28+
component: ({ children }: { children: React.ReactNode }) => (
29+
<a href="/">{children}</a>
30+
),
31+
});
32+
config.blocks.blocksConfig = {
33+
image: { getSizes: getImageBlockSizes },
34+
} as unknown as typeof config.blocks.blocksConfig;
35+
});
36+
37+
it('renders no caption without a title or description', () => {
38+
const { container } = render(<ImageBlockView {...makeProps(imageData)} />);
39+
40+
expect(container.querySelector('figcaption')).toBeNull();
41+
});
42+
43+
it('renders the title and description as the figure caption', () => {
44+
const { container } = render(
45+
<ImageBlockView
46+
{...makeProps({
47+
...imageData,
48+
title: 'Conference 2025',
49+
description: 'Taken in Bonn.',
50+
})}
51+
/>,
52+
);
53+
54+
const caption = container.querySelector('figure > figcaption');
55+
expect(caption).toHaveClass('block-image__caption');
56+
expect(screen.getByText('Conference 2025')).toHaveClass(
57+
'block-image__caption-title',
58+
);
59+
expect(screen.getByText('Taken in Bonn.')).toHaveClass(
60+
'block-image__caption-description',
61+
);
62+
});
63+
64+
it('renders the description alone, without an empty title', () => {
65+
const { container } = render(
66+
<ImageBlockView
67+
{...makeProps({ ...imageData, title: ' ', description: 'Only this' })}
68+
/>,
69+
);
70+
71+
expect(container.querySelector('.block-image__caption-title')).toBeNull();
72+
expect(screen.getByText('Only this')).toBeInTheDocument();
73+
});
74+
});

‎packages/blocks/Image/ImageBlockView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getImageBlockSizes,
99
getImageBlockSrc,
1010
} from './utils';
11+
import ImageCaption from './ImageCaption';
1112

1213
const ImageBlockView = (props: BlockViewProps) => {
1314
const { data } = props;
@@ -46,6 +47,7 @@ const ImageBlockView = (props: BlockViewProps) => {
4647
) : (
4748
image
4849
)}
50+
<ImageCaption title={data.title} description={data.description} />
4951
</figure>
5052
);
5153
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
type ImageCaptionProps = {
2+
title?: unknown;
3+
description?: unknown;
4+
/**
5+
* The element to render. A `figcaption` belongs inside a `figure`; the edit
6+
* view's frame is a `div`, so it renders a `div` instead.
7+
*/
8+
as?: 'figcaption' | 'div';
9+
};
10+
11+
const asText = (value: unknown) =>
12+
typeof value === 'string' ? value.trim() : '';
13+
14+
/**
15+
* The caption of an image block: its `title`, and its `description` below it,
16+
* as Volto's image block shows them. Renders nothing when both are empty.
17+
*/
18+
const ImageCaption = ({
19+
title,
20+
description,
21+
as: Component = 'figcaption',
22+
}: ImageCaptionProps) => {
23+
const captionTitle = asText(title);
24+
const captionDescription = asText(description);
25+
26+
if (!captionTitle && !captionDescription) return null;
27+
28+
return (
29+
<Component className="block-image__caption">
30+
{captionTitle && (
31+
<span className="block-image__caption-title">{captionTitle}</span>
32+
)}
33+
{captionDescription && (
34+
<span className="block-image__caption-description">
35+
{captionDescription}
36+
</span>
37+
)}
38+
</Component>
39+
);
40+
};
41+
42+
export default ImageCaption;

‎packages/blocks/Image/docs.block.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
block: image
33
---
44

5-
Shows a single image, either an Image content item from the site or an external image, with optional alt text, link, width, alignment and size.
5+
Shows a single image, either an Image content item from the site or an external image, with optional alt text, caption, link, width, alignment and size.
66

77
For an image from the site, set `url` to the path of the Image content item (for example `/my-folder/my-image`), and set `image_field` to `image`. Don't use an `@@images` scale URL.
88

@@ -13,6 +13,8 @@ For an external image, set `url` to the full image URL and leave out `image_fiel
1313
- `url`: path or URL of the image.
1414
- `image_field`: the image field of the content item, normally `image`. Only for images from the site.
1515
- `alt`: alternative text for screen readers. Leave it empty for a purely decorative image.
16+
- `title`: optional caption, shown below the image.
17+
- `description`: optional longer caption text, shown below `title`.
1618
- `href`: optional link target, a list with one item, `[{ "@id": path or URL }]`.
1719
- `openLinkInNewTab`: open the link in a new tab.
1820
- `blockWidth`, `align`, `size`: width, alignment and size on the page.

0 commit comments

Comments
 (0)