This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
generated from connor-baer/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9925b44
commit 7cfbf73
Showing
2 changed files
with
172 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import { BLOCKS, MARKS, NOTION_BLOCKS, NOTION_MARKS } from './constants'; | ||
import isEmpty from './utils/is-empty'; | ||
import get from './utils/get'; | ||
|
||
const markMap = { | ||
[NOTION_MARKS.BOLD]: MARKS.BOLD, | ||
[NOTION_MARKS.ITALIC]: MARKS.ITALIC, | ||
[NOTION_MARKS.CODE]: MARKS.CODE, | ||
[NOTION_MARKS.HIGHLIGHT]: MARKS.HIGHLIGHT | ||
}; | ||
|
||
export function toMark(mark = []) { | ||
const [typeId, data] = mark; | ||
return { | ||
type: markMap[typeId], | ||
data | ||
}; | ||
} | ||
|
||
export function toText(text = []) { | ||
const [value, marks = []] = text; | ||
return { | ||
nodeType: 'text', | ||
value, | ||
marks: marks.map(toMark) | ||
}; | ||
} | ||
|
||
export function toHeading(level) { | ||
return block => { | ||
const content = get(block, 'value.properties.title', []); | ||
|
||
if (isEmpty(content)) { | ||
return null; | ||
} | ||
|
||
return { | ||
nodeType: `heading-${level}`, | ||
content: content.map(toText) | ||
}; | ||
}; | ||
} | ||
|
||
export function toParagraph(block) { | ||
const content = get(block, 'value.properties.title', []); | ||
|
||
if (isEmpty(content)) { | ||
return null; | ||
} | ||
|
||
return { | ||
nodeType: BLOCKS.PARAGRAPH, | ||
content: content.map(toText) | ||
}; | ||
} | ||
|
||
export function toQuote(block) { | ||
const content = get(block, 'value.properties.title', []); | ||
|
||
if (isEmpty(content)) { | ||
return null; | ||
} | ||
|
||
return { | ||
nodeType: BLOCKS.QUOTE, | ||
content: content.map(toText) | ||
}; | ||
} | ||
|
||
export function toListItem(listType) { | ||
return block => ({ | ||
listType, | ||
nodeType: BLOCKS.LIST_ITEM, | ||
content: [toParagraph(block)] | ||
}); | ||
} | ||
|
||
export function toImage(block) { | ||
const url = get(block, 'value.format.display_source'); | ||
const isHostedByNotion = url.startsWith('/images/'); | ||
const src = isHostedByNotion ? `https://notion.so${url}` : url; | ||
const caption = get(block, 'value.properties.caption[0]', []); | ||
return { | ||
nodeType: BLOCKS.EMBEDDED_ENTRY, | ||
data: { | ||
type: 'image', | ||
src, | ||
alt: caption[0], | ||
caption: toText(caption) | ||
}, | ||
content: [] | ||
}; | ||
} | ||
|
||
export function toHorizontalRule() { | ||
return { | ||
content: [], | ||
nodeType: BLOCKS.HR | ||
}; | ||
} | ||
|
||
const transformerMap = { | ||
[NOTION_BLOCKS.HEADER]: toHeading(2), | ||
[NOTION_BLOCKS.SUB_HEADER]: toHeading(3), | ||
[NOTION_BLOCKS.SUB_SUB_HEADER]: toHeading(4), | ||
[NOTION_BLOCKS.TEXT]: toParagraph, | ||
[NOTION_BLOCKS.QUOTE]: toQuote, | ||
[NOTION_BLOCKS.BULLETED_LIST]: toListItem(BLOCKS.UL_LIST), | ||
[NOTION_BLOCKS.NUMBERED_LIST]: toListItem(BLOCKS.OL_LIST), | ||
[NOTION_BLOCKS.IMAGE]: toImage, | ||
[NOTION_BLOCKS.DIVIDER]: toHorizontalRule | ||
}; | ||
|
||
export default function notionBlocksToRichTextNodes(blocks = {}) { | ||
const nodes = []; | ||
|
||
let tmpListType = null; | ||
let tmpListContent = null; | ||
|
||
/* eslint-disable no-restricted-syntax, no-continue */ | ||
for (const block of Object.values(blocks)) { | ||
const type = get(block, 'value.type'); | ||
const transformerFn = transformerMap[type]; | ||
|
||
if (!transformerFn) { | ||
continue; | ||
} | ||
|
||
const node = transformerFn(block); | ||
|
||
if (!node) { | ||
continue; | ||
} | ||
|
||
const { listType, ...rest } = node; | ||
|
||
const isEndOfList = tmpListContent && tmpListType !== listType; | ||
|
||
if (isEndOfList) { | ||
nodes.push({ | ||
nodeType: tmpListType, | ||
content: tmpListContent | ||
}); | ||
tmpListType = null; | ||
tmpListContent = null; | ||
} | ||
|
||
const isList = !!listType; | ||
|
||
if (isList) { | ||
const isStartOfList = !tmpListType && !tmpListContent; | ||
|
||
if (isStartOfList) { | ||
tmpListType = listType; | ||
tmpListContent = [rest]; | ||
} else { | ||
tmpListContent.push(rest); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
nodes.push(rest); | ||
} | ||
/* eslint-enable no-restricted-syntax, no-continue */ | ||
|
||
return nodes; | ||
} |