Skip to content

[Bug] custom shcema error:Cannot create node for paragraph #1995

Description

@dingshaohua-com

card.ts

import { $node, $remark } from '@milkdown/kit/utils';
import { Editor } from '@milkdown/kit/core';
import { jsonToHtmlAttr } from '@dingshaohua.com/utils';

const cardRemark = $remark('remarkDirective1', function () {
  return function () {
    const that = this;
    return function (tree: any) {
      const transformer = (node: any) => {
        if (!node.children) return;
        const firstChild = node.children.at(0);
        const isCardStart = firstChild.type === 'html' && /^<card/.test(firstChild.value.trim());
        if (isCardStart) {
          const htmlNode = jsonToHtmlAttr(firstChild.value);
          const tree: any = that.parse(htmlNode.node.innerHTML.trim());
          node.children.splice(0, node.children.length, {
            type: 'card-node',
            children: tree.children,
            attrs: htmlNode.attrs,
          });
        }
        node.children.forEach(transformer);
      };
      transformer(tree);
    }
  };
});


const cardSchema = $node('card-node', (ctx) => {
  return {
    group: 'block',
    inline: false,
    atom: false, 
    content: 'block*',
    attrs: {
      title: '',
    },

    toDOM: (node) => {
      return [
        'div',
        {
          class: 'card'
        },
        [
          'div',
          {
            class: 'card-title'
          },
          node.attrs.title,
        ],
        [
          'div',
          {
            class: 'card-content'
          },
          0, 
        ],
      ];
    },

    parseMarkdown: {
      match: (node) => node.type === 'card-node',
      runner: (state, node, type) => {
        state.openNode(type, node.attrs);
        if (node.children) {
          state.next(node.children);
        } else if (node.value) {
          state.addText(node.value as string);
        }
        state.closeNode();
      },
    },
  };
});

export default (editor: Editor) => {
  editor.use(cardSchema).use(cardRemark);
};

markdown content

<card title="这是标题">
**这是**另一个测试卡片
哈哈
## 这是标题
嘻嘻
</card>

get error

my-editor.tsx:122 MilkdownError: [Description]: "Cannot create node for paragraph".
[Attributes]: null.
[Content]: ["card-node(paragraph(strong(\"这是\"), \"另一个测试卡片\\n哈哈\"), heading(\"这是标题\"), paragraph(\"嘻嘻\"))"].

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions