1234567891011121314151617181920212223242526272829303132333435363738 |
- function renderRichText(body) {
- let sections = body.sections || [];
- let html = [];
- sections.forEach(section => {
- html.push(renderSection(section));
- });
- return html.join('');
- }
- function renderSection(section) {
- let blocks = section.blocks || [];
- let inline = blocks.length > 1;
- let html = [];
- blocks.forEach(block => {
- html.push(renderBlock(block, inline));
- });
- return '<p>' + html.join('') + '</p>';
- }
- function renderBlock(block, inline) {
- let type = '';
- let inner = '';
- if (block.type === 'text') {
- type = 'text';
- inner = block.value;
- } else if (block.type === 'image') {
- type = 'image';
- if (inline === true) {
- type += ' inline';
- }
- // inner = '<img src="' + block.value + '" width="' + block.param.width + '" height="' + block.param.height + '"/>';
- inner = '<img src="' + block.value + '"/>';
- } else if (block.type === 'audio') {
- type = 'audio';
- inner = '<audio controls><source src="' + block.value + '" type="audio/mpeg"></audio>';
- }
- return '<span class="' + type + '">' + inner + '</span>';
- }
|