Skip to content

The open alternative to Salesforce, designed for AI.

fix(rich-text): normalize tiptap rich text written by workflow record steps (#26156)

Thomas Trompette78772c144ffc
diff --git a/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text.util.ts b/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text.util.ts
index e8290b528e8..891175921ca 100644
--- a/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/record-transformer/utils/transform-rich-text.util.ts
@@ -3,7 +3,7 @@ import {
type RichTextMetadata,
richTextValueSchema,
} from 'twenty-shared/types';
-import { isDefined } from 'twenty-shared/utils';
+import { convertTipTapBlocksToMarkdown, isDefined } from 'twenty-shared/utils';
import type { ServerBlockNoteEditor } from '@blocknote/server-util';
@@ -39,6 +39,19 @@ export const transformRichTextValue = async (
const serverBlockNoteEditor = await getServerBlockNoteEditor();
+ const tipTapMarkdown = isDefined(parsedValue.blocknote)
+ ? convertTipTapBlocksToMarkdown(parsedValue.blocknote)
+ : undefined;
+
+ if (isDefined(tipTapMarkdown)) {
+ return {
+ markdown: parsedValue.markdown || tipTapMarkdown,
+ blocknote: JSON.stringify(
+ await serverBlockNoteEditor.tryParseMarkdownToBlocks(tipTapMarkdown),
+ ),
+ };
+ }
+
// Patch: Handle cases where blocknote to markdown conversion fails for certain block types (custom/code blocks)
// Todo : This may be resolved once the server-utils library is updated with proper conversion support - #947
let convertedMarkdown: string | null = null;
diff --git a/packages/twenty-server/test/integration/constants/widget-configuration-test-data.constants.ts b/packages/twenty-server/test/integration/constants/widget-configuration-test-data.constants.ts
index b15d154730b..267b4150fa2 100644
--- a/packages/twenty-server/test/integration/constants/widget-configuration-test-data.constants.ts
+++ b/packages/twenty-server/test/integration/constants/widget-configuration-test-data.constants.ts
@@ -32,7 +32,7 @@ export const TEST_STANDALONE_RICH_TEXT_CONFIG: StandaloneRichTextConfigurationDT
configurationType: WidgetConfigurationType.STANDALONE_RICH_TEXT,
body: {
blocknote:
- '{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Hello world"}]}]}',
+ '[{"id":"5f4a1c1e-0000-4000-8000-000000000001","type":"paragraph","props":{},"content":[{"type":"text","text":"Hello world","styles":{}}],"children":[]}]',
markdown: '# Hello world',
},
};
@@ -42,7 +42,17 @@ export const TEST_STANDALONE_RICH_TEXT_CONFIG_MINIMAL: StandaloneRichTextConfigu
configurationType: WidgetConfigurationType.STANDALONE_RICH_TEXT,
body: {
blocknote:
- '{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Simple text"}]}]}',
+ '[{"id":"5f4a1c1e-0000-4000-8000-000000000002","type":"paragraph","props":{},"content":[{"type":"text","text":"Simple text","styles":{}}],"children":[]}]',
+ markdown: null,
+ },
+ };
+
+export const TEST_STANDALONE_RICH_TEXT_CONFIG_TIPTAP_BODY: StandaloneRichTextConfigurationDTO =
+ {
+ configurationType: WidgetConfigurationType.STANDALONE_RICH_TEXT,
+ body: {
+ blocknote:
+ '{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Checklist:"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"call the client"}]}]}]}]}',
markdown: null,
},
};
diff --git a/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-creation.integration-spec.ts.snap b/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-creation.integration-spec.ts.snap
index 374c82859cb..9f08be85199 100644
--- a/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-creation.integration-spec.ts.snap
+++ b/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-creation.integration-spec.ts.snap
@@ -396,7 +396,7 @@ exports[`Page layout widget creation should succeed should create a page layout
{
"configuration": {
"body": {
- "blocknote": "{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Hello world"}]}]}",
+ "blocknote": "[{"id":"5f4a1c1e-0000-4000-8000-000000000001","type":"paragraph","props":{},"content":[{"type":"text","text":"Hello world","styles":{}}],"children":[]}]",
"markdown": "# Hello world",
},
"configurationType": "STANDALONE_RICH_TEXT",
@@ -423,8 +423,9 @@ exports[`Page layout widget creation should succeed should create a page layout
{
"configuration": {
"body": {
- "blocknote": "{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Simple text"}]}]}",
- "markdown": "{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Simple text"}]}]}",
+ "blocknote": "[{"id":"5f4a1c1e-0000-4000-8000-000000000002","type":"paragraph","props":{},"content":[{"type":"text","text":"Simple text","styles":{}}],"children":[]}]",
+ "markdown": "Simple text
+",
},
"configurationType": "STANDALONE_RICH_TEXT",
},
diff --git a/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-update.integration-spec.ts.snap b/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-update.integration-spec.ts.snap
index f3b799f486d..1e1090e3bb5 100644
--- a/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-update.integration-spec.ts.snap
+++ b/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/__snapshots__/successful-page-layout-widget-update.integration-spec.ts.snap
@@ -226,7 +226,7 @@ exports[`Page layout widget update should succeed from a STANDALONE_RICH_TEXT wi
{
"configuration": {
"body": {
- "blocknote": "{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Hello world"}]}]}",
+ "blocknote": "[{"id":"5f4a1c1e-0000-4000-8000-000000000001","type":"paragraph","props":{},"content":[{"type":"text","text":"Hello world","styles":{}}],"children":[]}]",
"markdown": "# Hello world",
},
"configurationType": "STANDALONE_RICH_TEXT",
diff --git a/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/successful-page-layout-widget-creation.integration-spec.ts b/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/successful-page-layout-widget-creation.integration-spec.ts
index 8d9ee0acb43..f1d46eba101 100644
--- a/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/successful-page-layout-widget-creation.integration-spec.ts
+++ b/packages/twenty-server/test/integration/metadata/suites/page-layout-widget/successful-page-layout-widget-creation.integration-spec.ts
@@ -2,6 +2,7 @@ import {
TEST_IFRAME_CONFIG,
TEST_STANDALONE_RICH_TEXT_CONFIG,
TEST_STANDALONE_RICH_TEXT_CONFIG_MINIMAL,
+ TEST_STANDALONE_RICH_TEXT_CONFIG_TIPTAP_BODY,
} from 'test/integration/constants/widget-configuration-test-data.constants';
import { createOnePageLayoutTab } from 'test/integration/metadata/suites/page-layout-tab/utils/create-one-page-layout-tab.util';
import { destroyOnePageLayoutTab } from 'test/integration/metadata/suites/page-layout-tab/utils/destroy-one-page-layout-tab.util';
@@ -26,6 +27,7 @@ import { AxisNameDisplay } from 'src/engine/metadata-modules/page-layout-widget/
import { BarChartLayout } from 'src/engine/metadata-modules/page-layout-widget/enums/bar-chart-layout.enum';
import { ChartNumberFormat } from 'src/engine/metadata-modules/page-layout-widget/enums/chart-number-format.enum';
import { GraphOrderBy } from 'src/engine/metadata-modules/page-layout-widget/enums/graph-order-by.enum';
+import { type StandaloneRichTextConfigurationDTO } from 'src/engine/metadata-modules/page-layout-widget/dtos/standalone-rich-text-configuration.dto';
import { WidgetConfigurationType } from 'src/engine/metadata-modules/page-layout-widget/enums/widget-configuration-type.type';
import { type AllPageLayoutWidgetConfiguration } from 'src/engine/metadata-modules/page-layout-widget/types/all-page-layout-widget-configuration.type';
@@ -364,6 +366,33 @@ describe('Page layout widget creation should succeed', () => {
},
);
+ it('should store a tiptap rich text body as blocknote blocks', async () => {
+ const { data } = await createOnePageLayoutWidget({
+ expectToFail: false,
+ input: {
+ title: 'Rich Text Widget TipTap',
+ type: WidgetType.STANDALONE_RICH_TEXT,
+ configuration: TEST_STANDALONE_RICH_TEXT_CONFIG_TIPTAP_BODY,
+ position: DEFAULT_GRID_POSITION,
+ pageLayoutTabId: testSetup.pageLayoutTabId,
+ },
+ });
+
+ createdPageLayoutWidgetId = data?.createPageLayoutWidget?.id;
+
+ const { body } = data.createPageLayoutWidget
+ .configuration as StandaloneRichTextConfigurationDTO;
+
+ expect(body.markdown).toBe('Checklist:\n\n- call the client');
+
+ const storedBlocks = JSON.parse(body.blocknote ?? '[]');
+
+ expect(storedBlocks.map((block: { type: string }) => block.type)).toEqual([
+ 'paragraph',
+ 'bulletListItem',
+ ]);
+ });
+
it.each(eachTestingContextFilter(graphTestCases))(
'should $title',
async ({ context: { widgetTitle, buildConfiguration } }) => {
diff --git a/packages/twenty-shared/src/utils/index.ts b/packages/twenty-shared/src/utils/index.ts
index 1815159b1ea..4fadc5f5655 100644
--- a/packages/twenty-shared/src/utils/index.ts
+++ b/packages/twenty-shared/src/utils/index.ts
@@ -229,6 +229,7 @@ export { uncapitalize } from './strings/uncapitalize';
export { getSubdomainSlugFromDisplayName } from './subdomain/getSubdomainSlugFromDisplayName';
export type { CanvasTheme } from './tiptap/canvas-theme';
export { CANVAS_THEME_DEFAULTS } from './tiptap/canvas-theme';
+export { convertTipTapBlocksToMarkdown } from './tiptap/convert-tiptap-blocks-to-markdown';
export type { EmailDocumentMarkType } from './tiptap/email-document-mark-catalog';
export {
EMAIL_DOCUMENT_MARK_CATALOG,
diff --git a/packages/twenty-shared/src/utils/tiptap/__tests__/convert-tiptap-blocks-to-markdown.test.ts b/packages/twenty-shared/src/utils/tiptap/__tests__/convert-tiptap-blocks-to-markdown.test.ts
new file mode 100644
index 00000000000..da468191d82
--- /dev/null
+++ b/packages/twenty-shared/src/utils/tiptap/__tests__/convert-tiptap-blocks-to-markdown.test.ts
@@ -0,0 +1,122 @@
+import { convertTipTapBlocksToMarkdown } from '../convert-tiptap-blocks-to-markdown';
+
+describe('convertTipTapBlocksToMarkdown', () => {
+ it('should convert a bare array of TipTap blocks', () => {
+ const blocks = JSON.stringify([
+ {
+ type: 'paragraph',
+ content: [{ type: 'text', text: 'Checklist:' }],
+ },
+ {
+ type: 'bulletList',
+ content: [
+ {
+ type: 'listItem',
+ content: [
+ {
+ type: 'paragraph',
+ content: [{ type: 'text', text: 'call the client' }],
+ },
+ ],
+ },
+ ],
+ },
+ ]);
+
+ expect(convertTipTapBlocksToMarkdown(blocks)).toBe(
+ 'Checklist:\n\n- call the client',
+ );
+ });
+
+ it('should convert a checklist, keeping the checked state', () => {
+ const checklist = JSON.stringify([
+ {
+ type: 'taskList',
+ content: [
+ {
+ type: 'taskItem',
+ attrs: { checked: true },
+ content: [
+ {
+ type: 'paragraph',
+ content: [{ type: 'text', text: 'call the client' }],
+ },
+ ],
+ },
+ ],
+ },
+ ]);
+
+ expect(convertTipTapBlocksToMarkdown(checklist)).toBe(
+ '- [x] call the client',
+ );
+ });
+
+ it('should convert a whole TipTap document', () => {
+ const document = JSON.stringify({
+ type: 'doc',
+ content: [
+ { type: 'paragraph', content: [{ type: 'text', text: 'hello' }] },
+ ],
+ });
+
+ expect(convertTipTapBlocksToMarkdown(document)).toBe('hello');
+ });
+
+ it('should return undefined for invalid json', () => {
+ expect(convertTipTapBlocksToMarkdown('not json')).toBeUndefined();
+ });
+
+ it('should return undefined for an empty array', () => {
+ expect(convertTipTapBlocksToMarkdown('[]')).toBeUndefined();
+ });
+
+ it('should return undefined when a block is not a TipTap node', () => {
+ expect(convertTipTapBlocksToMarkdown('[{"foo":"bar"}]')).toBeUndefined();
+ });
+
+ it('should leave a blocknote body carrying a mention alone', () => {
+ const blocknoteBody = JSON.stringify([
+ {
+ id: 'b1',
+ type: 'paragraph',
+ props: {},
+ children: [],
+ content: [
+ { type: 'text', text: 'Hello ', styles: {} },
+ { type: 'mention', props: { label: 'John Doe' } },
+ ],
+ },
+ ]);
+
+ expect(convertTipTapBlocksToMarkdown(blocknoteBody)).toBeUndefined();
+ });
+
+ it('should leave a blocknote list alone', () => {
+ const blocknoteBody = JSON.stringify([
+ {
+ id: 'b1',
+ type: 'bulletListItem',
+ props: {},
+ children: [],
+ content: [{ type: 'text', text: 'call the client', styles: {} }],
+ },
+ ]);
+
+ expect(convertTipTapBlocksToMarkdown(blocknoteBody)).toBeUndefined();
+ });
+
+ it('should leave a blocknote table alone, whose content is not an array', () => {
+ const blocknoteBody = JSON.stringify([
+ {
+ id: 'b1',
+ type: 'table',
+ props: {},
+ children: [],
+ content: { type: 'tableContent', rows: [] },
+ },
+ ]);
+
+ expect(convertTipTapBlocksToMarkdown(blocknoteBody)).toBeUndefined();
+ });
+});
diff --git a/packages/twenty-shared/src/utils/tiptap/convert-tiptap-blocks-to-markdown.ts b/packages/twenty-shared/src/utils/tiptap/convert-tiptap-blocks-to-markdown.ts
new file mode 100644
index 00000000000..9834de672d9
--- /dev/null
+++ b/packages/twenty-shared/src/utils/tiptap/convert-tiptap-blocks-to-markdown.ts
@@ -0,0 +1,51 @@
+import { isTipTapNode } from './parse-tiptap-json-document';
+import { type TipTapDocument } from './tiptap-document';
+import { tipTapDocumentToMarkdown } from './tiptap-document-to-markdown';
+import { type TipTapNode } from './tiptap-node';
+import { TIPTAP_NODE_TYPES } from './tiptap-node-types';
+
+const TIPTAP_ONLY_NODE_TYPES: string[] = [
+ TIPTAP_NODE_TYPES.DOCUMENT,
+ TIPTAP_NODE_TYPES.BULLET_LIST,
+ TIPTAP_NODE_TYPES.ORDERED_LIST,
+ TIPTAP_NODE_TYPES.LIST_ITEM,
+ TIPTAP_NODE_TYPES.TASK_LIST,
+ TIPTAP_NODE_TYPES.TASK_ITEM,
+ TIPTAP_NODE_TYPES.SECTION,
+ TIPTAP_NODE_TYPES.COLUMNS,
+ TIPTAP_NODE_TYPES.COLUMN,
+ TIPTAP_NODE_TYPES.BUTTON,
+ TIPTAP_NODE_TYPES.DIVIDER,
+ TIPTAP_NODE_TYPES.HTML,
+];
+
+const containsTipTapOnlyNode = (node: TipTapNode): boolean =>
+ TIPTAP_ONLY_NODE_TYPES.includes(node.type) ||
+ (node.content ?? []).some(containsTipTapOnlyNode);
+
+export const convertTipTapBlocksToMarkdown = (
+ serializedBlocks: string,
+): string | undefined => {
+ let parsedBlocks: unknown;
+
+ try {
+ parsedBlocks = JSON.parse(serializedBlocks);
+ } catch {
+ return undefined;
+ }
+
+ const nodes = Array.isArray(parsedBlocks) ? parsedBlocks : [parsedBlocks];
+
+ if (!nodes.every(isTipTapNode) || !nodes.some(containsTipTapOnlyNode)) {
+ return undefined;
+ }
+
+ const [firstNode] = nodes;
+
+ return nodes.length === 1 && firstNode.type === TIPTAP_NODE_TYPES.DOCUMENT
+ ? tipTapDocumentToMarkdown(firstNode as TipTapDocument)
+ : tipTapDocumentToMarkdown({
+ type: TIPTAP_NODE_TYPES.DOCUMENT,
+ content: nodes,
+ });
+};
diff --git a/packages/twenty-shared/src/utils/tiptap/tiptap-document-to-markdown.ts b/packages/twenty-shared/src/utils/tiptap/tiptap-document-to-markdown.ts
index dfd841124dc..7153c6a69c2 100644
--- a/packages/twenty-shared/src/utils/tiptap/tiptap-document-to-markdown.ts
+++ b/packages/twenty-shared/src/utils/tiptap/tiptap-document-to-markdown.ts
@@ -137,6 +137,15 @@ const renderTipTapNodeToMarkdown = (node: TipTapNode): string => {
return `${(node.content ?? [])
.map((item) => renderListItem(item, '- '))
.join('')}\n`;
+ case TIPTAP_NODE_TYPES.TASK_LIST:
+ return `${(node.content ?? [])
+ .map((item) =>
+ renderListItem(
+ item,
+ item.attrs?.checked === true ? '- [x] ' : '- [ ] ',
+ ),
+ )
+ .join('')}\n`;
case TIPTAP_NODE_TYPES.ORDERED_LIST: {
const start =
typeof node.attrs?.start === 'number' ? node.attrs.start : 1;
diff --git a/packages/twenty-shared/src/utils/tiptap/tiptap-node-types.ts b/packages/twenty-shared/src/utils/tiptap/tiptap-node-types.ts
index b696e3ee4ef..10caea0545a 100644
--- a/packages/twenty-shared/src/utils/tiptap/tiptap-node-types.ts
+++ b/packages/twenty-shared/src/utils/tiptap/tiptap-node-types.ts
@@ -10,6 +10,8 @@ export const TIPTAP_NODE_TYPES = {
BULLET_LIST: 'bulletList',
ORDERED_LIST: 'orderedList',
LIST_ITEM: 'listItem',
+ TASK_LIST: 'taskList',
+ TASK_ITEM: 'taskItem',
HARD_BREAK: 'hardBreak',
SECTION: 'section',
COLUMNS: 'columns',