Documentation

Code block

Give multiline code its own preformatted block.

Try it

Put the caret in the sample paragraph and click Code block. The result is <pre>editor.execute('toggleBold');</pre>. A plain-text paste inside this block keeps its line breaks.

Live example
Example result

Add it to your editor

Enable CodeBlock and use toggleCodeBlock for standalone, preformatted code. The command creates a <pre> block.

import { EditorContent, EditorProvider, CodeBlock, useEditor, useIsActive } from 'yurich';

export function CodeBlockEditor() {
  const editor = useEditor({
    extensions: [CodeBlock],
    initialValue: "<p>editor.execute('toggleBold');</p>",
  });
  const active = useIsActive(editor, 'toggleCodeBlock');

  return (
    <EditorProvider editor={editor}>
      <button
        type="button"
        aria-pressed={active}
        onMouseDown={event => event.preventDefault()}
        onClick={() => editor.execute('toggleCodeBlock')}
      >
        Code block
      </button>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}

Behavior to account for

Add any code theme or syntax highlighting in the application. For short expressions that belong within a sentence, use inline code.

Features