render.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. function renderRichText(body) {
  2. let sections = body.sections || [];
  3. let html = [];
  4. sections.forEach(section => {
  5. html.push(renderSection(section));
  6. });
  7. return html.join('');
  8. }
  9. function renderSection(section) {
  10. let blocks = section.blocks || [];
  11. let inline = blocks.length > 1;
  12. let html = [];
  13. blocks.forEach(block => {
  14. html.push(renderBlock(block, inline));
  15. });
  16. return '<p>' + html.join('') + '</p>';
  17. }
  18. function renderBlock(block, inline) {
  19. let type = '';
  20. let inner = '';
  21. if (block.type === 'text') {
  22. type = 'text';
  23. inner = block.value;
  24. } else if (block.type === 'image') {
  25. type = 'image';
  26. if (inline === true) {
  27. type += ' inline';
  28. }
  29. // inner = '<img src="' + block.value + '" width="' + block.param.width + '" height="' + block.param.height + '"/>';
  30. inner = '<img src="' + block.value + '"/>';
  31. } else if (block.type === 'audio') {
  32. type = 'audio';
  33. inner = '<audio controls><source src="' + block.value + '" type="audio/mpeg"></audio>';
  34. }
  35. return '<span class="' + type + '">' + inner + '</span>';
  36. }