Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(editor): keep date mentions in table cells (#8076)

Wolf Mermelstein99852cf53e5d
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableCellContent.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableCellContent.test.ts
index 65092517de..164135794f 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableCellContent.test.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableCellContent.test.ts
@@ -4,6 +4,10 @@ import {
$createTableRowNode,
$isTableNode,
} from '@lexical/table';
+import {
+ $createDateMentionNode,
+ $isDateMentionNode,
+} from '@macro-inc/lexical-core/nodes/DateMentionNode';
import {
$createImageNode,
$isImageNode,
@@ -65,6 +69,38 @@ describe('table cell allowed content', () => {
});
});
+ it('keeps a date mention appended to a table cell', async () => {
+ const editor = createTableEditor();
+ await buildTable(editor, textGrid([['hello']]));
+ let mentionKey = '';
+
+ editor.update(
+ () => {
+ const mention = $createDateMentionNode({
+ date: '2026-03-24T00:00:00.000Z',
+ displayFormat: 'March 24, 2026',
+ });
+ mentionKey = mention.getKey();
+ $getCell(0, 0).append(mention);
+ },
+ { discrete: true }
+ );
+
+ editor.read(() => {
+ const children = $getCell(0, 0).getChildren();
+ expect(children.map((child) => child.getType())).toEqual([
+ 'paragraph',
+ 'paragraph',
+ ]);
+ const mention = $isElementNode(children[1])
+ ? children[1].getChildren().find($isDateMentionNode)
+ : undefined;
+ expect(mention?.getKey()).toBe(mentionKey);
+ expect(mention?.getDate()).toBe('2026-03-24T00:00:00.000Z');
+ expect(mention?.getDisplayFormat()).toBe('March 24, 2026');
+ });
+ });
+
it('keeps an image appended to a table cell', async () => {
const editor = createTableEditor();
await buildTable(editor, textGrid([['hello']]));
diff --git a/packages/lexical-core/tests/tables.test.ts b/packages/lexical-core/tests/tables.test.ts
index 6571126d82..60baacc1c6 100644
--- a/packages/lexical-core/tests/tables.test.ts
+++ b/packages/lexical-core/tests/tables.test.ts
@@ -24,6 +24,10 @@ import {
} from 'lexical';
import { describe, expect, it } from 'vitest';
import { SupportedNodeTypes } from '../node-list';
+import {
+ $createDateMentionNode,
+ $isDateMentionNode,
+} from '../nodes/DateMentionNode';
import { $createImageNode, $isImageNode } from '../nodes/ImageNode';
import { $createVideoNode, $isVideoNode } from '../nodes/VideoNode';
import { EXTERNAL_TRANSFORMERS, INTERNAL_TRANSFORMERS } from '../transformers';
@@ -322,6 +326,119 @@ describe('lists inside table cells', () => {
});
});
+describe('date mentions inside table cells', () => {
+ const DATE_MENTION_MARKDOWN =
+ '<m-date-mention>{"date":"2026-03-24T00:00:00.000Z","displayFormat":"March 24, 2026"}</m-date-mention>';
+
+ function $getFirstCellDateMention() {
+ const table = $getFirstTable();
+ const [row] = table.getChildren().filter($isTableRowNode);
+ const [cell] = (row as TableRowNode).getChildren().filter($isTableCellNode);
+ const mention = cell
+ .getChildren()
+ .flatMap((child) =>
+ $isDateMentionNode(child)
+ ? [child]
+ : $isElementNode(child)
+ ? child.getChildren().filter($isDateMentionNode)
+ : []
+ )[0];
+ expect(mention).toBeDefined();
+ return mention!;
+ }
+
+ it('round-trips a date mention in a cell through internal markdown', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(
+ editor,
+ `<m-table><m-table-row><m-table-cell>Due ${DATE_MENTION_MARKDOWN}</m-table-cell></m-table-row></m-table>`
+ );
+
+ const markdown = exportMarkdown(editor);
+ expect(markdown).toContain(DATE_MENTION_MARKDOWN);
+
+ await importMarkdown(editor, markdown);
+ editor.getEditorState().read(() => {
+ const mention = $getFirstCellDateMention();
+ expect(mention.getDate()).toBe('2026-03-24T00:00:00.000Z');
+ expect(mention.getDisplayFormat()).toBe('March 24, 2026');
+ });
+ });
+
+ it('round-trips a date mention appended to a cell', async () => {
+ const editor = createTestEditor();
+
+ await buildEditorState(editor, () => {
+ const table = $createTableNode();
+ const row = $createTableRowNode();
+ const cell = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
+ const paragraph = $createParagraphNode();
+ paragraph.append(
+ $createTextNode('Due '),
+ $createDateMentionNode({
+ date: '2026-03-24T00:00:00.000Z',
+ displayFormat: 'March 24, 2026',
+ })
+ );
+ cell.append(paragraph);
+ row.append(cell);
+ table.append(row);
+ $getRoot().append(table);
+ });
+
+ const markdown = exportMarkdown(editor);
+ expect(markdown).toContain(DATE_MENTION_MARKDOWN);
+
+ await importMarkdown(editor, markdown);
+ editor.getEditorState().read(() => {
+ const mention = $getFirstCellDateMention();
+ expect(mention.getDate()).toBe('2026-03-24T00:00:00.000Z');
+ expect(mention.getDisplayFormat()).toBe('March 24, 2026');
+ });
+ });
+
+ it('round-trips a date mention in a cell through external pipe-table markdown', async () => {
+ const editor = createTestEditor();
+ await importMarkdown(
+ editor,
+ `<m-table><m-table-row><m-table-cell>Due ${DATE_MENTION_MARKDOWN}</m-table-cell><m-table-cell>plain</m-table-cell></m-table-row></m-table>`
+ );
+
+ const markdown = exportMarkdown(editor, EXTERNAL_TRANSFORMERS);
+ expect(markdown).toContain(DATE_MENTION_MARKDOWN);
+ expect(markdown).toContain('|');
+ expect(markdown).toContain('plain');
+
+ await buildEditorState(editor, () => {
+ $getRoot().clear();
+ $convertFromMarkdownString(markdown, EXTERNAL_TRANSFORMERS);
+ });
+ editor.getEditorState().read(() => {
+ const mention = $getFirstCellDateMention();
+ expect(mention.getDate()).toBe('2026-03-24T00:00:00.000Z');
+ expect(mention.getDisplayFormat()).toBe('March 24, 2026');
+ });
+ });
+
+ it('imports an internal date mention tag from a pipe-table cell', async () => {
+ const editor = createTestEditor();
+
+ await buildEditorState(editor, () => {
+ $getRoot().clear();
+ $convertFromMarkdownString(
+ `| Due ${DATE_MENTION_MARKDOWN} | plain |`,
+ EXTERNAL_TRANSFORMERS
+ );
+ });
+
+ editor.getEditorState().read(() => {
+ const mention = $getFirstCellDateMention();
+ expect(mention.getDate()).toBe('2026-03-24T00:00:00.000Z');
+ expect(mention.getDisplayFormat()).toBe('March 24, 2026');
+ });
+ });
+});
+
describe('images inside table cells', () => {
function $getFirstCellImage() {
const table = $getFirstTable();
diff --git a/packages/lexical-core/transformers/tables.ts b/packages/lexical-core/transformers/tables.ts
index 48f7b959e5..46a5888d00 100644
--- a/packages/lexical-core/transformers/tables.ts
+++ b/packages/lexical-core/transformers/tables.ts
@@ -1,71 +1,72 @@
import {
- $convertFromMarkdownString,
- $convertToMarkdownString,
- CHECK_LIST,
- type ElementTransformer,
- TRANSFORMERS,
- type Transformer,
-} from "@lexical/markdown";
+ $convertFromMarkdownString,
+ $convertToMarkdownString,
+ CHECK_LIST,
+ type ElementTransformer,
+ TRANSFORMERS,
+ type Transformer,
+} from '@lexical/markdown';
import {
- $computeTableMapSkipCellCheck,
- $createTableNode,
- $createTableRowNode,
- $isTableCellNode,
- $isTableNode,
- $isTableRowNode,
- TableCellHeaderStates,
- TableCellNode,
- TableNode,
- TableRowNode,
-} from "@lexical/table";
-import { $isParagraphNode, $isTextNode, type LexicalNode } from "lexical";
-import { z } from "zod";
-import { CUSTOM_TRANSFORMERS } from "./customTransformers";
-import { HTML_TEXT_FORMAT_TRANSFORMERS } from "./htmlTextFormats";
-import { I_IMAGE_CONSTRAINED, IMAGE } from "./image";
-import { E_BLOCK_EQUATION_NODE, I_EQUATION_NODE } from "./katex";
+ $computeTableMapSkipCellCheck,
+ $createTableNode,
+ $createTableRowNode,
+ $isTableCellNode,
+ $isTableNode,
+ $isTableRowNode,
+ TableCellHeaderStates,
+ TableCellNode,
+ TableNode,
+ TableRowNode,
+} from '@lexical/table';
+import { $isParagraphNode, $isTextNode, type LexicalNode } from 'lexical';
+import { z } from 'zod';
+import { CUSTOM_TRANSFORMERS } from './customTransformers';
+import { HTML_TEXT_FORMAT_TRANSFORMERS } from './htmlTextFormats';
+import { I_IMAGE_CONSTRAINED, IMAGE } from './image';
+import { E_BLOCK_EQUATION_NODE, I_EQUATION_NODE } from './katex';
import {
- E_CONTACT_MENTION,
- E_DOCUMENT_MENTION,
- E_USER_MENTION,
- I_CONTACT_MENTION,
- I_DOCUMENT_MENTION,
- I_USER_MENTION,
-} from "./mentions";
-import { BR_TAG_TO_LINE_BREAK, HTML_ENTITY_TRANSFORMERS } from "./transformers";
-import { I_VIDEO } from "./video";
+ E_CONTACT_MENTION,
+ E_DOCUMENT_MENTION,
+ E_USER_MENTION,
+ I_CONTACT_MENTION,
+ I_DATE_MENTION,
+ I_DOCUMENT_MENTION,
+ I_USER_MENTION,
+} from './mentions';
+import { BR_TAG_TO_LINE_BREAK, HTML_ENTITY_TRANSFORMERS } from './transformers';
+import { I_VIDEO } from './video';
// Internal Table Node
-const TAG_TABLE = "m-table";
-const TAG_TABLE_ROW = "m-table-row";
-const TAG_TABLE_CELL = "m-table-cell";
+const TAG_TABLE = 'm-table';
+const TAG_TABLE_ROW = 'm-table-row';
+const TAG_TABLE_CELL = 'm-table-cell';
// Like `xmlMatcher`, but tolerates optional `key="value"` attributes on the
// opening tag so attribute-less tags from older documents still match.
// Group 1 is the raw attribute string, group 2 the tag content.
function xmlMatcherWithAttrs(tag: string, flags?: string) {
- return new RegExp(
- `<${tag}((?:\\s+[\\w-]+="[^"]*")*)\\s*>(.*?)</${tag}>`,
- flags ?? "s",
- );
+ return new RegExp(
+ `<${tag}((?:\\s+[\\w-]+="[^"]*")*)\\s*>(.*?)</${tag}>`,
+ flags ?? 's'
+ );
}
function serializeXmlAttrs(
- attrs: Record<string, string | number | undefined>,
+ attrs: Record<string, string | number | undefined>
): string {
- return Object.entries(attrs)
- .filter(([, value]) => value !== undefined)
- .map(([key, value]) => ` ${key}="${value}"`)
- .join("");
+ return Object.entries(attrs)
+ .filter(([, value]) => value !== undefined)
+ .map(([key, value]) => ` ${key}="${value}"`)
+ .join('');
}
function parseXmlAttrs(raw: string | undefined): Record<string, string> {
- const attrs: Record<string, string> = {};
- for (const match of (raw ?? "").matchAll(/([\w-]+)="([^"]*)"/g)) {
- attrs[match[1]] = match[2];
- }
- return attrs;
+ const attrs: Record<string, string> = {};
+ for (const match of (raw ?? '').matchAll(/([\w-]+)="([^"]*)"/g)) {
+ attrs[match[1]] = match[2];
+ }
+ return attrs;
}
// Attribute strings from parseXmlAttrs → validated numbers; invalid values are
@@ -76,165 +77,168 @@ const A_SPAN = z.coerce.number().int().min(2);
// Upstream's exact definition from @lexical/table's LexicalTableCellNode.ts;
// the package doesn't re-export the type from its root (not even on main).
type TableCellHeaderState =
- (typeof TableCellHeaderStates)[keyof typeof TableCellHeaderStates];
+ (typeof TableCellHeaderStates)[keyof typeof TableCellHeaderStates];
const HEADER_ATTR_BY_STATE: Record<number, string> = {
- [TableCellHeaderStates.ROW]: "row",
- [TableCellHeaderStates.COLUMN]: "col",
- [TableCellHeaderStates.BOTH]: "both",
+ [TableCellHeaderStates.ROW]: 'row',
+ [TableCellHeaderStates.COLUMN]: 'col',
+ [TableCellHeaderStates.BOTH]: 'both',
};
const HEADER_STATE_BY_ATTR: Record<string, TableCellHeaderState> = {
- row: TableCellHeaderStates.ROW,
- col: TableCellHeaderStates.COLUMN,
- both: TableCellHeaderStates.BOTH,
+ row: TableCellHeaderStates.ROW,
+ col: TableCellHeaderStates.COLUMN,
+ both: TableCellHeaderStates.BOTH,
};
-const REG_EXP_XML_TABLE = xmlMatcherWithAttrs(TAG_TABLE, "");
-const REG_EXP_XML_TABLE_ROW = xmlMatcherWithAttrs(TAG_TABLE_ROW, "gs");
-const REG_EXP_XML_TABLE_CELL = xmlMatcherWithAttrs(TAG_TABLE_CELL, "gs");
+const REG_EXP_XML_TABLE = xmlMatcherWithAttrs(TAG_TABLE, '');
+const REG_EXP_XML_TABLE_ROW = xmlMatcherWithAttrs(TAG_TABLE_ROW, 'gs');
+const REG_EXP_XML_TABLE_CELL = xmlMatcherWithAttrs(TAG_TABLE_CELL, 'gs');
const internalTransformersWithinTables: Transformer[] = [
- CHECK_LIST,
- I_USER_MENTION,
- I_DOCUMENT_MENTION,
- I_CONTACT_MENTION,
- I_EQUATION_NODE,
- I_IMAGE_CONSTRAINED,
- IMAGE,
- I_VIDEO,
- ...HTML_TEXT_FORMAT_TRANSFORMERS,
- ...TRANSFORMERS,
+ CHECK_LIST,
+ I_USER_MENTION,
+ I_DOCUMENT_MENTION,
+ I_CONTACT_MENTION,
+ I_DATE_MENTION,
+ I_EQUATION_NODE,
+ I_IMAGE_CONSTRAINED,
+ IMAGE,
+ I_VIDEO,
+ ...HTML_TEXT_FORMAT_TRANSFORMERS,
+ ...TRANSFORMERS,
];
// Transformers used inside table cells
const _TABLE_CELL_TRANSFORMERS: Transformer[] = [
- ...CUSTOM_TRANSFORMERS,
- I_EQUATION_NODE,
- I_USER_MENTION,
- I_DOCUMENT_MENTION,
- I_CONTACT_MENTION,
+ ...CUSTOM_TRANSFORMERS,
+ I_EQUATION_NODE,
+ I_USER_MENTION,
+ I_DOCUMENT_MENTION,
+ I_CONTACT_MENTION,
+ I_DATE_MENTION,
];
// Transformers that export table cells to markdown
const _TABLE_CELL_EXPORT_TRANSFORMERS: Transformer[] = [
- ...CUSTOM_TRANSFORMERS,
- I_EQUATION_NODE,
- I_USER_MENTION,
- I_DOCUMENT_MENTION,
- I_CONTACT_MENTION,
+ ...CUSTOM_TRANSFORMERS,
+ I_EQUATION_NODE,
+ I_USER_MENTION,
+ I_DOCUMENT_MENTION,
+ I_CONTACT_MENTION,
+ I_DATE_MENTION,
];
export const I_TABLE_NODE: ElementTransformer = {
- dependencies: [TableNode, TableRowNode, TableCellNode],
- type: "element",
- regExp: REG_EXP_XML_TABLE,
-
- export: (node) => {
- if (!(node instanceof TableNode)) return null;
-
- const colWidths = node.getColWidths();
- let output = `<${TAG_TABLE}${serializeXmlAttrs({
- "col-widths": colWidths?.map((width) => Math.round(width)).join(","),
- })}>`;
- const rows = node.getChildren();
-
- for (const row of rows) {
- if (row instanceof TableRowNode) {
- output += `<${TAG_TABLE_ROW}${serializeXmlAttrs({
- height: row.getHeight(),
- })}>`;
- const cells = row.getChildren();
-
- for (const cell of cells) {
- if (cell instanceof TableCellNode) {
- output += `<${TAG_TABLE_CELL}${serializeXmlAttrs({
- colspan: cell.getColSpan() > 1 ? cell.getColSpan() : undefined,
- rowspan: cell.getRowSpan() > 1 ? cell.getRowSpan() : undefined,
- header: HEADER_ATTR_BY_STATE[cell.__headerState],
- })}>`;
- output += $convertToMarkdownString(
- internalTransformersWithinTables,
- cell,
- ).replace(/\n/g, "\\n");
- output += `</${TAG_TABLE_CELL}>`;
- }
- }
-
- output += `</${TAG_TABLE_ROW}>`;
- }
- }
-
- output += `</${TAG_TABLE}>`;
- return output;
- },
-
- replace: (node, _children, match, _isImport) => {
- try {
- const xmlContent = match[0];
- const tableAttrs = parseXmlAttrs(match[1]);
- const tableNode = new TableNode();
- const rowMatches = xmlContent.matchAll(REG_EXP_XML_TABLE_ROW);
-
- for (const rowMatch of rowMatches) {
- const rowAttrs = parseXmlAttrs(rowMatch[1]);
- const rowContent = rowMatch[2];
- const rowNode = new TableRowNode();
-
- const height = A_POSITIVE.optional()
- .catch(undefined)
- .parse(rowAttrs.height);
- if (height) {
- rowNode.setHeight(height);
- }
-
- const cellMatches = rowContent.matchAll(REG_EXP_XML_TABLE_CELL);
-
- for (const cellMatch of cellMatches) {
- const cellAttrs = parseXmlAttrs(cellMatch[1]);
- const cellContent = cellMatch[2];
- const cellNode = new TableCellNode();
-
- const colSpan = A_SPAN.optional()
- .catch(undefined)
- .parse(cellAttrs.colspan);
- if (colSpan) {
- cellNode.setColSpan(colSpan);
- }
- const rowSpan = A_SPAN.optional()
- .catch(undefined)
- .parse(cellAttrs.rowspan);
- if (rowSpan) {
- cellNode.setRowSpan(rowSpan);
- }
- const headerState = HEADER_STATE_BY_ATTR[cellAttrs.header];
- if (headerState !== undefined) {
- cellNode.setHeaderStyles(headerState);
- }
-
- $convertFromMarkdownString(
- cellContent.replace(/\\n/g, "\n").replaceAll("<br>", ""),
- internalTransformersWithinTables,
- cellNode,
- );
- rowNode.append(cellNode);
- }
-
- tableNode.append(rowNode);
- }
-
- const colWidths = z
- .array(A_POSITIVE)
- .safeParse(tableAttrs["col-widths"]?.split(","));
- if (colWidths.success && colWidths.data.length) {
- tableNode.setColWidths(colWidths.data);
- }
-
- node.replace(tableNode);
- } catch (error) {
- console.error("Error parsing m-table:", error);
- }
- },
+ dependencies: [TableNode, TableRowNode, TableCellNode],
+ type: 'element',
+ regExp: REG_EXP_XML_TABLE,
+
+ export: (node) => {
+ if (!(node instanceof TableNode)) return null;
+
+ const colWidths = node.getColWidths();
+ let output = `<${TAG_TABLE}${serializeXmlAttrs({
+ 'col-widths': colWidths?.map((width) => Math.round(width)).join(','),
+ })}>`;
+ const rows = node.getChildren();
+
+ for (const row of rows) {
+ if (row instanceof TableRowNode) {
+ output += `<${TAG_TABLE_ROW}${serializeXmlAttrs({
+ height: row.getHeight(),
+ })}>`;
+ const cells = row.getChildren();
+
+ for (const cell of cells) {
+ if (cell instanceof TableCellNode) {
+ output += `<${TAG_TABLE_CELL}${serializeXmlAttrs({
+ colspan: cell.getColSpan() > 1 ? cell.getColSpan() : undefined,
+ rowspan: cell.getRowSpan() > 1 ? cell.getRowSpan() : undefined,
+ header: HEADER_ATTR_BY_STATE[cell.__headerState],
+ })}>`;
+ output += $convertToMarkdownString(
+ internalTransformersWithinTables,
+ cell
+ ).replace(/\n/g, '\\n');
+ output += `</${TAG_TABLE_CELL}>`;
+ }
+ }
+
+ output += `</${TAG_TABLE_ROW}>`;
+ }
+ }
+
+ output += `</${TAG_TABLE}>`;
+ return output;
+ },
+
+ replace: (node, _children, match, _isImport) => {
+ try {
+ const xmlContent = match[0];
+ const tableAttrs = parseXmlAttrs(match[1]);
+ const tableNode = new TableNode();
+ const rowMatches = xmlContent.matchAll(REG_EXP_XML_TABLE_ROW);
+
+ for (const rowMatch of rowMatches) {
+ const rowAttrs = parseXmlAttrs(rowMatch[1]);
+ const rowContent = rowMatch[2];
+ const rowNode = new TableRowNode();
+
+ const height = A_POSITIVE.optional()
+ .catch(undefined)
+ .parse(rowAttrs.height);
+ if (height) {
+ rowNode.setHeight(height);
+ }
+
+ const cellMatches = rowContent.matchAll(REG_EXP_XML_TABLE_CELL);
+
+ for (const cellMatch of cellMatches) {
+ const cellAttrs = parseXmlAttrs(cellMatch[1]);
+ const cellContent = cellMatch[2];
+ const cellNode = new TableCellNode();
+
+ const colSpan = A_SPAN.optional()
+ .catch(undefined)
+ .parse(cellAttrs.colspan);
+ if (colSpan) {
+ cellNode.setColSpan(colSpan);
+ }
+ const rowSpan = A_SPAN.optional()
+ .catch(undefined)
+ .parse(cellAttrs.rowspan);
+ if (rowSpan) {
+ cellNode.setRowSpan(rowSpan);
+ }
+ const headerState = HEADER_STATE_BY_ATTR[cellAttrs.header];
+ if (headerState !== undefined) {
+ cellNode.setHeaderStyles(headerState);
+ }
+
+ $convertFromMarkdownString(
+ cellContent.replace(/\\n/g, '\n').replaceAll('<br>', ''),
+ internalTransformersWithinTables,
+ cellNode
+ );
+ rowNode.append(cellNode);
+ }
+
+ tableNode.append(rowNode);
+ }
+
+ const colWidths = z
+ .array(A_POSITIVE)
+ .safeParse(tableAttrs['col-widths']?.split(','));
+ if (colWidths.success && colWidths.data.length) {
+ tableNode.setColWidths(colWidths.data);
+ }
+
+ node.replace(tableNode);
+ } catch (error) {
+ console.error('Error parsing m-table:', error);
+ }
+ },
};
// External Table Node
@@ -243,185 +247,186 @@ const REG_EXP_TABLE_ROW = /^(?:\|)(.+)(?:\|)\s?$/;
const REG_EXP_TABLE_ROW_DIVIDER = /^(\| ?:?-*:? ?)+\|\s?$/;
const externalTransformersWithinTables: Transformer[] = [
- BR_TAG_TO_LINE_BREAK,
- CHECK_LIST,
- E_USER_MENTION,
- I_DOCUMENT_MENTION, // for citations
- E_DOCUMENT_MENTION,
- E_CONTACT_MENTION,
- E_BLOCK_EQUATION_NODE,
- IMAGE,
- I_VIDEO,
- ...HTML_ENTITY_TRANSFORMERS,
- ...HTML_TEXT_FORMAT_TRANSFORMERS,
- ...TRANSFORMERS,
+ BR_TAG_TO_LINE_BREAK,
+ CHECK_LIST,
+ E_USER_MENTION,
+ I_DOCUMENT_MENTION, // for citations
+ E_DOCUMENT_MENTION,
+ E_CONTACT_MENTION,
+ I_DATE_MENTION,
+ E_BLOCK_EQUATION_NODE,
+ IMAGE,
+ I_VIDEO,
+ ...HTML_ENTITY_TRANSFORMERS,
+ ...HTML_TEXT_FORMAT_TRANSFORMERS,
+ ...TRANSFORMERS,
];
function getTableColumnsSize(table: TableNode): number {
- const row = table.getFirstChild();
- return $isTableRowNode(row) ? row.getChildrenSize() : 0;
+ const row = table.getFirstChild();
+ return $isTableRowNode(row) ? row.getChildrenSize() : 0;
}
function createTableCell(cellContent: string): TableCellNode {
- const cellNode = new TableCellNode();
- // Export escapes cell newlines as `\n` so rows stay on one line; restore
- // them so multi-line content (lists, multiple blocks) survives the trip.
- $convertFromMarkdownString(
- cellContent.replace(/\\n/g, "\n").trim(),
- externalTransformersWithinTables,
- cellNode,
- true,
- );
- return cellNode;
+ const cellNode = new TableCellNode();
+ // Export escapes cell newlines as `\n` so rows stay on one line; restore
+ // them so multi-line content (lists, multiple blocks) survives the trip.
+ $convertFromMarkdownString(
+ cellContent.replace(/\\n/g, '\n').trim(),
+ externalTransformersWithinTables,
+ cellNode,
+ true
+ );
+ return cellNode;
}
function mapToTableCells(textContent: string): Array<TableCellNode> | null {
- const match = textContent.match(REG_EXP_TABLE_ROW);
- if (!match || !match[1]) {
- return null;
- }
+ const match = textContent.match(REG_EXP_TABLE_ROW);
+ if (!match || !match[1]) {
+ return null;
+ }
- return match[1].split("|").map((text) => createTableCell(text));
+ return match[1].split('|').map((text) => createTableCell(text));
}
export const E_TABLE_NODE: ElementTransformer = {
- dependencies: [TableNode, TableRowNode, TableCellNode],
- type: "element",
- regExp: REG_EXP_TABLE_ROW,
- export: (node: LexicalNode) => {
- if (!$isTableNode(node)) {
- return null;
- }
-
- // Walk the computed grid rather than the row children so merged cells
- // (colspan/rowspan) pad the slots they cover and rows stay rectangular —
- // pipe tables cannot express merges.
- const [gridMap] = $computeTableMapSkipCellCheck(node, null, null);
-
- const output: string[] = [];
- for (let row = 0; row < gridMap.length; row++) {
- const rowOutput: string[] = [];
- let isHeaderRow = false;
-
- for (let column = 0; column < gridMap[row].length; column++) {
- const mapCell = gridMap[row][column];
- if (!mapCell?.cell) {
- rowOutput.push("");
- continue;
- }
-
- if (mapCell.startRow === row && mapCell.startColumn === column) {
- const cellContent = $convertToMarkdownString(
- externalTransformersWithinTables,
- mapCell.cell,
- )
- .replace(/\n/g, "\\n")
- .trim();
- rowOutput.push(cellContent);
- } else {
- rowOutput.push("");
- }
-
- if (
- mapCell.cell.__headerState === TableCellHeaderStates.ROW ||
- mapCell.cell.__headerState === TableCellHeaderStates.BOTH
- ) {
- isHeaderRow = true;
- }
- }
-
- output.push(`| ${rowOutput.join(" | ")} |`);
- if (isHeaderRow) {
- output.push(`| ${rowOutput.map(() => "---").join(" | ")} |`);
- }
- }
- return output.join("\n");
- },
- replace: (parentNode, _unused, match) => {
- if (REG_EXP_TABLE_ROW_DIVIDER.test(match[0])) {
- const table = parentNode.getPreviousSibling();
- if (!table || !$isTableNode(table)) {
- return;
- }
-
- const rows = table.getChildren();
- const lastRow = rows[rows.length - 1];
- if (!lastRow || !$isTableRowNode(lastRow)) {
- return;
- }
-
- for (const cell of lastRow.getChildren()) {
- if (!$isTableCellNode(cell)) {
- continue;
- }
- cell.setHeaderStyles(
- TableCellHeaderStates.NO_STATUS,
- TableCellHeaderStates.NO_STATUS,
- // TableCellHeaderStates.ROW, // disable header for now
- // TableCellHeaderStates.ROW // disable header for now
- );
- }
-
- 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(""));
- }
- }
-
- const previousSibling = parentNode.getPreviousSibling();
- if (
- $isTableNode(previousSibling) &&
- getTableColumnsSize(previousSibling) === maxCells
- ) {
- previousSibling.append(...table.getChildren());
- parentNode.remove();
- } else {
- parentNode.replace(table);
- }
-
- table.selectEnd();
- },
+ dependencies: [TableNode, TableRowNode, TableCellNode],
+ type: 'element',
+ regExp: REG_EXP_TABLE_ROW,
+ export: (node: LexicalNode) => {
+ if (!$isTableNode(node)) {
+ return null;
+ }
+
+ // Walk the computed grid rather than the row children so merged cells
+ // (colspan/rowspan) pad the slots they cover and rows stay rectangular —
+ // pipe tables cannot express merges.
+ const [gridMap] = $computeTableMapSkipCellCheck(node, null, null);
+
+ const output: string[] = [];
+ for (let row = 0; row < gridMap.length; row++) {
+ const rowOutput: string[] = [];
+ let isHeaderRow = false;
+
+ for (let column = 0; column < gridMap[row].length; column++) {
+ const mapCell = gridMap[row][column];
+ if (!mapCell?.cell) {
+ rowOutput.push('');
+ continue;
+ }
+
+ if (mapCell.startRow === row && mapCell.startColumn === column) {
+ const cellContent = $convertToMarkdownString(
+ externalTransformersWithinTables,
+ mapCell.cell
+ )
+ .replace(/\n/g, '\\n')
+ .trim();
+ rowOutput.push(cellContent);
+ } else {
+ rowOutput.push('');
+ }
+
+ if (
+ mapCell.cell.__headerState === TableCellHeaderStates.ROW ||
+ mapCell.cell.__headerState === TableCellHeaderStates.BOTH
+ ) {
+ isHeaderRow = true;
+ }
+ }
+
+ output.push(`| ${rowOutput.join(' | ')} |`);
+ if (isHeaderRow) {
+ output.push(`| ${rowOutput.map(() => '---').join(' | ')} |`);
+ }
+ }
+ return output.join('\n');
+ },
+ replace: (parentNode, _unused, match) => {
+ if (REG_EXP_TABLE_ROW_DIVIDER.test(match[0])) {
+ const table = parentNode.getPreviousSibling();
+ if (!table || !$isTableNode(table)) {
+ return;
+ }
+
+ const rows = table.getChildren();
+ const lastRow = rows[rows.length - 1];
+ if (!lastRow || !$isTableRowNode(lastRow)) {
+ return;
+ }
+
+ for (const cell of lastRow.getChildren()) {
+ if (!$isTableCellNode(cell)) {
+ continue;
+ }
+ cell.setHeaderStyles(
+ TableCellHeaderStates.NO_STATUS,
+ TableCellHeaderStates.NO_STATUS
+ // TableCellHeaderStates.ROW, // disable header for now
+ // TableCellHeaderStates.ROW // disable header for now
+ );
+ }
+
+ 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(''));
+ }
+ }
+
+ const previousSibling = parentNode.getPreviousSibling();
+ if (
+ $isTableNode(previousSibling) &&
+ getTableColumnsSize(previousSibling) === maxCells
+ ) {
+ previousSibling.append(...table.getChildren());
+ parentNode.remove();
+ } else {
+ parentNode.replace(table);
+ }
+
+ table.selectEnd();
+ },
};