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.

8.6 KB
Raw Blame
import { splitInlineNodesAt } from './inlineContent'
import { makeEmptyParagraph } from './markdown'
import {
    NotebookBlockNode,
    NotebookInlineNode,
    NotebookListBlockNode,
    NotebookListItem,
    NotebookTextBlockNode,
} from './types'
import { getInlineText, normalizeInlineNodes } from './utils'

export type RenderedListItem = NotebookListItem & {
    index: number
    keyPath: string
    childrenItems: RenderedListItem[]
}

export function getListItemRefKey(nodeId: string, itemKey: string | number): string {
    return `${nodeId}:${String(itemKey)}`
}

export function buildRenderedListItems(items: NotebookListItem[]): RenderedListItem[] {
    const rootItems: RenderedListItem[] = []
    const stack: RenderedListItem[] = []

    items.forEach((item, index) => {
        const normalizedDepth = Math.max(0, item.depth)
        while (stack.length && normalizedDepth <= stack[stack.length - 1].depth) {
            stack.pop()
        }

        const parent = stack[stack.length - 1]
        const siblingIndex = parent ? parent.childrenItems.length : rootItems.length
        const renderedItem: RenderedListItem = {
            ...item,
            depth: normalizedDepth,
            index,
            keyPath: parent ? `${parent.keyPath}.${String(siblingIndex)}` : String(siblingIndex),
            childrenItems: [],
        }

        if (parent) {
            parent.childrenItems.push(renderedItem)
        } else {
            rootItems.push(renderedItem)
        }
        stack.push(renderedItem)
    })

    return rootItems
}

export function getOrderedListStart(
    items: NotebookListItem[],
    ordered: boolean,
    fallbackStart?: number
): number | undefined {
    if (!ordered) {
        return undefined
    }

    return items.find((item) => item.depth === 0 && (item.ordered ?? ordered))?.start ?? fallbackStart ?? 1
}

export function normalizeListItemDepths(items: NotebookListItem[]): NotebookListItem[] {
    const minimumDepth = Math.min(...items.map((item) => Math.max(0, item.depth)))
    const baseDepth = Number.isFinite(minimumDepth) ? Math.max(0, minimumDepth) : 0

    // Clamp each item to at most one level deeper than its predecessor, mirroring the parser:
    // deleting a mid-depth item must not leave a deeper survivor orphaned across a depth jump.
    let previousDepth = -1
    return items.map((item) => {
        const depth = Math.min(Math.max(0, item.depth) - baseDepth, previousDepth + 1)
        previousDepth = depth
        return { ...item, depth }
    })
}

/**
 * Builds the nodes that replace a list when one of its items is unwrapped into a paragraph
 * (or quote text when the list is quoted): the items before it stay a list, its children move
 * one depth up, and trailing items become a new list.
 */
export function getListItemParagraphReplacement(
    node: NotebookListBlockNode,
    targetItemIndex: number
): { replacementNodes: NotebookBlockNode[]; paragraphId: string } | null {
    const item = node.items[targetItemIndex]
    if (!item) {
        return null
    }

    const makeListNode = (
        items: NotebookListItem[],
        idSeed: string,
        idOverride?: string
    ): NotebookListBlockNode | null => {
        if (!items.length) {
            return null
        }

        const normalizedItems = normalizeListItemDepths(items)
        const ordered = normalizedItems[0]?.ordered ?? node.ordered
        return {
            ...node,
            id: idOverride ?? makeEmptyParagraph(idSeed).id,
            ordered,
            start: getOrderedListStart(normalizedItems, ordered, node.start),
            items: normalizedItems,
        }
    }

    const subtreeEndIndex = getListItemSubtreeEndIndex(node.items, targetItemIndex)
    const beforeListNode = makeListNode(node.items.slice(0, targetItemIndex), `before-list-${node.id}`, node.id)
    const paragraph: NotebookTextBlockNode = {
        id: beforeListNode ? makeEmptyParagraph(`unlisted-${node.id}`).id : node.id,
        type: node.blockquote ? 'blockquote' : 'paragraph',
        children: item.children,
    }
    const childItems = node.items
        .slice(targetItemIndex + 1, subtreeEndIndex)
        .map((childItem) => ({ ...childItem, depth: Math.max(0, childItem.depth - 1) }))
    const afterListNode = makeListNode([...childItems, ...node.items.slice(subtreeEndIndex)], `after-list-${node.id}`)
    const replacementNodes: NotebookBlockNode[] = []
    if (beforeListNode) {
        replacementNodes.push(beforeListNode)
    }
    replacementNodes.push(paragraph)
    if (afterListNode) {
        replacementNodes.push(afterListNode)
    }

    return { replacementNodes, paragraphId: paragraph.id }
}

