Skip to content

Commit 3ecb16d

Browse files
committed
feat: ListItem 추가, 로직 refactoring
1 parent fdc85f5 commit 3ecb16d

File tree

4 files changed

+46
-73
lines changed

4 files changed

+46
-73
lines changed

packages/notion-to-jsx/src/components/Renderer/components/Block/BlockRenderer.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '../MemoizedComponents';
88
import { CodeBlock } from '../Code';
99
import { Heading1, Heading2, Heading3, Paragraph } from '../Typography';
10+
import { ListItem } from '../List';
1011

1112
export interface Props {
1213
block: any;
@@ -54,16 +55,16 @@ const BlockRenderer: React.FC<Props> = ({ block, onFocus, index }) => {
5455

5556
case 'bulleted_list_item':
5657
return (
57-
<div {...blockProps}>
58+
<ListItem {...blockProps}>
5859
<MemoizedRichText richTexts={block.bulleted_list_item.rich_text} />
59-
</div>
60+
</ListItem>
6061
);
6162

6263
case 'numbered_list_item':
6364
return (
64-
<div {...blockProps}>
65+
<ListItem {...blockProps}>
6566
<MemoizedRichText richTexts={block.numbered_list_item.rich_text} />
66-
</div>
67+
</ListItem>
6768
);
6869

6970
case 'code':

packages/notion-to-jsx/src/components/Renderer/components/List/ListBlocksRenderer.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ const ListBlocksRenderer: React.FC<Props> = ({ blocks, startIndex, type }) => {
1818
break;
1919
}
2020
}
21-
2221
return (
23-
<List as={type === 'numbered' ? 'ol' : 'ul'} type={type}>
22+
<List
23+
as={type === 'numbered' ? 'ol' : 'ul'}
24+
type={type}
25+
role="list"
26+
aria-label={type === 'bulleted' ? 'Bulleted list' : 'Numbered list'}
27+
>
2428
{blocks
2529
.slice(startIndex, startIndex + consecutiveItems)
2630
.map((block, index) => (

packages/notion-to-jsx/src/components/Renderer/components/List/styles.css.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { vars } from '../../../../styles/theme.css';
44

55
export const list = recipe({
66
base: {
7-
margin: `${vars.spacing.sm} 0`,
8-
paddingLeft: vars.spacing.xl,
7+
paddingLeft: vars.spacing.lg,
98
},
109
variants: {
1110
type: {
@@ -19,6 +18,4 @@ export const list = recipe({
1918
},
2019
});
2120

22-
export const listItem = style({
23-
margin: `${vars.spacing.xs} 0`,
24-
});
21+
export const listItem = style({});

packages/notion-to-jsx/src/components/Renderer/index.tsx

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ const notion = {
1616
},
1717
};
1818

19-
// const GlobalStyle = createGlobalStyle<{ theme: Theme }>`
20-
// body {
21-
// background-color: ${({ theme }) => theme.colors.background};
22-
// color: ${({ theme }) => theme.colors.text};
23-
// font-family: ${({ theme }) => theme.typography.fontFamily.base};
24-
// line-height: ${({ theme }) => theme.typography.lineHeight.base};
25-
// margin: 0;
26-
// padding: 0;
27-
// }
28-
// `;
29-
3019
interface Props {
3120
isDarkMode?: boolean;
3221
onBlockFocus?: (index: number) => void;
@@ -37,7 +26,7 @@ export const Renderer: React.FC<Props> = React.memo(
3726
const theme = isDarkMode ? darkTheme : lightTheme;
3827
const [focusedIndex, setFocusedIndex] = useState<number>(-1);
3928

40-
// For test
29+
// TODO: For test
4130
useEffect(() => {
4231
const fetchBlocks = async () => {
4332
const result = await notion.getPageBlocks();
@@ -62,62 +51,44 @@ export const Renderer: React.FC<Props> = React.memo(
6251
const block = blocks[i];
6352
if (!block) break;
6453

65-
if (
66-
block.type === 'bulleted_list_item' &&
67-
(i === 0 || blocks[i - 1]?.type !== 'bulleted_list_item')
68-
) {
69-
result.push(
70-
<List
71-
as="ul"
72-
type="bulleted"
73-
role="list"
74-
aria-label="Bulleted list"
75-
key={block.id}
76-
>
77-
<ListBlocksRenderer
78-
blocks={blocks}
79-
startIndex={i}
80-
type="bulleted"
81-
/>
82-
</List>
83-
);
84-
while (
85-
i + 1 < blocks.length &&
86-
blocks[i + 1] &&
87-
blocks[i + 1]?.type === 'bulleted_list_item'
54+
// 리스트 아이템 타입 처리를 위한 공통 함수
55+
const handleListItem = (listType: 'bulleted' | 'numbered') => {
56+
const listItemType = `${listType}_list_item`;
57+
58+
if (
59+
block.type === listItemType &&
60+
(i === 0 || blocks[i - 1]?.type !== listItemType)
8861
) {
89-
i++;
90-
}
91-
} else if (
92-
block.type === 'numbered_list_item' &&
93-
(i === 0 || blocks[i - 1]?.type !== 'numbered_list_item')
94-
) {
95-
result.push(
96-
<List
97-
as="ol"
98-
type="numbered"
99-
role="list"
100-
aria-label="Numbered list"
101-
key={block.id}
102-
>
62+
result.push(
10363
<ListBlocksRenderer
64+
key={block.id}
10465
blocks={blocks}
10566
startIndex={i}
106-
type="numbered"
67+
type={listType}
10768
/>
108-
</List>
109-
);
110-
while (
111-
i + 1 < blocks.length &&
112-
blocks[i + 1] &&
113-
blocks[i + 1]?.type === 'numbered_list_item'
114-
) {
115-
i++;
69+
);
70+
71+
// 연속된 같은 타입의 리스트 아이템 건너뛰기
72+
while (
73+
i + 1 < blocks.length &&
74+
blocks[i + 1] &&
75+
blocks[i + 1]?.type === listItemType
76+
) {
77+
i++;
78+
}
79+
80+
return true;
11681
}
117-
} else if (
118-
block.type !== 'bulleted_list_item' &&
119-
block.type !== 'numbered_list_item'
120-
) {
82+
83+
return false;
84+
};
85+
86+
// 순서대로 각 리스트 타입 처리 시도
87+
if (handleListItem('bulleted') || handleListItem('numbered')) {
88+
// 리스트 아이템이 처리되었으므로 다음 블록으로 진행
89+
continue;
90+
} else {
91+
// 리스트 아이템이 아닌 일반 블록 처리
12192
result.push(
12293
<BlockRenderer
12394
key={block.id}

0 commit comments

Comments
 (0)