Skip to content

Fix image URLs #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.8.0",
"version": "0.8.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
5 changes: 2 additions & 3 deletions src/components/asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,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 @@ -322,4 +322,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;
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