udecode / plate

A rich-text editor powered by AI
https://platejs.org
Other
12.1k stars 738 forks source link

Autoformat should not take effect within code blocks #3814

Open tomdyqin opened 20 hours ago

tomdyqin commented 20 hours ago

Description

Autoformat should not take effect within code blocks

Reproduction URL

No response

Reproduction steps

1. Go to 'playground'
2. Click on ' '
3. Input '# '
4. See '#' will remove, becase the markdown autoformat is take effect

Plate version

39.2.15

Slate React version

0.110.3

Screenshots

No response

Logs

No response

Browsers

No response

tomdyqin commented 20 hours ago
  // plate/packages/autoformat/src/lib/withAutoformat.ts
  editor.insertText = (text) => {
    if (!isCollapsed(editor.selection)) return insertText(text);

    if (getCodeLineEntry(editor)) return insertText(text); // I'm not sure if adding this processing here is too much

    for (const rule of getOptions().rules!) {
      const { insertTrigger, mode = 'text', query } = rule;

      if (query && !query(editor as any, { ...rule, text })) continue;

      const autoformatter: Record<typeof mode, any> = {
        block: autoformatBlock,
        mark: autoformatMark,
        text: autoformatText,
      };

      if (
        autoformatter[mode]?.(editor, {
          ...(rule as any),
          text,
        })
      ) {
        return insertTrigger && insertText(text);
      }
    }

    insertText(text);
  };