Skip to content

Commit

Permalink
Fix generics
Browse files Browse the repository at this point in the history
  • Loading branch information
12joan committed Jan 31, 2023
1 parent 1b14eef commit 5c4cf6f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TDescendant,
TElement,
TText,
Value,
} from '@udecode/plate-core';
import {
ELEMENT_H1,
Expand All @@ -31,7 +32,7 @@ import { remarkTransformElementChildren } from './remarkTransformElementChildren
import { RemarkElementRules } from './types';

// FIXME: underline, subscript superscript not yet supported by remark-slate
export const remarkDefaultElementRules: RemarkElementRules = {
export const remarkDefaultElementRules: RemarkElementRules<Value> = {
heading: {
transform: (node, options) => {
const headingType = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MARK_BOLD, MARK_CODE, MARK_ITALIC } from '@udecode/plate-basic-marks';
import { getPluginType } from '@udecode/plate-core';
import { getPluginType, Value } from '@udecode/plate-core';
import { RemarkTextRules } from './types';

export const remarkDefaultTextRules: RemarkTextRules = {
export const remarkDefaultTextRules: RemarkTextRules<Value> = {
text: {},
emphasis: { mark: ({ editor }) => getPluginType(editor, MARK_ITALIC) },
strong: { mark: ({ editor }) => getPluginType(editor, MARK_BOLD) },
Expand Down
3 changes: 2 additions & 1 deletion packages/serializers/md/src/remark-slate/remarkPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Value } from '@udecode/plate-core';
import { remarkTransformNode } from './remarkTransformNode';
import { MdastNode, RemarkPluginOptions } from './types';

export function remarkPlugin(options: RemarkPluginOptions) {
export function remarkPlugin<V extends Value>(options: RemarkPluginOptions<V>) {
const compiler = (node: { children: Array<MdastNode> }) => {
return node.children.flatMap((child) =>
remarkTransformNode(child, options)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TElement } from '@udecode/plate-core';
import { TElement, Value } from '@udecode/plate-core';
import { remarkDefaultElementRules } from './remarkDefaultElementRules';
import { MdastNode, RemarkPluginOptions } from './types';

export const remarkTransformElement = (
export const remarkTransformElement = <V extends Value>(
node: MdastNode,
options: RemarkPluginOptions
options: RemarkPluginOptions<V>
): TElement | TElement[] => {
const { elementRules = remarkDefaultElementRules } = options;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TDescendant } from '@udecode/plate-core';
import { TDescendant, Value } from '@udecode/plate-core';
import { remarkTransformNode } from './remarkTransformNode';
import { MdastNode, RemarkPluginOptions } from './types';

export const remarkTransformElementChildren = (
export const remarkTransformElementChildren = <V extends Value>(
node: MdastNode,
options: RemarkPluginOptions
options: RemarkPluginOptions<V>
): TDescendant[] => {
const { children } = node;
if (!children) return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { TDescendant } from '@udecode/plate-core';
import { TDescendant, Value } from '@udecode/plate-core';
import { remarkTextTypes } from './remarkTextTypes';
import { remarkTransformElement } from './remarkTransformElement';
import { remarkTransformText } from './remarkTransformText';
import { MdastNode, RemarkPluginOptions } from './types';

export const remarkTransformNode = (
export const remarkTransformNode = <V extends Value>(
node: MdastNode,
options: RemarkPluginOptions
options: RemarkPluginOptions<V>
): TDescendant | TDescendant[] => {
const { type } = node;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TText } from '@udecode/plate-core';
import { TText, Value } from '@udecode/plate-core';
import { remarkDefaultTextRules } from './remarkDefaultTextRules';
import { MdastNode, RemarkPluginOptions } from './types';

export const remarkTransformText = (
export const remarkTransformText = <V extends Value>(
node: MdastNode,
options: RemarkPluginOptions,
options: RemarkPluginOptions<V>,
inheritedMarkProps: { [key: string]: boolean } = {}
): TText | TText[] => {
const { editor, textRules = remarkDefaultTextRules } = options;
Expand Down
26 changes: 13 additions & 13 deletions packages/serializers/md/src/remark-slate/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlateEditor, TElement } from '@udecode/plate-core';
import { PlateEditor, TElement, Value } from '@udecode/plate-core';

export type MdastElementType =
| 'paragraph'
Expand Down Expand Up @@ -38,28 +38,28 @@ export interface MdastNode {
indent?: any;
}

export type RemarkElementRule = {
export type RemarkElementRule<V extends Value> = {
transform: (
node: MdastNode,
options: RemarkPluginOptions
options: RemarkPluginOptions<V>
) => TElement | TElement[];
};

export type RemarkElementRules = {
[key in MdastElementType]?: RemarkElementRule;
export type RemarkElementRules<V extends Value> = {
[key in MdastElementType]?: RemarkElementRule<V>;
};

export type RemarkTextRule = {
mark?: (options: RemarkPluginOptions) => string;
export type RemarkTextRule<V extends Value> = {
mark?: (options: RemarkPluginOptions<V>) => string;
transform?: (text: string) => string;
};

export type RemarkTextRules = {
[key in MdastTextType]?: RemarkTextRule;
export type RemarkTextRules<V extends Value> = {
[key in MdastTextType]?: RemarkTextRule<V>;
};

export type RemarkPluginOptions = {
editor: PlateEditor;
elementRules?: RemarkElementRules;
textRules?: RemarkTextRules;
export type RemarkPluginOptions<V extends Value> = {
editor: PlateEditor<V>;
elementRules?: RemarkElementRules<V>;
textRules?: RemarkTextRules<V>;
};

0 comments on commit 5c4cf6f

Please sign in to comment.