images.js 867 B

12345678910111213141516171819202122232425262728293031
  1. var _ = require("underscore");
  2. var promises = require("./promises");
  3. var Html = require("./html");
  4. exports.imgElement = imgElement;
  5. function imgElement(func) {
  6. return function(element, messages) {
  7. return promises.when(func(element)).then(function(result) {
  8. var attributes = {};
  9. if (element.altText) {
  10. attributes.alt = element.altText;
  11. }
  12. _.extend(attributes, result);
  13. return [Html.freshElement("img", attributes)];
  14. });
  15. };
  16. }
  17. // Undocumented, but retained for backwards-compatibility with 0.3.x
  18. exports.inline = exports.imgElement;
  19. exports.dataUri = imgElement(function(element) {
  20. return element.readAsBase64String().then(function(imageBuffer) {
  21. return {
  22. src: "data:" + element.contentType + ";base64," + imageBuffer
  23. };
  24. });
  25. });