All files / mutxt-react/src/MuTxt/behavior outline.ts

100% Statements 19/19
100% Branches 15/15
100% Functions 4/4
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581x                         1x                   1x 6x               1x 7x 12x 6x 6x 4x 4x 3x                 6x       1x 1x 1x 1x    
import {type Descendant, Text, Element as SlateElement, Node} from 'slate';
import type {CustomElement, SlateEditorDocument} from '../types';
 
export type OutlineHeadingType = 'title' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
 
export interface DocumentOutlineItem {
  key: string;
  path: number[];
  type: OutlineHeadingType;
  level: 0 | 1 | 2 | 3 | 4 | 5 | 6;
  title: string;
}
 
const HEADING_LEVELS: Record<OutlineHeadingType, DocumentOutlineItem['level']> = {
  title: 0,
  h1: 1,
  h2: 2,
  h3: 3,
  h4: 4,
  h5: 5,
  h6: 6,
};
 
const isOutlineHeadingType = (type: CustomElement['type']): type is OutlineHeadingType =>
  type === 'h1' ||
  type === 'h2' ||
  type === 'h3' ||
  type === 'h4' ||
  type === 'h5' ||
  type === 'h6' ||
  type === 'title';
 
const collectDocumentOutline = (nodes: Descendant[], path: number[], outline: DocumentOutlineItem[]): void => {
  nodes.forEach((node, index) => {
    if (Text.isText(node)) return;
    const nodePath = [...path, index];
    if (SlateElement.isElement(node) && isOutlineHeadingType(node.type)) {
      const title = Node.string(node).trim();
      if (title) {
        outline.push({
          key: nodePath.join('.'),
          path: nodePath,
          type: node.type,
          level: HEADING_LEVELS[node.type],
          title,
        });
      }
    }
    collectDocumentOutline(node.children as Descendant[], nodePath, outline);
  });
};
 
export const getDocumentOutline = (value: SlateEditorDocument): DocumentOutlineItem[] => {
  const outline: DocumentOutlineItem[] = [];
  collectDocumentOutline(value, [], outline);
  return outline;
};