All files / src main.ts

81.81% Statements 18/22
70% Branches 7/10
100% Functions 6/6
81.81% Lines 18/22

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                1x         4x 50x 47x 47x     3x 3x 1x   2x     2x       1x         7x 7x                     1x       5x   5x 4x   7x          
import { OutputBlockData, OutputData } from "@editorjs/editorjs";
import { default as parsers } from "./parsers";
 
type Plugins = (props: OutputBlockData) => {};
type Options = {
  strict: boolean;
};
 
const parse = (
  { blocks }: OutputData,
  parsers: Record<string, Plugins>,
  options: Options
) => {
  return blocks.reduce((accumlator: string, block: OutputBlockData) => {
    if (block.type in parsers) {
      accumlator += parsers[block.type](block);
      return accumlator;
    }
 
    const error = `[editorjs-html]: Parser function for ${block.type} does not exist`;
    if (options.strict) {
      throw new Error(error);
    } else {
      console.error(error);
    }
 
    return accumlator;
  }, "");
};
 
const parseBlock = (
  block: OutputBlockData,
  parsers: Record<string, Plugins>,
  options: Options
) => {
  Eif (block.type in parsers) {
    return parsers[block.type](block);
  }
 
  const error = `[editorjs-html]: Parser function for ${block.type} does not exist`;
  if (options.strict) {
    throw new Error(error);
  } else {
    console.error(error);
  }
};
 
const parser = (
  plugins: Record<string, Plugins> = {},
  options: Options = { strict: false }
) => {
  const combinedParsers = { ...parsers, ...plugins };
 
  return {
    parse: (blocks: OutputData) => parse(blocks, combinedParsers, options),
    parseBlock: (block: OutputBlockData) =>
      parseBlock(block, combinedParsers, options),
  };
};
 
export default parser;