How i can create text node types?

Hi guys,

I am trying to create text node type in this way schema.nodes.text.create(attrs, Fragment.from("some string"), marks), but this throw Error("NodeType.create can't construct text nodes"), what is proper way to create text node types?

Thank you!

2 Likes

Use schema.text("some text" /*, [optional, marks]*/)

4 Likes

Hi, even though this thread is pretty old, I have a follow up question to this.

With a mark, I am wrapping text in a tag. The mark does nothing, expect it replaces the <p> tag with a <variable> tag.

I programmatically create the text node and mark and on the console, it looks good (the node object has the mark in the marks array).

const mark = this.editor.schema.marks.variable.create();
  const textNode = this.editor.schema.text(someString, [mark]);
  this.editor.dispatchTransaction(
    state.tr.replaceSelectionWith(textNode, $from.pos)
  );

After, the text appears in the editor, yet the mark is missing.

When I manually select (any) text and add the variable mark via command/button, it works as expected. The

is swapped to .

Should I expect the marks in the array to rendered correctly when adding them like so?

I have tried with preexisting marks, but the result is the same.

Thanks :slight_smile:

Look at the second argument to replaceSelectionWith. I’m not sure why you’re passing a position there, but that’s not meaningful.

Also, please start a new topic for a new question next time.

1 Like

Thanks, I guess the position came from a lot of experimentation with the code and was left over, while not noticing that it makes no sense.

“Something” evaluated to true, ergo the inherited marks were used, which were no marks at all.

Thanks again and I am sorry to revive this thread. I thought it would be related enough to merit it, but obivously, it isn’t. I’ll behave better the next time, promise. :slight_smile: