src/document/doc-debug.js/flattenDoc
function flattenDoc(doc) {
  if (!doc) {
    return "";
  }

  if (isConcat(doc)) {
    const res = [];
    for (const part of getDocParts(doc)) {
      if (isConcat(part)) {
        res.push(...flattenDoc(part).parts);
      } else {
        const flattened = flattenDoc(part);
        if (flattened !== "") {
          res.push(flattened);
        }
      }
    }

    return { type: "concat", parts: res };
  }

  if (doc.type === "if-break") {
    return {
      ...doc,
      breakContents: flattenDoc(doc.breakContents),
      flatContents: flattenDoc(doc.flatContents),
    };
  }

  if (doc.type === "group") {
    return {
      ...doc,
      contents: flattenDoc(doc.contents),
      expandedStates: doc.expandedStates && doc.expandedStates.map(flattenDoc),
    };
  }

  if (doc.type === "fill") {
    return { type: "fill", parts: doc.parts.map(flattenDoc) };
  }

  if (doc.contents) {
    return { ...doc, contents: flattenDoc(doc.contents) };
  }

  return doc;
}