Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

567 B
Raw Blame
import { $getNodeByKey, $isRootOrShadowRoot, type LexicalEditor, type NodeKey } from 'lexical';

export const getSectionTitle = (editor: LexicalEditor, nodeKey: NodeKey): string | undefined =>
  editor.getEditorState().read(() => {
    const node = $getNodeByKey(nodeKey);
    if (!node || $isRootOrShadowRoot(node)) return undefined;

    let block = node.getTopLevelElement();
    while (block) {
      if (block.getType() === 'heading') return block.getTextContent().trim() || undefined;
      block = block.getPreviousSibling();
    }
    return undefined;
  });