All files / src/parsers list.ts

59.37% Statements 19/32
44.73% Branches 17/38
75% Functions 3/4
59.37% Lines 19/32

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71    1x 6x 6x 2x     6x 9x       9x   21x 13x       8x 8x   8x 3x     8x                 8x     9x   9x                 9x     6x                                  
import { OutputBlockData } from "@editorjs/editorjs";
 
export const list = ({ data }: OutputBlockData) => {
  let listStyle = "ul";
  if (data.style === "ordered") {
    listStyle = "ol";
  }
 
  const recursor = (items: any, listStyle: string) => {
    Iif (!items || !items.length) {
      return "";
    }
 
    const list = items.map((item: any) => {
      // If it's legacy format (just strings)
      if (typeof item === "string") {
        return `<li>${item}</li>`;
      }
 
      // For v2.0 format with content property
      let content = item.content || "";
      let nestedList = "";
 
      if (item.items && item.items.length) {
        nestedList = recursor(item.items, listStyle);
      }
 
      Iif (
        data.style === "checklist" &&
        item.meta &&
        item.meta.hasOwnProperty("checked")
      ) {
        const checked = item.meta.checked ? " checked" : "";
        return `<li><label><input type="checkbox"${checked} disabled> ${content}</label>${nestedList}</li>`;
      }
 
      return `<li>${content}${nestedList}</li>`;
    });
 
    let attributes = "";
 
    Iif (listStyle === "ol" && data.meta) {
      if (data.meta.start && data.meta.start !== 1) {
        attributes += ` start="${data.meta.start}"`;
      }
      if (data.meta.counterType && data.meta.counterType !== "numeric") {
        attributes += ` type="${getCounterTypeAttribute(data.meta.counterType)}"`;
      }
    }
 
    return `<${listStyle}${attributes}>${list.join("")}</${listStyle}>`;
  };
 
  return recursor(data.items, listStyle);
};
 
function getCounterTypeAttribute(counterType: string): string {
  switch (counterType) {
    case "lower-alpha":
      return "a";
    case "upper-alpha":
      return "A";
    case "lower-roman":
      return "i";
    case "upper-roman":
      return "I";
    default:
      return "1";
  }
}