Skip to content
Draft
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
15 changes: 7 additions & 8 deletions src/nestLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ export function nestLists<T extends TypedObject = PortableTextBlock | TypedObjec
if ((block.level || 1) > currentList.level) {
const newList = listFromBlock(block, i, mode)

if (mode === 'html') {
if (currentList.mode === 'html' && newList.mode === 'html') {
// Because HTML is kinda weird, nested lists needs to be nested within list items.
// So while you would think that we could populate the parent list with a new sub-list,
// we actually have to target the last list element (child) of the parent.
// However, at this point we need to be very careful - simply pushing to the list of children
// will mutate the input, and we don't want to blindly clone the entire tree.

// Clone the last child while adding our new list as the last child of it
const lastListItem = currentList.children[
currentList.children.length - 1
] as ToolkitPortableTextListItem
const lastListItem = currentList.children[currentList.children.length - 1]
if (!lastListItem) {
continue
}

const newLastChild: ToolkitPortableTextListItem = {
...lastListItem,
Expand All @@ -108,10 +109,8 @@ export function nestLists<T extends TypedObject = PortableTextBlock | TypedObjec

// Swap the last child
currentList.children[currentList.children.length - 1] = newLastChild
} else {
;(currentList as ToolkitPortableTextDirectList).children.push(
newList as ToolkitPortableTextDirectList,
)
} else if (currentList.mode === 'direct' && newList.mode === 'direct') {
currentList.children.push(newList)
}

// Set the newly created, deeper list as the current
Expand Down