MCPcopy
hub / github.com/NotionX/react-notion-x / renderBlock

Function renderBlock

packages/notion-x-to-md/src/block.ts:24–377  ·  view source on GitHub ↗
(
  blockId: string,
  recordMap: ExtendedRecordMap,
  level = 0
)

Source from the content-addressed store, hash-verified

22import { decorationsToMarkdown } from './text'
23
24export async function renderBlock(
25 blockId: string,
26 recordMap: ExtendedRecordMap,
27 level = 0
28): Promise<string> {
29 const rawBlock = recordMap.block[blockId]
30 if (!rawBlock) return ''
31
32 const block = getBlockValue(rawBlock)
33 if (!block) return ''
34
35 const { type } = block
36
37 switch (type) {
38 case 'page':
39 if (level === 0) {
40 const title = getTitle(block, recordMap)
41 const children = await renderBlockChildren(block, recordMap, level + 1)
42 return `# ${title}\n\n${children}\n`
43 } else {
44 const title = [
45 getIcon(block, recordMap),
46 getBlockTitle(block, recordMap)
47 ]
48 .filter(Boolean)
49 .join(' ')
50 return `[${title}](/${uuidToId(blockId)})\n`
51 }
52
53 case 'header':
54 return `# ${getTitle(block, recordMap)}\n`
55
56 case 'sub_header':
57 return `## ${getTitle(block, recordMap)}\n`
58
59 case 'sub_sub_header':
60 return `### ${getTitle(block, recordMap)}\n`
61
62 case 'header_4':
63 return `#### ${getTitle(block, recordMap)}\n`
64
65 case 'text': {
66 const content = getTitle(block, recordMap)
67 const children = await renderBlockChildren(block, recordMap, level + 1)
68 if (!content && !children) return '\n'
69 if (children) {
70 return `${content}\n${prefixLines(children, ' ')}`
71 }
72 return `${content}\n`
73 }
74
75 case 'bulleted_list': {
76 const listNestingLevel = getListNestingLevel(block.id, recordMap.block)
77 const indent = ' '.repeat(listNestingLevel)
78 const content = `${indent}- ${getTitle(block, recordMap)}`
79 if (block.content?.length) {
80 const children = await renderBlockChildren(block, recordMap, level + 1)
81 return `${content}\n${children}`

Callers 2

notionPageToMarkdownFunction · 0.90
renderBlockChildrenFunction · 0.85

Calls 15

getBlockValueFunction · 0.90
getIconFunction · 0.90
getBlockTitleFunction · 0.90
uuidToIdFunction · 0.90
getListNestingLevelFunction · 0.90
getListNumberFunction · 0.90
getTextContentFunction · 0.90
defaultMapImageUrlFunction · 0.90
decorationsToMarkdownFunction · 0.90
getBlockCollectionIdFunction · 0.90
renderCollectionPropertyFunction · 0.90
getTitleFunction · 0.85

Tested by

no test coverage detected