Generates display data, an object with key/value pairs, where each key is a mime type. Unless the value is null/undefined, a plain text representation is always produced if no other specific representation can be generated.
function createDisplayData(value) {
  var displayData = {};

  if ((value === null) || (value === undefined)) {
    return displayData;
  }

  var useFallbacks = true;

  if (typeof value.toHTML == 'function') {
    displayData['text/html'] = value.toHTML();
    useFallbacks = false;
  }

  if (typeof value.toScript == 'function') {
    displayData['application/javascript'] = value.toScript();
    useFallbacks = false;
  }

  if (typeof value.toImage == 'function') {
    var buffer = value.toImage();
    if (buffer) {
      var data = buffer.toString('base64');
      var mime = buffer.mime || 'image/png';

      displayData[mime] = data;
      useFallbacks = false;
    }
  }

  if (typeof value.toText == 'function') {
    var text = value.toText();
    if (text) {
      displayData['text/plain'] = text;
      useFallbacks = false;
    }
  }

  if (useFallbacks) {
    if ((value.constructor == Object) ||
        (value.constructor == Array)) {
      displayData['application/json'] = JSON.stringify(value);
    }
    else if (value.constructor == Buffer) {
      var mime = value.mime || 'application/octet-stream';
      displayData[mime] = value.toString('base64');
    }
    else {
      displayData['text/plain'] = value.toString();
    }
  }

  return displayData;
}