sacha
July 27, 2017, 6:45pm
1
I’ve removed h1
from my schema. Now any text with the h1
tag is converted to the current style when pasted into the editor. This is great.
However, menu Heading > Level 1
is still present in the menu. I expected that, even though I kind of hoped it would automatically be removed.
What I did not expect is that the Heading > Level 1
menu works normally: it can actually insert h1
tag in my document. What am I missing?
marijn
July 28, 2017, 11:13am
2
There is no h1
as such in the basic schema – there’s a heading with a level
attribute. What do you mean when you say you removed h1
?
sacha
July 28, 2017, 12:57pm
3
What do you mean when you say you removed h1?
const { nodes } = require('prosemirror-schema-basic')
nodes.heading = {
attrs: {level: {default: 2}}, // 2 instead of 1
content: "inline<_>*",
group: "block",
defining: true,
parseDOM: [ {tag: "h2", attrs: {level: 2}}, // Removed h1 here
{tag: "h3", attrs: {level: 3}},
{tag: "h4", attrs: {level: 4}},
{tag: "h5", attrs: {level: 5}},
{tag: "h6", attrs: {level: 6}}],
toDOM(node) { return ["h" + node.attrs.level, 0] }
}
sacha
July 28, 2017, 1:08pm
4
The rational for the above is that I want a document with one h1
title and then no more h1
. So my code also contains:
nodes.heading1 = {
content: "inline<_>*",
defining: true,
parseDOM: [{tag: "h1"}],
toDOM(node) { return ["h1", 0] }
}
nodes.doc.content = 'heading1 block*'
marijn
July 28, 2017, 1:09pm
5
That just changes the default level and does nothing to forbid headers with {level: 1}
from occurring. The items that example-setup creates for the menu also aren’t that clever – they won’t appear if there isn’t a heading node type, but they won’t try to automatically figure out the minimum heading level.
marijn
July 28, 2017, 1:22pm
6
Use a different node type for the H1 headings, that makes it much easier to specify schema constraints.
sacha
July 28, 2017, 3:02pm
7
This forbids header with {level:1} because there’s no parseDOM
rule for level 1, right? As I said, it works well when doing copy/past…
sacha
July 30, 2017, 6:39pm
8
For those landing here, the code for removing h1
completely is:
What do you mean when you say you removed h1?
const { nodes } = require('prosemirror-schema-basic')
nodes.heading = {
attrs: {level: {default: 2}}, // 2 instead of 1
content: "inline<_>*",
group: "block",
defining: true,
parseDOM: [ {tag: "h2", attrs: {level: 2}}, // Removed h1 here
{tag: "h3", attrs: {level: 3}},
{tag: "h4", attrs: {level: 4}},
{tag: "h5", attrs: {level: 5}},
{tag: "h6", attrs: {level: 6}}],
toDOM(node) {
if(node.attrs.level < 2 || node.attrs.level > 6)
return // Prevent h1 generation here
return ["h" + node.attrs.level, 0] }
}
With the modified toDOM
function, the menu bar cannot insert h1
anymore.