Skip to content

: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.

1.8 KB
Raw Blame
import { JSONContent } from 'lib/components/RichContentEditor/types'
import { FEATURE_FLAGS } from 'lib/constants'
import { FeatureFlagsSet } from 'lib/logic/featureFlagLogic'

import { buildMarkdownNotebookContent, convertNotebookContentToMarkdown } from './Notebook/markdownNotebookV2'
import { CreatePostHogWidgetNodeOptions } from './types'

export const KNOWN_NODES: Record<string, CreatePostHogWidgetNodeOptions<any>> = {}

export function isKernelUiEnabled(featureFlags: FeatureFlagsSet): boolean {
    return !!featureFlags[FEATURE_FLAGS.REVAMPED_PY_NOTEBOOKS]
}

// Jupyter mode lays out the sandbox-kernel cells, so it needs them available too.
export function isJupyterModeAvailable(featureFlags: FeatureFlagsSet): boolean {
    return isKernelUiEnabled(featureFlags)
}

export function defaultNotebookContent(title?: string, content?: JSONContent[]): JSONContent {
    const initialContent = [
        {
            type: 'heading',
            attrs: { level: 1 },
            content: [{ type: 'text', text: title }],
        },
    ] as JSONContent[]

    if (content) {
        initialContent.push(...content)
    }

    const richContent: JSONContent = { type: 'doc', content: initialContent }

    return buildMarkdownNotebookContent(convertNotebookContentToMarkdown(richContent))
}

export function updateContentHeading(content: JSONContent, newTitle: string): JSONContent {
    const firstNode = content?.content?.[0]
    const firstTextNode = firstNode?.content?.[0]
    if (!firstNode || !firstTextNode?.text) {
        return content
    }

    return {
        ...content,
        content: [
            {
                ...firstNode,
                content: [{ ...firstTextNode, text: newTitle }, ...(firstNode.content?.slice(1) ?? [])],
            },
            ...(content.content?.slice(1) ?? []),
        ],
    }
}