Skip to content
Open
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions packages/lexical-markdown/flow/LexicalMarkdown.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ declare export var CHECK_LIST: ElementTransformer;
declare export var ORDERED_LIST: ElementTransformer;

declare export var LINK: TextMatchTransformer;
declare export var INDENT: TextMatchTransformer;

declare export var TRANSFORMERS: Array<Transformer>;
declare export var ELEMENT_TRANSFORMERS: Array<ElementTransformer>;
Expand Down
34 changes: 34 additions & 0 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
$createLineBreakNode,
$createTextNode,
$getState,
$isParagraphNode,
$isTextNode,
$setState,
createState,
ElementNode,
Expand Down Expand Up @@ -216,6 +218,7 @@ const TAG_START_REGEX = /^<[a-z_][\w-]*(?:\s[^<>]*)?\/?>/i;
const TAG_END_REGEX = /^<\/[a-z_][\w-]*\s*>/i;
const ENDS_WITH = (regex: RegExp) =>
new RegExp(`(?:${regex.source})$`, regex.flags);
const INDENT_REGEX = /^\t+/;

export const listMarkerState = createState('mdListMarker', {
parse: (v) => (typeof v === 'string' && /^[-*+]$/.test(v) ? v : '-'),
Expand Down Expand Up @@ -622,6 +625,37 @@ export const LINK: TextMatchTransformer = {
type: 'text-match',
};

export const INDENT: TextMatchTransformer = {
dependencies: [],
export: (node, exportChildren, exportFormat) => {
const parentNode = node.getParent();
const textContent = node.getTextContent();
if (
!$isParagraphNode(parentNode) ||
!$isTextNode(node) ||
parentNode.getFirstChild() !== node
) {
return null;
}
const indent = parentNode.getIndent();
const textWithFormat = exportFormat(node, textContent);
return textWithFormat ? '\t'.repeat(indent) + textWithFormat : null;
},
importRegExp: INDENT_REGEX,
regExp: INDENT_REGEX,
replace: (textNode, match) => {
const [indents] = match;
const parentNode = textNode.getParent();
if (!parentNode || !$isParagraphNode(parentNode)) {
return;
}
parentNode.setIndent(indents.length);
textNode.setTextContent(textNode.getTextContent().replace(/^\t+/, ''));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like maybe there's a bug elsewhere because tabs are usually supposed to use TabNode

return textNode;
},
type: 'text-match',
};

export const ELEMENT_TRANSFORMERS: Array<ElementTransformer> = [
HEADING,
QUOTE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {describe, expect, it} from 'vitest';
import {
$convertFromMarkdownString,
$convertToMarkdownString,
INDENT,
LINK,
registerMarkdownShortcuts,
TextMatchTransformer,
Expand Down Expand Up @@ -706,6 +707,26 @@ describe('Markdown', () => {
html: '<p><span style="white-space: pre-wrap;">[](https://lexical.dev)</span></p>',
md: '[](https://lexical.dev)',
},
{
customTransformers: [INDENT],
html: '<p style="padding-inline-start: 40px;"><span style="white-space: pre-wrap;">Hello Word</span></p>',
md: '\tHello Word',
},
{
customTransformers: [INDENT],
html: '<p style="padding-inline-start: 80px;"><span style="white-space: pre-wrap;">Hello Word</span></p>',
md: '\t\tHello Word',
},
{
customTransformers: [INDENT],
html: '<p><span style="white-space: pre-wrap;">Hello\t Word</span></p>',
md: 'Hello\t Word',
},
{
customTransformers: [INDENT],
html: '<p style="padding-inline-start: 40px;"><i><em style="white-space: pre-wrap;">Hello</em></i><span style="white-space: pre-wrap;"> Word</span></p>',
md: '\t*Hello* Word',
},
];

for (const {
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ELEMENT_TRANSFORMERS,
HEADING,
HIGHLIGHT,
INDENT,
INLINE_CODE,
ITALIC_STAR,
ITALIC_UNDERSCORE,
Expand Down Expand Up @@ -94,6 +95,7 @@ export {
type ElementTransformer,
HEADING,
HIGHLIGHT,
INDENT,
INLINE_CODE,
ITALIC_STAR,
ITALIC_UNDERSCORE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CHECK_LIST,
ELEMENT_TRANSFORMERS,
ElementTransformer,
INDENT,
MULTILINE_ELEMENT_TRANSFORMERS,
TEXT_FORMAT_TRANSFORMERS,
TEXT_MATCH_TRANSFORMERS,
Expand Down Expand Up @@ -315,6 +316,7 @@ export const PLAYGROUND_TRANSFORMERS: Array<Transformer> = [
EQUATION,
TWEET,
CHECK_LIST,
INDENT,
...ELEMENT_TRANSFORMERS,
...MULTILINE_ELEMENT_TRANSFORMERS,
...TEXT_FORMAT_TRANSFORMERS,
Expand Down