Adding attributes to a link

I currently have this code for applying a link to selected text:

export const addMarkWrapper = (
  state: EditorState,
  markType: MarkType<Schema<string, string>>,
  attrs: { href: string; id: string; type: string },
): Transaction<Schema<string, string>> => {
  const { selection } = state;
  const [{ $from, $to }] = selection.ranges;
  return$from.pos, $to.pos, markType.create(attrs));

However, id and type are not showing as attributes in the JSON output. Is there a better way to add attribute fields?

Hi, the code itself looks fine to me.

Could you elaborate where it is not showing up? The __addMarkWrapper __ command returns a transaction. Are you applying this transaction to the state?

(also see commands)

p.s.: Is there a reason you are not using selection.from and It should both be fine for a basic test but seems lengthy. If you have multiple ranges, you would have to apply the range to all of them, not just the first one.

Did you define those as attributes for the link mark? If you’re using the basic schema then link only has href and title as valid attribs. You would need to define your own link mark with the additional allowed attributes.