Skip to content

Commit 4ed212f

Browse files
Merge pull request #627 from LooseLi/feat-list-number
2 parents 040c625 + 4c3ac84 commit 4ed212f

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

packages/react-notion-x/src/block.tsx

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ import { SyncPointerBlock } from './components/sync-pointer-block'
2323
import { Text } from './components/text'
2424
import { useNotionContext } from './context'
2525
import { LinkIcon } from './icons/link-icon'
26-
import { cs, getListNumber, isUrl } from './utils'
26+
import {
27+
cs,
28+
getListNestingLevel,
29+
getListNumber,
30+
getListStyle,
31+
isUrl
32+
} from './utils'
2733

2834
interface BlockProps {
2935
block: types.Block
@@ -433,24 +439,57 @@ export function Block(props: BlockProps) {
433439
<ol
434440
start={start}
435441
className={cs('notion-list', 'notion-list-numbered', blockId)}
442+
style={
443+
block.type === 'numbered_list'
444+
? {
445+
listStyleType: getListStyle(
446+
getListNestingLevel(block.id, recordMap.block)
447+
)
448+
}
449+
: undefined
450+
}
436451
>
437452
{content}
438453
</ol>
439454
)
440455

441456
let output: React.ReactNode | null = null
457+
const isTopLevel =
458+
block.type !== recordMap.block[block.parent_id]?.value?.type
459+
const start = getListNumber(block.id, recordMap.block)
442460

443461
if (block.content) {
444-
output = (
445-
<>
446-
{block.properties && (
447-
<li>
448-
<Text value={block.properties.title} block={block} />
449-
</li>
450-
)}
451-
{wrapList(children)}
452-
</>
453-
)
462+
const listItem = block.properties ? (
463+
<li>
464+
<Text value={block.properties.title} block={block} />
465+
</li>
466+
) : null
467+
468+
if (block.type === 'bulleted_list') {
469+
output = (
470+
<>
471+
{listItem}
472+
<ul className={cs('notion-list', 'notion-list-disc', blockId)}>
473+
{children}
474+
</ul>
475+
</>
476+
)
477+
} else {
478+
const nestingLevel = getListNestingLevel(block.id, recordMap.block)
479+
output = (
480+
<>
481+
{listItem}
482+
<ol
483+
className={cs('notion-list', 'notion-list-numbered', blockId)}
484+
style={{
485+
listStyleType: getListStyle(nestingLevel + 1)
486+
}}
487+
>
488+
{children}
489+
</ol>
490+
</>
491+
)
492+
}
454493
} else {
455494
output = block.properties ? (
456495
<li>
@@ -459,10 +498,6 @@ export function Block(props: BlockProps) {
459498
) : null
460499
}
461500

462-
const isTopLevel =
463-
block.type !== recordMap.block[block.parent_id]?.value?.type
464-
const start = getListNumber(block.id, recordMap.block)
465-
466501
return isTopLevel ? wrapList(output, start) : output
467502
}
468503

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ export const getListNumber = (blockId: string, blockMap: BlockMap) => {
4848
return group.indexOf(blockId) + 1
4949
}
5050

51+
export const getListNestingLevel = (
52+
blockId: string,
53+
blockMap: BlockMap
54+
): number => {
55+
let level = 0
56+
let currentBlockId = blockId
57+
58+
while (true) {
59+
const parentId = blockMap[currentBlockId]?.value?.parent_id
60+
61+
if (!parentId) break
62+
63+
const parentBlock = blockMap[parentId]?.value
64+
if (!parentBlock) break
65+
66+
if (parentBlock.type === 'numbered_list') {
67+
level++
68+
currentBlockId = parentId
69+
} else {
70+
break
71+
}
72+
}
73+
74+
return level
75+
}
76+
77+
export const getListStyle = (level: number): string => {
78+
const styles: string[] = ['decimal', 'lower-alpha', 'lower-roman']
79+
const index = ((level % styles.length) + styles.length) % styles.length
80+
return styles[index] as string
81+
}
82+
5183
export const getHashFragmentValue = (url: string) => {
5284
return url.includes('#') ? url.replace(/^.+(#.+)$/, '$1') : ''
5385
}

0 commit comments

Comments
 (0)