docs/(plugins)/(marks)/code.cn.mdx
<code> HTML 元素最快捷的方式是使用 BasicMarksKit 来添加代码插件,该套件包含预配置的 CodePlugin 以及其他基础标记及其 Plate UI 组件。
CodeLeaf: 渲染内联代码元素将套件添加到你的插件中:
import { createPlateEditor } from 'platejs/react';
import { BasicMarksKit } from '@/components/editor/plugins/basic-marks-kit';
const editor = createPlateEditor({
plugins: [
// ...其他插件
...BasicMarksKit,
],
});
npm install @platejs/basic-nodes
在创建编辑器时,将 CodePlugin 包含到 Plate 插件数组中。
import { CodePlugin } from '@platejs/basic-nodes/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...其他插件
CodePlugin,
],
});
你可以为 CodePlugin 配置自定义组件和键盘快捷键。
import { CodePlugin } from '@platejs/basic-nodes/react';
import { createPlateEditor } from 'platejs/react';
import { CodeLeaf } from '@/components/ui/code-node';
const editor = createPlateEditor({
plugins: [
// ...其他插件
CodePlugin.configure({
node: { component: CodeLeaf },
shortcuts: { toggle: { keys: 'mod+e' } },
}),
],
});
你可以在工具栏中添加 MarkToolbarButton 来切换代码格式。
CodePlugin用于内联代码文本格式化的插件。默认渲染为 <code> HTML 元素。
tf.code.toggle切换所选文本的代码格式。