Skip to content

Commit c585f88

Browse files
authored
Merge pull request #32 from splitbee/fix-image-urls
Fix image URLs
2 parents aafdb69 + 84fbbde commit c585f88

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/components/asset.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ const Asset: React.FC<{
4040
);
4141
}
4242

43-
const src = mapImageUrl(value.properties.source[0][0]);
44-
45-
if (type === "image") {
43+
if (block.value.type === "image") {
44+
const src = mapImageUrl(value.properties.source[0][0], block);
4645
const caption = value.properties.caption?.[0][0];
4746

4847
if (block_aspect_ratio) {

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,4 @@ export interface LoadPageChunkData {
328328
}
329329

330330
export type MapPageUrl = (pageId: string) => string;
331-
export type MapImageUrl = (image: string) => string;
331+
export type MapImageUrl = (image: string, block?: BlockType) => string;

src/utils.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DecorationType, BlockMapType } from "./types";
1+
import { DecorationType, BlockMapType, MapImageUrl } from "./types";
22

33
export const classNames = (...classes: Array<string | undefined | false>) =>
44
classes.filter(a => !!a).join(" ");
@@ -43,10 +43,22 @@ export const getListNumber = (blockId: string, blockMap: BlockMapType) => {
4343
return group.indexOf(blockId) + 1;
4444
};
4545

46-
export const defaultMapImageUrl = (image: string = "") => {
47-
return `https://www.notion.so${
48-
image.startsWith("/image") ? image : `/image/${encodeURIComponent(image)}`
49-
}`;
46+
export const defaultMapImageUrl: MapImageUrl = (image = "", block) => {
47+
const url = new URL(
48+
`https://www.notion.so${
49+
image.startsWith("/image") ? image : `/image/${encodeURIComponent(image)}`
50+
}`
51+
);
52+
53+
if (block) {
54+
const table =
55+
block.value.parent_table === "space" ? "block" : block.value.parent_table;
56+
url.searchParams.set("table", table);
57+
url.searchParams.set("id", block.value.id);
58+
url.searchParams.set("cache", "v2");
59+
}
60+
61+
return url.toString();
5062
};
5163

5264
export const defaultMapPageUrl = (pageId: string = "") => {

0 commit comments

Comments
 (0)