export type ListItemSelectionRange = {
    firstItemIndex: number
    firstStart: number
    lastItemIndex: number
    lastEnd: number
}

export type ListItemSelectionDeletion = {
    items: NotebookListItem[]
    caretItemIndex: number
    caretItemId: string | undefined
    caretOffset: number
}

/**
 * Deletes a text selection that lives inside one list block: the first selected item keeps its
 * text before the selection (plus the optional replacement text) and inherits the last selected
 * item's text after it, the items in between are removed, and the caret lands where the
 * selection started. Returns null when the range describes no change.
 */
export function deleteListItemSelectionRange(
    items: NotebookListItem[],
    range: ListItemSelectionRange,
    replacementText: string = ''
): ListItemSelectionDeletion | null {
    const firstItem = items[range.firstItemIndex]
    const lastItem = items[range.lastItemIndex]
    if (!firstItem || !lastItem || range.lastItemIndex < range.firstItemIndex) {
        return null
    }

    const firstTextLength = getInlineText(firstItem.children).length
    const lastTextLength = getInlineText(lastItem.children).length
    const firstStart = Math.max(0, Math.min(range.firstStart, firstTextLength))
    const lastEnd = Math.max(0, Math.min(range.lastEnd, lastTextLength))
    if (range.firstItemIndex === range.lastItemIndex && firstStart >= lastEnd && !replacementText) {
        return null
    }

    const [beforeSelection] = splitInlineNodesAt(firstItem.children, firstStart)
    const [, afterSelection] = splitInlineNodesAt(lastItem.children, lastEnd)
    const insertedChildren: NotebookInlineNode[] = replacementText ? [{ type: 'text', text: replacementText }] : []
    const mergedItem: NotebookListItem = {
        ...firstItem,
        children: normalizeInlineNodes([...beforeSelection, ...insertedChildren, ...afterSelection]),
    }
    const nextItems = normalizeListItemDepths([
        ...items.slice(0, range.firstItemIndex),
        mergedItem,
        ...items.slice(range.lastItemIndex + 1),
    ])

    return {
        items: nextItems,
        caretItemIndex: range.firstItemIndex,
        caretItemId: mergedItem.id,
        caretOffset: getInlineText(beforeSelection).length + replacementText.length,
    }
}

/** Shifts a list item and its subtree one depth step in or out, or returns null when the shift is not allowed. */
export function shiftListItemSubtreeDepth(
    items: NotebookListItem[],
    itemIndex: number,
    direction: 'in' | 'out',
    listOrdered: boolean
): NotebookListItem[] | null {
    const item = items[itemIndex]
    if (!item) {
        return null
    }

    const maximumDepth = itemIndex === 0 ? 0 : items[itemIndex - 1].depth + 1
    const nextDepth = direction === 'in' ? Math.min(item.depth + 1, maximumDepth) : Math.max(0, item.depth - 1)
    const depthDelta = nextDepth - item.depth
    if (depthDelta === 0) {
        return null
    }

    const subtreeEndIndex = getListItemSubtreeEndIndex(items, itemIndex)
    return items.map((currentItem, index) => {
        if (index < itemIndex || index >= subtreeEndIndex) {
            return currentItem
        }

        const nextItem = { ...currentItem, depth: Math.max(0, currentItem.depth + depthDelta) }
        if (index === itemIndex && depthDelta > 0 && (nextItem.ordered ?? listOrdered)) {
            return { ...nextItem, start: undefined }
        }

        return nextItem
    })
}

export function getListItemSubtreeEndIndex(items: NotebookListItem[], itemIndex: number): number {
    const item = items[itemIndex]
    if (!item) {
        return itemIndex
    }

    let nextIndex = itemIndex + 1
    while (nextIndex < items.length && items[nextIndex].depth > item.depth) {
        nextIndex += 1
    }
    return nextIndex
}

export function getListItemIndex(items: NotebookListItem[], fallbackIndex: number, itemId?: string): number {
    if (itemId) {
        const itemIndex = items.findIndex((item) => item.id === itemId)
        if (itemIndex !== -1) {
            return itemIndex
        }
    }

    return fallbackIndex
}