Skip to content

Commit

Permalink
Merge pull request #32 from splitbee/fix-image-urls
Browse files Browse the repository at this point in the history
Fix image URLs
  • Loading branch information
tobiaslins authored Sep 6, 2020
2 parents aafdb69 + 84fbbde commit c585f88
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/components/asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ const Asset: React.FC<{
);
}

const src = mapImageUrl(value.properties.source[0][0]);

if (type === "image") {
if (block.value.type === "image") {
const src = mapImageUrl(value.properties.source[0][0], block);
const caption = value.properties.caption?.[0][0];

if (block_aspect_ratio) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,4 @@ export interface LoadPageChunkData {
}

export type MapPageUrl = (pageId: string) => string;
export type MapImageUrl = (image: string) => string;
export type MapImageUrl = (image: string, block?: BlockType) => string;
22 changes: 17 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DecorationType, BlockMapType } from "./types";
import { DecorationType, BlockMapType, MapImageUrl } from "./types";

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

export const defaultMapImageUrl = (image: string = "") => {
return `https://www.notion.so${
image.startsWith("/image") ? image : `/image/${encodeURIComponent(image)}`
}`;
export const defaultMapImageUrl: MapImageUrl = (image = "", block) => {
const url = new URL(
`https://www.notion.so${
image.startsWith("/image") ? image : `/image/${encodeURIComponent(image)}`
}`
);

if (block) {
const table =
block.value.parent_table === "space" ? "block" : block.value.parent_table;

This comment has been minimized.

Copy link
@dvdsgl

dvdsgl Sep 7, 2020

Contributor

This seems to not get applied to page covers correctly.

url.searchParams.set("table", table);
url.searchParams.set("id", block.value.id);
url.searchParams.set("cache", "v2");
}

return url.toString();
};

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

0 comments on commit c585f88

Please sign in to comment.