Skip to content

Commit 55de26e

Browse files
chore: refactor map image and map page urls
1 parent 0aab4dc commit 55de26e

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Block } from 'notion-types'
2+
3+
export const defaultMapImageUrl = (
4+
url: string,
5+
block: Block
6+
): string | null => {
7+
if (!url) {
8+
return null
9+
}
10+
11+
if (url.startsWith('data:')) {
12+
return url
13+
}
14+
15+
// more recent versions of notion don't proxy unsplash images
16+
if (url.startsWith('https://images.unsplash.com')) {
17+
return url
18+
}
19+
20+
try {
21+
const u = new URL(url)
22+
23+
if (
24+
u.pathname.startsWith('/secure.notion-static.com') &&
25+
u.hostname.endsWith('.amazonaws.com')
26+
) {
27+
if (
28+
u.searchParams.has('X-Amz-Credential') &&
29+
u.searchParams.has('X-Amz-Signature') &&
30+
u.searchParams.has('X-Amz-Algorithm')
31+
) {
32+
// if the URL is already signed, then use it as-is
33+
return url
34+
}
35+
}
36+
} catch {
37+
// ignore invalid urls
38+
}
39+
40+
if (url.startsWith('/images')) {
41+
url = `https://www.notion.so${url}`
42+
}
43+
44+
url = `https://www.notion.so${
45+
url.startsWith('/image') ? url : `/image/${encodeURIComponent(url)}`
46+
}`
47+
48+
const notionImageUrlV2 = new URL(url)
49+
let table = block.parent_table === 'space' ? 'block' : block.parent_table
50+
if (table === 'collection') {
51+
table = 'block'
52+
}
53+
notionImageUrlV2.searchParams.set('table', table)
54+
notionImageUrlV2.searchParams.set('id', block.id)
55+
notionImageUrlV2.searchParams.set('cache', 'v2')
56+
57+
url = notionImageUrlV2.toString()
58+
59+
return url
60+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const defaultMapPageUrl = (rootPageId?: string) => (pageId: string) => {
2+
pageId = (pageId || '').replace(/-/g, '')
3+
4+
if (rootPageId && pageId === rootPageId) {
5+
return '/'
6+
} else {
7+
return `/${pageId}`
8+
}
9+
}

packages/react-notion-x/src/utils.ts

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,8 @@
1-
import { Block, BlockMap } from 'notion-types'
2-
import { isUrl, formatDate, formatNotionDateTime } from 'notion-utils'
1+
import { BlockMap } from 'notion-types'
32

4-
export { isUrl, formatDate, formatNotionDateTime }
5-
6-
export const defaultMapImageUrl = (url: string, block: Block) => {
7-
if (!url) {
8-
return null
9-
}
10-
11-
if (url.startsWith('data:')) {
12-
return url
13-
}
14-
15-
// more recent versions of notion don't proxy unsplash images
16-
if (url.startsWith('https://images.unsplash.com')) {
17-
return url
18-
}
19-
20-
try {
21-
const u = new URL(url)
22-
23-
if (
24-
u.pathname.startsWith('/secure.notion-static.com') &&
25-
u.hostname.endsWith('.amazonaws.com')
26-
) {
27-
if (
28-
u.searchParams.has('X-Amz-Credential') &&
29-
u.searchParams.has('X-Amz-Signature') &&
30-
u.searchParams.has('X-Amz-Algorithm')
31-
) {
32-
// if the URL is already signed, then use it as-is
33-
return url
34-
}
35-
}
36-
} catch {
37-
// ignore invalid urls
38-
}
39-
40-
if (url.startsWith('/images')) {
41-
url = `https://www.notion.so${url}`
42-
}
43-
44-
url = `https://www.notion.so${
45-
url.startsWith('/image') ? url : `/image/${encodeURIComponent(url)}`
46-
}`
47-
48-
const notionImageUrlV2 = new URL(url)
49-
let table = block.parent_table === 'space' ? 'block' : block.parent_table
50-
if (table === 'collection') {
51-
table = 'block'
52-
}
53-
notionImageUrlV2.searchParams.set('table', table)
54-
notionImageUrlV2.searchParams.set('id', block.id)
55-
notionImageUrlV2.searchParams.set('cache', 'v2')
56-
57-
url = notionImageUrlV2.toString()
58-
59-
return url
60-
}
61-
62-
export const defaultMapPageUrl = (rootPageId?: string) => (pageId: string) => {
63-
pageId = (pageId || '').replace(/-/g, '')
64-
65-
if (rootPageId && pageId === rootPageId) {
66-
return '/'
67-
} else {
68-
return `/${pageId}`
69-
}
70-
}
3+
export { isUrl, formatDate, formatNotionDateTime } from 'notion-utils'
4+
export * from './map-image-url'
5+
export * from './map-page-url'
716

727
export const cs = (...classes: Array<string | undefined | false>) =>
738
classes.filter((a) => !!a).join(' ')

0 commit comments

Comments
 (0)