Skip to content

[email protected] #78

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
Apr 17, 2025
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
5 changes: 5 additions & 0 deletions .changeset/common-places-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"notion-to-utils": patch
---

add image url formatting utility for Notion API image blocks
27 changes: 25 additions & 2 deletions packages/notion-to-utils/src/utils/formatNotionImageUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const formatNotionImageUrl = (
if (!url || typeof url !== 'string' || !url.startsWith('https://')) {
return url ?? '';
}

try {
// 이미 notion.so 형식인 경우 그대로 반환
if (url.includes('notion.so/image/')) {
Expand All @@ -45,7 +44,8 @@ export const formatNotionImageUrl = (

// 블록 ID가 있는 경우 추가
if (blockId) {
params.push(`table=block&id=${blockId}`);
const formattedBlockId = formatNotionId(blockId);
params.push(`table=block&id=${formattedBlockId}`);
}

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

/**
* Notion ID 문자열에 하이픈을 추가하는 함수
*
* 예시 입력: 1239c6bf2b178076a838d17ca1c89783
* 예시 출력: 1239c6bf-2b17-8076-a838-d17ca1c89783
*
* @param id - 하이픈 없는 Notion ID 문자열
* @returns 하이픈이 추가된 UUID 형식의 문자열
*/
const formatNotionId = (id: string): string => {
if (!id || typeof id !== 'string' || id.length !== 32) {
return id; // 유효하지 않은 ID인 경우 원래 ID 반환
}

try {
// 8-4-4-4-12 형식으로 하이픈 추가 (UUID 형식)
return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20)}`;
} catch (error) {
console.error('Notion ID 포맷팅 중 오류 발생:', error);
return id; // 오류 발생 시 원래 ID 반환
}
};

/**
* 노션 블록에서 이미지 URL을 추출하고 포맷팅하는 함수
* ! 현재 미사용 (25.4.7)
Expand Down