Do parseDOM and toDOM have to match?


#1

I would like to use only h2-h4 headings in the content, while h1 should be reserved for content outside the editor.

I have pretty standard schema defined:

heading: {
  attrs: {
    level: {default: 1},
  },
  content: 'inline*',
  group: 'block',
  defining: true,
  parseDOM: [
    {tag: 'h2', attrs: {level: 1}},
    {tag: 'h3', attrs: {level: 2}},
    {tag: 'h4', attrs: {level: 3}},
  ],
  toDOM(node) {
    return [`h${node.attrs.level + 1}`, 0];
  },
},

The issue is what happens if somebody copies content from web or Word into it, which does use h1-h3. In that case I would like that they would be mapped as well. So I was thinking of maybe doing the following:

heading: {
  attrs: {
    level: {default: 1},
  },
  content: 'inline*',
  group: 'block',
  defining: true,
  parseDOM: [
    {tag: 'h1', attrs: {level: 1}},
    {tag: 'h2', attrs: {level: 2}},
    {tag: 'h3', attrs: {level: 3}},
  ],
  toDOM(node) {
    return [`h${node.attrs.level + 1}`, 0];
  },
},

Would that work? Or do parseDOM and toDOM have to match?


#2

They should, yes. If you copy-paste, for example, you’ll see your headings change level if you use that spec.