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 1 commit
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
17 changes: 16 additions & 1 deletion packages/lexical-markdown/src/MarkdownShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {ElementNode, LexicalEditor, TextNode} from 'lexical';
import {$isCodeNode} from '@lexical/code';
import {
$createRangeSelection,
$getEditor,
$getSelection,
$isLineBreakNode,
$isRangeSelection,
Expand Down Expand Up @@ -54,8 +55,14 @@ function runElementTransformers(
if (textContent[anchorOffset - 1] !== ' ') {
return false;
}
const editor = $getEditor();

for (const {regExp, replace} of elementTransformers) {
for (const {dependencies, regExp, replace} of elementTransformers) {
if (dependencies !== undefined) {
if (!editor.hasNodes(dependencies)) {
continue;
}
}
const match = textContent.match(regExp);

if (match && match[0].length === anchorOffset) {
Expand Down Expand Up @@ -86,13 +93,21 @@ function runTextMatchTransformers(
return false;
}

const editor = $getEditor();

// If typing in the middle of content, remove the tail to do
// reg exp match up to a string end (caret position)
if (anchorOffset < textContent.length) {
textContent = textContent.slice(0, anchorOffset);
}

for (const transformer of transformers) {
const dependencies = transformer.dependencies;
if (dependencies !== undefined) {
if (!editor.hasNodes(dependencies)) {
continue;
}
}
const match = textContent.match(transformer.regExp);

if (match === null) {
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
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