I’m looking on adding IDs to heading for easier linking (GitHub style). I thought this would be easy by just modifying toDOM
method for the header schema to calculate the value from text content:
heading: {
attrs: {
level: {
default: 1,
},
},
content: "inline*",
group: "block",
defining: true,
draggable: false,
parseDOM: HEADING_LEVELS.map(level => ({
tag: `h${level}`,
attrs: { level },
})),
toDOM: node => {
const id = `h${node.attrs.level}-${slugify(node.textContent)}`;
return [`h${node.attrs.level}`, { id }, 0];
},
},
While the id
calculation works inside function, the value inserted into id
is always h2-
and lacks the dynamic part. I think I’m missing something obvious on how HTML serialization works but unsure what it is.