function calcIndent()

in base/services/trimIndentation.js [3:35]


function calcIndent(text) {
  const MAX_INDENT = 9999;
  const lines = text.split('\n');
  let minIndent = MAX_INDENT;
  let emptyLinesRemoved = false;

  // ignore leading empty lines
  while(isEmpty(lines[0])) {
    lines.shift();
    emptyLinesRemoved = true;
  }

  if(lines.length) {

    // ignore first line if it has no indentation and there is more than one line
    // this is because sometimes our text starts in the middle of a line of other
    // text that is indented and so doesn't appear to have an indent when it really does.
    const ignoreLine = (lines[0][0] != ' '  && lines.length > 1);
    if ( ignoreLine && !emptyLinesRemoved ) {
      lines.shift();
    }

    lines.forEach(line => {
      if ( !isEmpty(line) ) {
        const indent = line.match(/^\s*/)[0].length;
        minIndent = Math.min(minIndent, indent);
      }
    });

  }

  return minIndent;
}