function trim(docs, isWhitespace) {
    trimLeft(docs, isWhitespace);
    trimRight(docs, isWhitespace);
    return docs;
}
Trims both leading and trailing nodes matching `isWhitespace` independent of nesting level
(though all trimmed adjacent nodes need to be a the same level). Modifies the `docs` array.