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.

2.4 KB
Raw Blame
"""
Facade re-exports for notebook content-tree helpers.

These functions operate on plain ProseMirror/TipTap dicts — no Django, no ORM —
so cross-product callers that build or sanitize notebook content import them from
here rather than reaching into ``backend.util``.
"""

from typing import Any

from ..markdown_conversion import (
    build_markdown_notebook_content as build_markdown_notebook_content,
    convert_notebook_content_to_markdown as convert_notebook_content_to_markdown,
    is_markdown_notebook_content as is_markdown_notebook_content,
)
from ..util import (
    QUERY_NODE_TYPE as QUERY_NODE_TYPE,
    SAVED_INSIGHT_NODE_KIND as SAVED_INSIGHT_NODE_KIND,
    SHARED_NOTEBOOK_SUPPORTED_NODE_TYPES as SHARED_NOTEBOOK_SUPPORTED_NODE_TYPES,
    TipTapContent as TipTapContent,
    TipTapNode as TipTapNode,
    _parse_markdown_component_props,
    create_bullet_list as create_bullet_list,
    create_empty_paragraph as create_empty_paragraph,
    create_heading_with_text as create_heading_with_text,
    create_paragraph_with_content as create_paragraph_with_content,
    create_paragraph_with_text as create_paragraph_with_text,
    create_task_list as create_task_list,
    create_text_content as create_text_content,
    extract_inline_query_nodes as extract_inline_query_nodes,
    extract_referenced_insight_short_ids as extract_referenced_insight_short_ids,
    filter_notebook_content_for_sharing as filter_notebook_content_for_sharing,
    iter_markdown_blocks,
    iter_prosemirror_nodes as iter_prosemirror_nodes,
    sanitize_text_content as sanitize_text_content,
)
from . import contracts


def parse_markdown_component_document(content: Any) -> list[contracts.NotebookMarkdownComponent]:
    if not is_markdown_notebook_content(content):
        raise contracts.NotebookMarkdownContentInvalid("Content must use the Markdown notebook document format.")

    markdown = convert_notebook_content_to_markdown(content)
    components: list[contracts.NotebookMarkdownComponent] = []
    for block in iter_markdown_blocks(markdown):
        if block.kind != "component" or block.tag_name is None:
            raise contracts.NotebookMarkdownContentInvalid("Account views can contain components only.")
        components.append(
            contracts.NotebookMarkdownComponent(
                tag_name=block.tag_name,
                node_id=block.node_id,
                props=_parse_markdown_component_props(block.source),
            )
        )
    return components