All files / src/lib/block-info parse.ts

13.33% Statements 2/15
0% Branches 0/8
0% Functions 0/1
13.33% Lines 2/15

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  1x   1x                                                                
import { BlockInfo } from ".";
import { parseAttributes } from "../attributes";
 
export default function(raw: string): BlockInfo {
  let language;
  let attributesAsString: string;
  let attributes: object;
  const trimmedParams = raw.trim();
  const match =
    trimmedParams.indexOf("{") !== -1
      ? trimmedParams.match(/^([^\s\{]*)\s*\{(.*?)\}/)
      : trimmedParams.match(/^([^\s]+)\s+(.+?)$/);
 
  if (match) {
    if (match[1].length) {
      language = match[1];
    }
    attributesAsString = match[2];
  } else {
    language = trimmedParams;
    attributesAsString = "";
  }
 
  if (attributesAsString) {
    try {
      attributes = parseAttributes(attributesAsString);
    } catch (e) {
      //
    }
  } else {
    attributes = {};
  }
 
  return { language, attributes };
}