Extending the schema of a pre-defined doc

Hi! Currently, when I create an EditorView’s state, I use the ‘doc’ attribute, which automatically creates a schema. I was wondering how to extend that schema to add a mark? I tried ‘setProps’ after initially defining the view, and created a new state using the previous ‘doc’, as well as a new Schema object where I added to spec.marks using the 'doc’s schema.

Here’s the state for the initial view. It parses pre-existing markdown to generate a Node.

    state: EditorState.create({
        doc: (Markdown as any).defaultMarkdownParser.parse(
            typeof source === "string" ? source : source.join('')

Here’s when I try to setProps to change the state to include a new mark (i.e. underline).

        state: EditorState.create({
            schema: new Schema({
                nodes: state.schema.spec.nodes,
                marks: state.schema.spec.marks.addBefore("strong", "underline", {
                    parseDOM: [{tag: "u"}],
                    toDOM() { return ["u", 0]}
            doc: state.doc,
            plugins: [keymap(baseKeymap)]

This, however, created weird functionality with the editor. I was wondering what is the proper way to do this?

It doesn’t really create a schema, it just uses the schema that was used to create that document.

So if you want to extend the schema, you’ll have to do that with the original schema, before you create the document. To use that schema with the markdown parser, you’ll have to create a new instance of the MarkdownParser class, which usually involves adding extra parse rules to defaultMarkdownParser.tokens.

1 Like

Ah, thank you for that clarification. I’ll look into extending via the markdown parser. So just to be clear, when the library guide says

When initializing a state, you can give it an initial document to use. In that case, the schema field is optional, since the schema can be taken from the document.

you really shouldn’t be using the schema field in conjunction with the doc field? Or are there cases where that’s possible and recommended? Thank you.

It’s redundant—you can get the schema from the document, so there’s no need to pass both.