:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(conversations): There is no mark type italic in this schema (#100767)
Alex V0fb17f306f36
diff --git a/frontend/src/lib/components/Comments/commentUtils.test.ts b/frontend/src/lib/components/Comments/commentUtils.test.tsnew file mode 100644index 000000000000..e7e3e3e09bf7--- /dev/null+++ b/frontend/src/lib/components/Comments/commentUtils.test.ts@@ -0,0 +1,93 @@+import { JSONContent } from 'lib/components/RichContentEditor/types'++import { getCommentText } from './commentUtils'++describe('getCommentText', () => {+ // Each shape here carries a mark or node DEFAULT_EXTENSIONS lacks, which used to throw out of+ // generateText and crash the ticket thread. The fallback must return readable text instead.+ it.each([+ [+ 'italic mark',+ {+ type: 'doc',+ content: [+ {+ type: 'paragraph',+ content: [+ { type: 'text', text: 'plain ' },+ { type: 'text', text: 'emphasis', marks: [{ type: 'italic' }] },+ ],+ },+ ],+ },+ 'plain emphasis',+ ],+ [+ 'bullet list node',+ {+ type: 'doc',+ content: [+ {+ type: 'bulletList',+ content: [+ {+ type: 'listItem',+ content: [{ type: 'paragraph', content: [{ type: 'text', text: 'one' }] }],+ },+ {+ type: 'listItem',+ content: [{ type: 'paragraph', content: [{ type: 'text', text: 'two' }] }],+ },+ ],+ },+ ],+ },+ 'one\n\ntwo',+ ],+ [+ 'image node',+ {+ type: 'doc',+ content: [+ { type: 'paragraph', content: [{ type: 'text', text: 'see', marks: [{ type: 'italic' }] }] },+ { type: 'image', attrs: { src: 'https://example.com/a.png', alt: 'a' } },+ ],+ },+ 'see\n\n',+ ],+ [+ 'id-less mention',+ {+ type: 'doc',+ content: [+ {+ type: 'paragraph',+ content: [+ { type: 'text', text: 'hi ', marks: [{ type: 'bold' }] },+ { type: 'ph-mention', attrs: {} },+ ],+ },+ ],+ },+ 'hi ',+ ],+ ])('renders %s content without throwing', (_label, richContent, expected) => {+ expect(getCommentText({ rich_content: richContent as JSONContent })).toBe(expected)+ })++ it('serializes a mention through the fallback', () => {+ const richContent: JSONContent = {+ type: 'doc',+ content: [+ {+ type: 'paragraph',+ content: [+ { type: 'text', text: 'hey ', marks: [{ type: 'bold' }] },+ { type: 'ph-mention', attrs: { id: 42 } },+ ],+ },+ ],+ }+ expect(getCommentText({ rich_content: richContent })).toBe('hey @member:42')+ })+})diff --git a/frontend/src/lib/components/Comments/commentUtils.ts b/frontend/src/lib/components/Comments/commentUtils.tsindex df1dfa72f82f..97c562195e10 100644--- a/frontend/src/lib/components/Comments/commentUtils.ts+++ b/frontend/src/lib/components/Comments/commentUtils.ts@@ -1,6 +1,6 @@import { generateText } from '@tiptap/core'-import { JSONContent } from 'lib/components/RichContentEditor/types'+import { JSONContent, RichContentNodeType } from 'lib/components/RichContentEditor/types'import { dayjs } from 'lib/dayjs'import { DEFAULT_EXTENSIONS, serializationOptions } from 'lib/lemon-ui/LemonRichContent/LemonRichContentEditor'import { urls } from 'scenes/urls'@@ -55,7 +55,38 @@ export function getCommentText(comment: { content?: string | null; rich_content?],}- return generateText(content, DEFAULT_EXTENSIONS, serializationOptions)+ try {+ return generateText(content, DEFAULT_EXTENSIONS, serializationOptions)+ } catch {+ // Conversations authors comments with a richer schema than DEFAULT_EXTENSIONS, so a mark or+ // node it lacks (italic, list, image, ...) makes generateText throw. Extract text schema-free.+ return extractPlainText(content)+ }+}++const INLINE_NODE_TYPES = new Set<string>(['text', 'hardBreak', RichContentNodeType.Mention])++/** Concatenate a rich-content doc's text with no ProseMirror schema, so no node or mark can throw. */+function extractPlainText(node: JSONContent): string {+ if (node.type === RichContentNodeType.Mention) {+ return node.attrs?.id != null ? `@member:${node.attrs.id}` : ''+ }+ if (node.type === 'hardBreak') {+ return '\n'+ }+ if (node.type === 'image') {+ return node.attrs?.src ? `` : ''+ }+ const children = node.content ?? []+ const joined = children+ .map(extractPlainText)+ // Block children are separated by a blank line, inline runs concatenate directly.+ .map((part, index) => {+ const isBlock = index > 0 && !INLINE_NODE_TYPES.has(children[index].type ?? '')+ return isBlock ? `\n\n${part}` : part+ })+ .join('')+ return (node.text ?? '') + joined}export function isViewingRecording(recordingId: string): boolean {