Skip to content

Commit

Permalink
fix: deeply nested lists don't throw an error
Browse files Browse the repository at this point in the history
Fixes #583
  • Loading branch information
petyosi committed Sep 14, 2024
1 parent b9ad456 commit ac63059
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/examples/nested-lists.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react'
import { MDXEditor, listsPlugin, diffSourcePlugin, toolbarPlugin, DiffSourceToggleWrapper, UndoRedo } from '..'

const listsMarkdown = `
* hello
* world
* indented
* more
* back
`

export function NestedLists() {
return (
<MDXEditor
markdown={listsMarkdown}
onChange={(md) => {
console.log(md)
}}
plugins={[
listsPlugin(),
diffSourcePlugin(),
toolbarPlugin({
toolbarContents: () => (
<DiffSourceToggleWrapper>
<UndoRedo />
</DiffSourceToggleWrapper>
)
})
]}
/>
)
}
11 changes: 9 additions & 2 deletions src/plugins/lists/LexicalListItemVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ export const LexicalListItemVisitor: LexicalExportVisitor<ListItemNode, Mdast.Li

if (children.length === 1 && $isListNode(firstChild)) {
// append the list after the paragraph of the previous list item
const prevListItemNode = mdastParent.children.at(-1) as Mdast.ListItem
actions.visitChildren(lexicalNode, prevListItemNode)
const prevListItemNode = mdastParent.children.at(-1) as Mdast.ListItem | undefined
// XXX: this is a hack to avoid errors with deeply nested lists - the approach will flatten them.
// Deeply nested lists are still not supported, but at least no error will be thrown.
if (!prevListItemNode) {
// @ts-expect-error - MDAST type is incorrect
actions.visitChildren(firstChild, mdastParent)
} else {
actions.visitChildren(lexicalNode, prevListItemNode)
}
} else {
const parentList = lexicalNode.getParent()! as ListNode
// nest the children in a paragraph for MDAST compatibility
Expand Down

0 comments on commit ac63059

Please sign in to comment.