Skip to content

Commit

Permalink
feat: support regex in auto format plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yf-yang committed Aug 13, 2024
1 parent 342ff1e commit 62eb351
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/pink-starfishes-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@udecode/slate-utils': minor
'@udecode/plate-autoformat': minor
---

feat: Add matchByRegex option
3 changes: 2 additions & 1 deletion apps/www/src/lib/plate/demo/plugins/autoformatIndentLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const autoformatIndentLists: AutoformatRule[] = [
toggleIndentList(editor, {
listStyleType: ListStyleType.Decimal,
}),
match: ['1. ', '1) '],
match: ['^\\d+\\.$ ', '^\\d+\\)$ '],
matchByRegex: true,
mode: 'block',
type: 'list',
},
Expand Down
3 changes: 2 additions & 1 deletion apps/www/src/lib/plate/demo/plugins/autoformatLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const autoformatLists: AutoformatRule[] = [
},
{
format: (editor) => formatList(editor, ELEMENT_OL),
match: ['1. ', '1) '],
match: ['^\\d+\\.$ ', '^\\d+\\)$ '],
matchByRegex: true,
mode: 'block',
preFormat,
type: ELEMENT_LI,
Expand Down
8 changes: 7 additions & 1 deletion packages/autoformat/src/transforms/autoformatBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const autoformatBlock = <V extends Value>(
allowSameTypeAbove = false,
format,
match: _match,
matchByRegex = false,
preFormat,
text,
trigger,
Expand Down Expand Up @@ -62,9 +63,14 @@ export const autoformatBlock = <V extends Value>(

const textFromBlockStart = getEditorString(editor, matchRange);

if (end !== textFromBlockStart) continue;
const isMatched = matchByRegex
? !!textFromBlockStart.match(end)
: end === textFromBlockStart;

if (!isMatched) continue;
} else {
matchRange = getRangeBefore(editor, editor.selection as Range, {
matchByRegex,
matchString: end,
});

Expand Down
8 changes: 8 additions & 0 deletions packages/autoformat/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export interface AutoformatBlockRule<
*/
format?: (editor: E) => void;

/**
* If true, `match` will be interpreted as regex expression(s). Otherwise, it
* will be compared by string equality.
*
* @default false
*/
matchByRegex?: boolean;

/**
* Function called just before `format`. Generally used to reset the selected
* block.
Expand Down
17 changes: 16 additions & 1 deletion packages/slate-utils/src/queries/getPointBeforeLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export interface PointBeforeOptions extends BeforeOptions {
beforeString: string;
}) => boolean;

/**
* If true, `matchString` will be interpreted as regex expression(s).
* Otherwise, it will be compared by string equality.
*
* @default false
*/
matchByRegex?: boolean;

/** Lookup before the location for `matchString`. */
matchString?: string | string[];

Expand Down Expand Up @@ -61,6 +69,8 @@ export const getPointBeforeLocation = <V extends Value>(
? castArray(options.matchString)
: [''];

const matchByRegex = options.matchByRegex ?? false;

let point: any;

matchStrings.some((matchString) => {
Expand Down Expand Up @@ -105,8 +115,13 @@ export const getPointBeforeLocation = <V extends Value>(

beforeStringToMatch = map(stack.slice(0, -1), 'text').join('');
}

const isMatched = matchByRegex
? !!matchString.match(beforeStringToMatch)
: beforeStringToMatch === matchString;

if (
matchString === beforeStringToMatch ||
isMatched ||
options.match?.({ at, beforePoint, beforeString: beforeStringToMatch })
) {
if (options.afterMatch) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { AutoformatRule } from '@udecode/plate-autoformat';
import { ListStyleType, toggleIndentList } from '@udecode/plate-indent-list';





export const autoformatIndentLists: AutoformatRule[] = [
{
mode: 'block',
Expand All @@ -15,10 +19,11 @@ export const autoformatIndentLists: AutoformatRule[] = [
{
mode: 'block',
type: 'list',
match: ['1. ', '1) '],
match: ['^\\d+\\.$ ', '^\\d+\\)$ '],
matchByRegex: true,
format: (editor) =>
toggleIndentList(editor, {
listStyleType: ListStyleType.Decimal,
}),
},
];
];
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AutoformatRule } from '@udecode/plate-autoformat';
import { isBlock, setNodes } from '@udecode/plate-common';
import {
ELEMENT_LI,
ELEMENT_OL,
ELEMENT_TODO_LI,
ELEMENT_UL,
TTodoListItemElement,
} from '@udecode/plate-list';
import { ELEMENT_LI, ELEMENT_OL, ELEMENT_TODO_LI, ELEMENT_UL, TTodoListItemElement } from '@udecode/plate-list';



import { formatList, preFormat } from '@/lib/plate/autoformatUtils';





export const autoformatLists: AutoformatRule[] = [
{
mode: 'block',
Expand All @@ -21,7 +21,8 @@ export const autoformatLists: AutoformatRule[] = [
{
mode: 'block',
type: ELEMENT_LI,
match: ['1. ', '1) '],
match: ['^\\d+\\.$ ', '^\\d+\\)$ '],
matchByRegex: true,
preFormat,
format: (editor) => formatList(editor, ELEMENT_OL),
},
Expand All @@ -43,4 +44,4 @@ export const autoformatLists: AutoformatRule[] = [
}
),
},
];
];

0 comments on commit 62eb351

Please sign in to comment.