How to allow h1-h4 but not h5/h6?


Hey, within a document one my have several richtext sections - some which allow h1-h6 and others that may only allow a more limited amount of headings. I guess this cannot be expressed in the schema spec.

But say one wanted to prohibit h5/h6 in a given section while still allowing them in other parts of the document, what would be the places where one would have to intercept them?

I am guessing:

  • paste handling
  • user interface menus that control headings
  • keyboard shortcuts that could turn a textblock into a heading.

Is that all?

Edit: I wonder if there is actually any advantage to having the level stored as a separate attribute to the heading. Maybe it’s easier to just define heading1, heading2, heading3, etc. separately and add them to a heading-group. That should allow me to discriminate between them in the content-description of their container without any workarounds.


You could indeed make each header level its own node type, at which point it becomes easy to express this in the schema. Otherwise, yes, you can’t constraint attributes in the schema so you’ll have to script around that in your interface and maybe an appendTransaction hook to handle things like pasting.


I’m not sure whether you were trying to limiting the “number of headings” in doc or the “types of headings” in doc. I guess that the former one could be done via filtering the transforms and the later one could be done by defining context at NodeSpec^fromSchema

context: ?string

When given, restricts this rule to only match when the current context—
the parent nodes into which the content is being parsed—matches this 
expression. Should contain one or more node names followed by single 
or double slashes. For example  `"paragraph/"`  means the rule only 
matches when the parent node is a paragraph,  `"blockquote/paragraph/"` 
restricts it to be in a paragraph that is inside a blockquote, and 
`"section//"`  matches any position inside a section—a double slash 
matches any sequence of ancestor nodes.


hedgerwang Right, but that only helps us with the paste part, right?

I now went ahead and divided heading up into six different node types. The only disadvantage that I can see is if one has a command to increase/decrease the heading level, then this will be slightly more complicated to implement.