# 创建行内格式 {#mark}

Source: [创建行内格式 | 为 React 编辑器编写自定义扩展 · Yurich](https://leeyujoo.com/yurich/zh-cn/extensions/mark/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

用 `mark()` 给一行中的部分文字添加格式。选中 **Yurich** 后点击 **高亮**，选中的单词会被包在 `<mark>` 中。

```tsx
import { mark, EditorContent, EditorProvider, useEditor, useIsActive } from 'yurich';

const Highlight = mark({ name: 'highlight', tag: 'mark', shortcut: 'Mod+Shift+H' });

export function HighlightEditor() {
  const editor = useEditor({
    extensions: [Highlight],
    initialValue: "<p>给 Yurich 一点强调。</p>",
  });
  const active = useIsActive(editor, 'toggleHighlight');

  return (
    <EditorProvider editor={editor}>
      <button
        type="button"
        aria-pressed={active}
        onMouseDown={event => event.preventDefault()}
        onClick={() => editor.execute('toggleHighlight')}
      >
        高亮
      </button>
      <EditorContent ariaLabel="编辑器" />
    </EditorProvider>
  );
}
```

## 查看结果 {#result}

在 `editor.getHTML()` 返回的 HTML 中，只有 **Yurich** 被 `<mark>` 标签包裹。

```html
<p>给 <mark>Yurich</mark> 一点强调。</p>
```

下面是 `editor.getJSON()` 返回的文档树中 **Yurich** 对应的文本节点。`marks` 数组中的 `highlight` 是通过 `mark()` 注册的格式名称。

```json
{
  "type": "text",
  "text": "Yurich",
  "marks": [{ "type": "highlight" }]
}
```

标签名使用小写字母。`mark()` 支持用 `aliases` 指定等效的输入标签。

[验证扩展](https://leeyujoo.com/yurich/zh-cn/extensions/verify/)
