Node.hasMarkup empty attrs for Heading nodes

Upgraded from 0.4.0 to 0.6.1 and noticed that heading levels appear to be missing from attrs. In 0.4.0 hasMarkup returns an object with attrs set and a level defined. In 0.6.1 attrs seems to be an empty object.

In 0.4.0 I used the following code:

var node = pm.doc.path(pm.selection.from.path);
if (node && node.hasMarkup(pm.schema.nodes.heading, {name:"make", level:3})) { ... }

Following changes to path in 0.6.1 I’ve updated this to:

var node = pm.doc.nodeAt(pm.selection.from);
if (node && node.hasMarkup(pm.schema.nodes.heading, {name:"make", level:3})) { ... }

I didn’t notice anything mentioned on the change log about this. Looking at schema_commands.js there is a slight change in the way the attrs is attached but that seems fine. Any ideas?

level is still an attribute of the default Heading type. (name, on the other hand, isn’t). In the demo, if I do pm.doc.child(0).attrs I get {level: "2"}. The return value of hasMarkup is a boolean, so I’m not sure what you mean when you are talking about it returning an object.

Ahh now that is interesting… It looks as if the content of a heading is identified as a text node, is that an intentional change and I missed it in the in the changelog?

A little experimenting…

So working in node coordinates pm.doc.child(0).attrs gives {level:"2"} as you say. Now as I want to determine the node type based on the current cursor position / selection I work in position coordinates so I do pm.doc.nodeAt(0).attrs, all good {level:"2"}, but if I do pm.doc.nodeAt(1).attrs still within the heading text I now get { }, removing the .attrs indicates { type: Text ... rather than {type: Heading ...

In 0.4.0 doing pm.doc.path(pm.selection.from.path).attrs would give me {level: 2}

A minor thing but is there a particular reason why we now store level as a string rather than a Number?

This works… but hmmm really?

I’ve found a workaround but it isn’t pretty…

pm.doc.nodeAt(pm.selection.from-pm.doc.resolve(pm.selection.from).parentOffset-1).attrs

This returns {level: "2"} where ever I click within the heading.

It’s much clearer working with these positions rather than paths but for this use case it feels a bit clunky, is there a neater solution, and how would I check with hasMarkup for a heading?

That’s not a change. Heading’s have been textblocks from the very first release.

You may be looking for pm.doc.resolve(pm.selection.from).parent.attrs.

1 Like

Excellent, thanks