Documentation

Define an extension

Use an Extension object when a format needs attributes, multiple commands, or its own active state. This example stores a numeric identifier on a <cite> element.

Select @Ada and click Add mention. The HTML output shows <cite data-id="42">@Ada</cite>.

Live example
Example result
import { EditorContent, EditorProvider, useEditor, type Extension } from 'yurich';

export const Mention: Extension = {
  name: 'mention',
  marks: [{
    tag: 'cite',
    type: 'mention',
    attrs: {
      'data-id': value => value && /^\d+$/.test(value) ? value : null,
    },
  }],
  commands: {
    setMention(editor, value) {
      if (typeof value === 'string' && /^\d+$/.test(value)) {
        editor.setMark('cite', { 'data-id': value });
      }
    },
    removeMention(editor) {
      editor.removeMark('cite');
    },
  },
  isActive: editor => editor.isMarkActive('cite'),
};

export function MentionEditor() {
  const editor = useEditor({
    extensions: [Mention],
    initialValue: "<p>Ask @Ada to try the Bold button.</p>",
  });

  return (
    <EditorProvider editor={editor}>
      <button
        type="button"
        onMouseDown={event => event.preventDefault()}
        onClick={() => editor.execute('setMention', '42')}
      >
        Add mention
      </button>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}

Register Mention in the editor’s extension list. Select text, then call editor.execute('setMention', '42') and check the result with editor.getHTML(). Use useIsActive(editor, 'mention') to reflect the selection in the toolbar.

Verify an extension