/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type {
ElementTransformer,
TextMatchTransformer,
Transformer,
} from '@lexical/markdown';
import type { ElementNode, LexicalNode } from 'lexical';
import {
CHECK_LIST,
ELEMENT_TRANSFORMERS,
TEXT_FORMAT_TRANSFORMERS,
TEXT_MATCH_TRANSFORMERS,
} from '@lexical/markdown';
import {
$createHorizontalRuleNode,
$isHorizontalRuleNode,
HorizontalRuleNode,
} from '@lexical/react/LexicalHorizontalRuleNode';
import {
$createTableCellNode,
$createTableNode,
$createTableRowNode,
$isTableCellNode,
$isTableNode,
$isTableRowNode,
TableCellHeaderStates,
TableCellNode,
TableNode,
TableRowNode,
} from '@lexical/table';
import {
$createParagraphNode,
$createTextNode,
$isElementNode,
$isParagraphNode,
$isTextNode,
} from 'lexical';
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;
},
regExp: /^(---|\*\*\*|___)\s?$/,
replace: (parentNode, _1, _2, isImport) => {
const line = $createHorizontalRuleNode();
// TODO: Get rid of isImport flag
if (isImport || parentNode.getNextSibling() != null) {
parentNode.replace(line);
} else {
parentNode.insertBefore(line);
}
line.selectNext();
},
type: 'element',
};
export const IMAGE: TextMatchTransformer = {
dependencies: [ImageNode],
export: (node, exportChildren, exportFormat) => {
if (!$isImageNode(node)) {
return null;
}
return `})`;
},
importRegExp: /!(?:\[([^[]*)\])(?:\(([^(]+)\))/,
regExp: /!(?:\[([^[]*)\])(?:\(([^(]+)\))$/,
replace: (textNode, match) => {
const [, altText, src] = match;
const imageNode = $createImageNode({
altText,
maxWidth: 800,
src,
});
textNode.replace(imageNode);
},
trigger: ')',
type: 'text-match',
};
export const TWEET: ElementTransformer = {
dependencies: [TweetNode],
export: (node) => {
if (!$isTweetNode(node)) {
return null;
}
return ``;
},
regExp: /\s?$/,
replace: (textNode, _1, match) => {
const [, id] = match;
const tweetNode = $createTweetNode(id);
textNode.replace(tweetNode);
},
type: 'element',
};
// Very primitive table setup
const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
const TABLE_ROW_DIVIDER_REG_EXP = /^(\| ?:?-*:? ?)+\|\s?$/;
export const TABLE: ElementTransformer = {
dependencies: [TableNode, TableRowNode, TableCellNode],
export: (
node: LexicalNode,
exportChildren: (elementNode: ElementNode) => string
) => {
if (!$isTableNode(node)) {
return null;
}
const output = [];
let firstRow = true;
for (const row of node.getChildren()) {
const rowOutput = [];
if ($isTableRowNode(row)) {
for (const cell of row.getChildren()) {
// It's TableCellNode (hence ElementNode) so it's just to make flow happy
if ($isElementNode(cell)) {
rowOutput.push(exportChildren(cell));
}
}
}
output.push(`| ${rowOutput.join(' | ')} |`);
if (firstRow) {
output.push('| ' + rowOutput.map(() => '---').join(' | ') + ' |');
firstRow = false;
}
}
return output.join('\n');
},
regExp: TABLE_ROW_REG_EXP,
replace: (parentNode, _1, match) => {
if (TABLE_ROW_DIVIDER_REG_EXP.test(match[0])) {
parentNode.getPreviousSibling();
parentNode.remove();
return;
}
const matchCells = mapToTableCells(match[0]);
if (matchCells == null) {
return;
}
const rows = [matchCells];
let sibling = parentNode.getPreviousSibling();
let maxCells = matchCells.length;
while (sibling) {
if (!$isParagraphNode(sibling)) {
break;
}
if (sibling.getChildrenSize() !== 1) {
break;
}
const firstChild = sibling.getFirstChild();
if (!$isTextNode(firstChild)) {
break;
}
const cells = mapToTableCells(firstChild.getTextContent());
if (cells == null) {
break;
}
maxCells = Math.max(maxCells, cells.length);
rows.unshift(cells);
const previousSibling = sibling.getPreviousSibling();
sibling.remove();
sibling = previousSibling;
}
const table = $createTableNode();
for (const cells of rows) {
const tableRow = $createTableRowNode();
table.append(tableRow);
for (let i = 0; i < maxCells; i++) {
tableRow.append(i < cells.length ? cells[i] : createTableCell(null));
}
}
const previousSibling = parentNode.getPreviousSibling();
if (
$isTableNode(previousSibling) &&
getTableColumnsSize(previousSibling) === maxCells
) {
previousSibling.append(...table.getChildren());
parentNode.remove();
} else {
parentNode.replace(table);
}
table.selectEnd();
},
type: 'element',
};
function getTableColumnsSize(table: TableNode) {
const row = table.getFirstChild();
return $isTableRowNode(row) ? row.getChildrenSize() : 0;
}
const createTableCell = (
textContent: string | null | undefined
): TableCellNode => {
const cell = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
const paragraph = $createParagraphNode();
if (textContent != null) {
paragraph.append($createTextNode(textContent.trim()));
}
cell.append(paragraph);
return cell;
};
const mapToTableCells = (textContent: string): Array | null => {
// TODO:
// For now plain text, single node. Can be expanded to more complex content
// including formatted text
const match = textContent.match(TABLE_ROW_REG_EXP);
if (!match || !match[1]) {
return null;
}
return match[1].split('|').map((text) => createTableCell(text));
};
export const PLAYGROUND_TRANSFORMERS: Array = [
TABLE,
HR,
IMAGE,
TWEET,
CHECK_LIST,
...ELEMENT_TRANSFORMERS,
...TEXT_FORMAT_TRANSFORMERS,
...TEXT_MATCH_TRANSFORMERS,
];