Back to Plate

Highlight

content/docs/(plugins)/(marks)/highlight.mdx

53.0.84.1 KB
Original Source

Highlight applies the highlight leaf mark to selected text. It is the authoring mark; Find Replace uses a separate search highlight leaf for transient search results.

<ComponentPreview name="basic-marks-demo" /> <PackageInfo>

Features

  • KEYS.highlight leaf mark.
  • Directional selection affinity.
  • HTML deserialization from mark.
  • <mark> rendering by default.
  • Registry HighlightLeaf for styled marked text.
  • Optional input rules through HighlightRules.
</PackageInfo>

Kit Usage

<Steps>

Add Basic Marks

BasicMarksKit includes HighlightPlugin, HighlightLeaf, == and input rules, and a mod+shift+h shortcut.

<ComponentSource name="basic-marks-kit" />
tsx
import { createPlateEditor } from 'platejs/react';

import { BasicMarksKit } from '@/components/editor/plugins/basic-marks-kit';

export const editor = createPlateEditor({
  plugins: [...BasicMarksKit],
});

Add A Toolbar Button

Use MarkToolbarButton with KEYS.highlight.

tsx
import { HighlighterIcon } from 'lucide-react';
import { KEYS } from 'platejs';

import { MarkToolbarButton } from '@/components/ui/mark-toolbar-button';

export function HighlightToolbarButton() {
  return (
    <MarkToolbarButton nodeType={KEYS.highlight} tooltip="Highlight">
      <HighlighterIcon />
    </MarkToolbarButton>
  );
}
</Steps>

Manual Usage

Install the mark package.

bash
npm install @platejs/basic-nodes

Add HighlightPlugin directly when you want the default <mark> render.

tsx
import { HighlightPlugin } from '@platejs/basic-nodes/react';
import { createPlateEditor } from 'platejs/react';

export const editor = createPlateEditor({
  plugins: [HighlightPlugin],
});

Configure the registry leaf, input rules, and shortcut when you want the same behavior as the kit.

tsx
import { HighlightRules } from '@platejs/basic-nodes';
import { HighlightPlugin } from '@platejs/basic-nodes/react';

import { HighlightLeaf } from '@/components/ui/highlight-node';

export const highlightPlugin = HighlightPlugin.configure({
  inputRules: [
    HighlightRules.markdown({ variant: '==' }),
    HighlightRules.markdown({ variant: '≡' }),
  ],
  node: { component: HighlightLeaf },
  shortcuts: { toggle: { keys: 'mod+shift+h' } },
});

Ownership

SurfaceOwnerWhat It Does
BaseHighlightPlugin@platejs/basic-nodesHeadless highlight mark, HTML parser, render tag, selection rule, and toggle transform.
HighlightPlugin@platejs/basic-nodes/reactReact wrapper for the headless highlight mark.
HighlightRules.markdown@platejs/basic-nodesOptional mark input rule factory.
HighlightLeafRegistry UIStyled client highlight leaf using PlateLeaf.
HighlightLeafStaticRegistry UIStatic rendering version using SlateLeaf.
BasicMarksKitRegistryAdds HighlightPlugin, HighlightLeaf, two input-rule variants, and mod+shift+h.
MarkToolbarButtonRegistry UIReads active mark state and calls the mark toggle hook.

The package owns the mark. The registry owns highlight color and toolbar placement.

Behavior

BehaviorSource
Mark keyKEYS.highlight
Leaf behaviornode.isLeaf: true
Toggle transformeditor.tf.highlight.toggle() calls editor.tf.toggleMark(type).
Selection affinitydirectional
HTML tagsmark
Render outputmark
Kit input rules== and variants
Kit shortcutmod+shift+h

API Reference

APIPackageUse
BaseHighlightPlugin@platejs/basic-nodesHeadless highlight plugin.
HighlightPlugin@platejs/basic-nodes/reactReact highlight plugin.
HighlightRules.markdown(options)@platejs/basic-nodesCreates a highlight mark input rule.
tf.highlight.toggle()@platejs/basic-nodesToggles the highlight mark at the selection.
HighlightLeafRegistry UIStyled client highlight leaf.
HighlightLeafStaticRegistry UIStyled static highlight leaf.