All files / components/ContentfulRichText headings.ts

95.12% Statements 39/41
50% Branches 1/2
100% Functions 1/1
95.12% Lines 39/41

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 421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x     18x 18x  
import type { Node } from "@contentful/rich-text-types";
import { isHeading } from "./predicates";
 
import type {
  Heading1,
  Heading2,
  Heading3,
  Heading4,
  Heading5,
  Heading6,
} from "@contentful/rich-text-types";
 
export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
 
export type HeadingTypeByLevel<L extends HeadingLevel> = {
  1: Heading1;
  2: Heading2;
  3: Heading3;
  4: Heading4;
  5: Heading5;
  6: Heading6;
}[L];
 
export const headingTagByLevel = {
  1: "h1",
  2: "h2",
  3: "h3",
  4: "h4",
  5: "h5",
  6: "h6",
} as const;
 
export const getAssertedHeadingLevel = <L extends HeadingLevel>(
  level: L,
  node: Node,
): HeadingTypeByLevel<L> => {
  if (!isHeading<L>(level, node)) {
    throw new Error(`node is not a heading-${level}`);
  }
  return node;
};