Skip to content

Commit f2b0478

Browse files
authored
Merge pull request #78 from 01-binary/[email protected]
[email protected]
2 parents cea72a8 + aebfe8f commit f2b0478

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

.changeset/common-places-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"notion-to-utils": patch
3+
---
4+
5+
add image url formatting utility for Notion API image blocks

packages/notion-to-utils/src/utils/formatNotionImageUrl.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const formatNotionImageUrl = (
2121
if (!url || typeof url !== 'string' || !url.startsWith('https://')) {
2222
return url ?? '';
2323
}
24-
2524
try {
2625
// 이미 notion.so 형식인 경우 그대로 반환
2726
if (url.includes('notion.so/image/')) {
@@ -45,7 +44,8 @@ export const formatNotionImageUrl = (
4544

4645
// 블록 ID가 있는 경우 추가
4746
if (blockId) {
48-
params.push(`table=block&id=${blockId}`);
47+
const formattedBlockId = formatNotionId(blockId);
48+
params.push(`table=block&id=${formattedBlockId}`);
4949
}
5050

5151
// 추가 파라미터가 있는 경우 URL에 추가
@@ -64,6 +64,29 @@ export const formatNotionImageUrl = (
6464
}
6565
};
6666

67+
/**
68+
* Notion ID 문자열에 하이픈을 추가하는 함수
69+
*
70+
* 예시 입력: 1239c6bf2b178076a838d17ca1c89783
71+
* 예시 출력: 1239c6bf-2b17-8076-a838-d17ca1c89783
72+
*
73+
* @param id - 하이픈 없는 Notion ID 문자열
74+
* @returns 하이픈이 추가된 UUID 형식의 문자열
75+
*/
76+
const formatNotionId = (id: string): string => {
77+
if (!id || typeof id !== 'string' || id.length !== 32) {
78+
return id; // 유효하지 않은 ID인 경우 원래 ID 반환
79+
}
80+
81+
try {
82+
// 8-4-4-4-12 형식으로 하이픈 추가 (UUID 형식)
83+
return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20)}`;
84+
} catch (error) {
85+
console.error('Notion ID 포맷팅 중 오류 발생:', error);
86+
return id; // 오류 발생 시 원래 ID 반환
87+
}
88+
};
89+
6790
/**
6891
* 노션 블록에서 이미지 URL을 추출하고 포맷팅하는 함수
6992
* ! 현재 미사용 (25.4.7)

0 commit comments

Comments
 (0)