formatContentfulRichText static method

List formatContentfulRichText(
  1. List content,
  2. List assets,
  3. List entries
)

Implementation

static List formatContentfulRichText(List<dynamic> content, List<dynamic> assets, List<dynamic> entries) {
  return content
      .fold([], (acc, _) {
        dynamic value;
        switch (_['nodeType']) {
          case 'text':
            value = _['value'];
            break;
          case 'hyperlink':
            value = '${_['data']['uri']}';
            break;
          case 'ordered-list':
          case 'unordered-list':
          case 'list-item':
          case 'paragraph':
            value = _['content'].isNotEmpty
                ? formatContentfulRichText(_['content'], assets, entries).fold('', (acc, __) => '$acc$__')
                : formatContentfulRichText(_['content'], assets, entries);
            break;
          case 'embedded-entry-block':
            value = {
              'image':
                  'https:${assets.where((__) => __['sys']['id'] == entries.where((__) => __['sys']['id'] == _['data']['target']['sys']['id']).first['fields']['image']['sys']['id']).first['fields']['file']['url']}',
              'link': '${entries.where((__) => __['sys']['id'] == _['data']['target']['sys']['id']).first['fields']['link']}'
            };
            break;
          case 'embedded-asset-block':
            value = {
              'image':
                  'https:${assets.where((__) => __['sys']['id'] == _['data']['target']['sys']['id']).first['fields']['file']['url']}'
            };
            break;
          default:
            break;
        }
        if (acc == null) {
          return [value];
        }
        return [...{acc}, value];
      })
      .where((_) => _ != null && _.length > 0)
      .toList();
}