Skip to content

Commit a009360

Browse files
committed
MapPictureUrl
1 parent 1cdb6dd commit a009360

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

src/block.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
BlockMapType,
77
MapPageUrl,
88
MapImageUrl,
9+
MapPictureUrl,
910
CustomBlockComponents,
1011
BlockValueProp,
1112
CustomDecoratorComponents,
@@ -89,6 +90,7 @@ interface Block {
8990
blockMap: BlockMapType;
9091
mapPageUrl: MapPageUrl;
9192
mapImageUrl: MapImageUrl;
93+
mapPictureUrl?: MapPictureUrl;
9294

9395
fullPage?: boolean;
9496
hideHeader?: boolean;
@@ -106,6 +108,7 @@ export const Block: React.FC<Block> = props => {
106108
blockMap,
107109
mapPageUrl,
108110
mapImageUrl,
111+
mapPictureUrl,
109112
customBlockComponents,
110113
customDecoratorComponents
111114
} = props;
@@ -308,7 +311,7 @@ export const Block: React.FC<Block> = props => {
308311
<figure
309312
className="notion-asset-wrapper"
310313
>
311-
<Asset block={block} mapImageUrl={mapImageUrl} />
314+
<Asset block={block} mapImageUrl={mapImageUrl} mapPictureUrl={mapPictureUrl} />
312315

313316
{value.properties.caption && (
314317
<figcaption className="notion-image-caption">

src/components/asset.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as React from "react";
2-
import { ThumbnailValueType, BlockType, ContentValueType, MapImageUrl } from "../types";
2+
import { ThumbnailValueType, BlockType, ContentValueType, MapImageUrl, MapPictureUrl } from "../types";
33

44
const types = ["video", "image", "embed", "figma"];
55
const reponsiveWidth = [213, 320, 640, 768, 1024, 1366, 1600, 1920, 2560];
66

77
const Asset: React.FC<{
88
block: BlockType;
99
mapImageUrl: MapImageUrl;
10-
}> = ({ block, mapImageUrl }) => {
10+
mapPictureUrl?: MapPictureUrl;
11+
}> = ({ block, mapImageUrl, mapPictureUrl }) => {
1112
const value = block.value as ContentValueType;
1213
const type = block.value.type;
1314

@@ -43,16 +44,45 @@ const Asset: React.FC<{
4344
}
4445

4546
if (block.value.type === "image") {
46-
const src = mapImageUrl(value.properties.source[0][0], block);
4747
const caption = value.properties.caption?.[0][0];
48+
const title = value.properties.title?.[0][0];
4849
const style: any = {};
4950

50-
let thumbnails: ThumbnailValueType[] = [];
51-
5251
if (block_aspect_ratio) {
5352
style["aspectRatio"] = `${1 / aspectRatio}`;
5453
}
5554

55+
if (mapPictureUrl) {
56+
const [coverUrl, coverSrcSets, coverStyles] = mapPictureUrl(value.properties.source[0][0], block);
57+
58+
if (coverUrl) {
59+
Object.assign(style, coverStyles);
60+
61+
return (
62+
<picture>
63+
{coverSrcSets.map((src: any) => (
64+
<source
65+
key={src.type}
66+
srcSet={src.srcSet}
67+
type={src.type}
68+
sizes="(min-width: 1280px) 40vw, (min-width: 1024px) 60vw, (min-width: 768px) 80vw, 100vw"
69+
/>
70+
))}
71+
<img
72+
src={coverUrl}
73+
loading="lazy"
74+
decoding="async"
75+
alt={caption || title}
76+
style={style}
77+
/>
78+
</picture>
79+
);
80+
}
81+
}
82+
const src = mapImageUrl(value.properties.source[0][0], block);
83+
84+
let thumbnails: ThumbnailValueType[] = [];
85+
5686
const blockWidth = value.format?.block_width;
5787

5888
reponsiveWidth.forEach(width => {
@@ -84,7 +114,7 @@ const Asset: React.FC<{
84114
/>
85115
<img
86116
src={thumbnails[0].src}
87-
alt={caption}
117+
alt={caption || title}
88118
loading="lazy"
89119
decoding="async"
90120
style={style}

src/renderer.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
BlockMapType,
44
MapPageUrl,
55
MapImageUrl,
6+
MapPictureUrl,
67
CustomBlockComponents,
78
CustomDecoratorComponents
89
} from "./types";
@@ -15,6 +16,7 @@ export interface NotionRendererProps {
1516
hideHeader?: boolean;
1617
mapPageUrl?: MapPageUrl;
1718
mapImageUrl?: MapImageUrl;
19+
mapPictureUrl?: MapPictureUrl;
1820

1921
currentId?: string;
2022
level?: number;
@@ -27,6 +29,7 @@ export const NotionRenderer: React.FC<NotionRendererProps> = ({
2729
currentId,
2830
mapPageUrl = defaultMapPageUrl,
2931
mapImageUrl = defaultMapImageUrl,
32+
mapPictureUrl,
3033
...props
3134
}) => {
3235
const { blockMap } = props;
@@ -47,6 +50,7 @@ export const NotionRenderer: React.FC<NotionRendererProps> = ({
4750
block={currentBlock}
4851
mapPageUrl={mapPageUrl}
4952
mapImageUrl={mapImageUrl}
53+
mapPictureUrl={mapPictureUrl}
5054
{...props}
5155
>
5256
{currentBlock?.value?.content?.map(contentId => (
@@ -56,6 +60,7 @@ export const NotionRenderer: React.FC<NotionRendererProps> = ({
5660
level={level + 1}
5761
mapPageUrl={mapPageUrl}
5862
mapImageUrl={mapImageUrl}
63+
mapPictureUrl={mapPictureUrl}
5964
{...props}
6065
/>
6166
))}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export interface ContentValueType extends BaseValueType {
203203
properties: {
204204
source: string[][];
205205
caption?: DecorationType[];
206+
title?: DecorationType[];
206207
};
207208
format?: {
208209
block_width: number;
@@ -347,6 +348,7 @@ export interface LoadPageChunkData {
347348

348349
export type MapPageUrl = (pageId: string) => string;
349350
export type MapImageUrl = (image: string, block?: BlockType) => string;
351+
export type MapPictureUrl = (image: string, block?: BlockType) => any;
350352

351353
export type BlockValueProp<T> = Extract<BlockValueType, { type: T }>;
352354

0 commit comments

Comments
 (0)