Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide markdown plugin node dependencies #2910

Merged
merged 7 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/lexical-markdown/flow/LexicalMarkdown.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Transformer =
| TextMatchTransformer;

export type ElementTransformer = {
dependencies: Array<Class<LexicalNode>>,
export: (
node: LexicalNode,
traverseChildren: (node: ElementNode) => string,
Expand All @@ -43,6 +44,7 @@ export type TextFormatTransformer = $ReadOnly<{
}>;

export type TextMatchTransformer = $ReadOnly<{
dependencies: Array<Class<LexicalNode>>,
export: (
node: LexicalNode,
exportChildren: (node: ElementNode) => string,
Expand Down
14 changes: 14 additions & 0 deletions packages/lexical-markdown/src/MarkdownShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
$isTextNode,
$setSelection,
} from 'lexical';
import invariant from 'shared/invariant';

import {TRANSFORMERS} from '.';
import {indexBy, PUNCTUATION_OR_SPACE, transformersByType} from './utils';
Expand Down Expand Up @@ -333,6 +334,19 @@ export function registerMarkdownShortcuts(
({trigger}) => trigger,
);

for (const transformer of transformers) {
const type = transformer.type;
if (type === 'element' || type === 'text-match') {
const dependencies = transformer.dependencies;
if (!editor.hasNodes(dependencies)) {
invariant(
false,
'MarkdownShortcuts: missing dependency for transformer. Ensure node dependency is included in editor initial config.',
);
}
}
}

const transform = (
parentNode: ElementNode,
anchorNode: TextNode,
Expand Down
27 changes: 23 additions & 4 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@
*
*/

import type {ListNode, ListType} from '@lexical/list';
import type {ListType} from '@lexical/list';
import type {HeadingTagType} from '@lexical/rich-text';
import type {ElementNode, LexicalNode, TextFormatType, TextNode} from 'lexical';
import type {
ElementNode,
Klass,
LexicalNode,
TextFormatType,
TextNode,
} from 'lexical';

import {$createCodeNode, $isCodeNode} from '@lexical/code';
import {$createLinkNode, $isLinkNode} from '@lexical/link';
import {$createCodeNode, $isCodeNode, CodeNode} from '@lexical/code';
import {$createLinkNode, $isLinkNode, LinkNode} from '@lexical/link';
import {
$createListItemNode,
$createListNode,
$isListItemNode,
$isListNode,
ListItemNode,
ListNode,
} from '@lexical/list';
import {
$createHeadingNode,
$createQuoteNode,
$isHeadingNode,
$isQuoteNode,
HeadingNode,
QuoteNode,
} from '@lexical/rich-text';
import {$createLineBreakNode, $createTextNode, $isTextNode} from 'lexical';

Expand All @@ -32,6 +42,7 @@ export type Transformer =
| TextMatchTransformer;

export type ElementTransformer = {
dependencies: Array<Klass<LexicalNode>>;
export: (
node: LexicalNode,
// eslint-disable-next-line no-shadow
Expand All @@ -55,6 +66,7 @@ export type TextFormatTransformer = Readonly<{
}>;

export type TextMatchTransformer = Readonly<{
dependencies: Array<Klass<LexicalNode>>;
export: (
node: LexicalNode,
// eslint-disable-next-line no-shadow
Expand Down Expand Up @@ -144,6 +156,7 @@ const listExport = (
};

export const HEADING: ElementTransformer = {
dependencies: [HeadingNode],
export: (node, exportChildren) => {
if (!$isHeadingNode(node)) {
return null;
Expand All @@ -160,6 +173,7 @@ export const HEADING: ElementTransformer = {
};

export const QUOTE: ElementTransformer = {
dependencies: [QuoteNode],
export: (node, exportChildren) => {
if (!$isQuoteNode(node)) {
return null;
Expand Down Expand Up @@ -196,6 +210,7 @@ export const QUOTE: ElementTransformer = {
};

export const CODE: ElementTransformer = {
dependencies: [CodeNode],
export: (node: LexicalNode) => {
if (!$isCodeNode(node)) {
return null;
Expand All @@ -217,6 +232,7 @@ export const CODE: ElementTransformer = {
};

export const UNORDERED_LIST: ElementTransformer = {
dependencies: [ListNode, ListItemNode],
export: (node, exportChildren) => {
return $isListNode(node) ? listExport(node, exportChildren, 0) : null;
},
Expand All @@ -226,6 +242,7 @@ export const UNORDERED_LIST: ElementTransformer = {
};

export const CHECK_LIST: ElementTransformer = {
dependencies: [ListNode, ListItemNode],
export: (node, exportChildren) => {
return $isListNode(node) ? listExport(node, exportChildren, 0) : null;
},
Expand All @@ -235,6 +252,7 @@ export const CHECK_LIST: ElementTransformer = {
};

export const ORDERED_LIST: ElementTransformer = {
dependencies: [ListNode, ListItemNode],
export: (node, exportChildren) => {
return $isListNode(node) ? listExport(node, exportChildren, 0) : null;
},
Expand Down Expand Up @@ -299,6 +317,7 @@ export const ITALIC_UNDERSCORE: TextFormatTransformer = {
// - code should go first as it prevents any transformations inside
// - then longer tags match (e.g. ** or __ should go before * or _)
export const LINK: TextMatchTransformer = {
dependencies: [LinkNode],
export: (node, exportChildren, exportFormat) => {
if (!$isLinkNode(node)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {
$createHorizontalRuleNode,
$isHorizontalRuleNode,
HorizontalRuleNode,
} from '@lexical/react/LexicalHorizontalRuleNode';
import {
$createTableCellNode,
Expand All @@ -32,6 +33,7 @@ import {
TableCellHeaderStates,
TableCellNode,
TableNode,
TableRowNode,
} from '@lexical/table';
import {
$createParagraphNode,
Expand All @@ -41,11 +43,16 @@ import {
$isTextNode,
} from 'lexical';

import {$createEquationNode, $isEquationNode} from '../../nodes/EquationNode';
import {$createImageNode, $isImageNode} from '../../nodes/ImageNode';
import {$createTweetNode, $isTweetNode} from '../../nodes/TweetNode';
import {
$createEquationNode,
$isEquationNode,
EquationNode,
} from '../../nodes/EquationNode';
import {$createImageNode, $isImageNode, ImageNode} from '../../nodes/ImageNode';
import {$createTweetNode, $isTweetNode, TweetNode} from '../../nodes/TweetNode';

export const HR: ElementTransformer = {
dependencies: [HorizontalRuleNode],
export: (node: LexicalNode) => {
return $isHorizontalRuleNode(node) ? '***' : null;
},
Expand All @@ -66,6 +73,7 @@ export const HR: ElementTransformer = {
};

export const IMAGE: TextMatchTransformer = {
dependencies: [ImageNode],
export: (node, exportChildren, exportFormat) => {
if (!$isImageNode(node)) {
return null;
Expand All @@ -89,6 +97,7 @@ export const IMAGE: TextMatchTransformer = {
};

export const EQUATION: TextMatchTransformer = {
dependencies: [EquationNode],
export: (node, exportChildren, exportFormat) => {
if (!$isEquationNode(node)) {
return null;
Expand All @@ -108,6 +117,7 @@ export const EQUATION: TextMatchTransformer = {
};

export const TWEET: ElementTransformer = {
dependencies: [TweetNode],
export: (node) => {
if (!$isTweetNode(node)) {
return null;
Expand All @@ -128,6 +138,7 @@ export const TWEET: ElementTransformer = {
const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;

export const TABLE: ElementTransformer = {
dependencies: [TableNode, TableRowNode, TableCellNode],
export: (
node: LexicalNode,
exportChildren: (elementNode: ElementNode) => string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
$createHorizontalRuleNode,
$isHorizontalRuleNode,
HorizontalRuleNode,
} from '@lexical/react/LexicalHorizontalRuleNode';
import {useEffect} from 'react';

const HR: ElementTransformer = {
dependencies: [HorizontalRuleNode],
export: (node: LexicalNode) => {
return $isHorizontalRuleNode(node) ? '***' : null;
},
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ type InternalSerializedNode = {
version: number,
};

declare export function $getEditor(): LexicalEditor;

declare export function $parseSerializedNode(
serializedNode: InternalSerializedNode,
): LexicalNode;
Expand Down
5 changes: 4 additions & 1 deletion packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export {
$isNodeSelection,
$isRangeSelection,
} from './LexicalSelection';
export {$parseSerializedNode} from './LexicalUpdates';
export {
getActiveEditor as $getEditor,
$parseSerializedNode,
} from './LexicalUpdates';
export {
$getDecoratorNode,
$getNearestNodeFromDOMNode,
Expand Down