import { URL } from 'url';
import getObject from './getObject';

const getImages = (result: any): string[] => {
  const origin = (new URL(result.url)).origin;

  return getObject(result.html, 'name', 'img')
    .filter((image: any) => {
      return Object.prototype.hasOwnProperty.call(image, 'attribs') && Object.prototype.hasOwnProperty.call(image.attribs, 'src');
    })
    .map((image: any) => {
      if (image.attribs.src.startsWith('./')) {
        if (result.url.endsWith('/')) {
          return result.url + image.attribs.src.substring(2);
        }
        return result.url + image.attribs.src.substring(1);
      }
      if (image.attribs.src.startsWith('/')) {
        return origin + image.attribs.src;
      }
      if (!image.attribs.src.startsWith('http')) {
        return origin + '/' + image.attribs.src;
      }

      return image.attribs.src;
    });
};

export default getImages;

import { URL } from 'url';
import getObject from './getObject';

const getImages = (result: any): string[] => {
  const origin = (new URL(result.url)).origin;

  return getObject(result.html, 'name', 'img')
    .filter((image: any) => {
      return Object.prototype.hasOwnProperty.call(image, 'attribs') && Object.prototype.hasOwnProperty.call(image.attribs, 'src');
    })
    .filter((image: any) => {
      return !image.attribs.src.startsWith('data:image')
    })
    .map((image: any) => {
      if (image.attribs.src.startsWith('./')) {
        if (result.url.endsWith('/')) {
          return result.url + image.attribs.src.substring(2);
        }
        return result.url + image.attribs.src.substring(1);
      }
      if (image.attribs.src.startsWith('/')) {
        return origin + image.attribs.src;
      }
      if (!image.attribs.src.startsWith('http')) {
        return origin + '/' + image.attribs.src;
      }

      return image.attribs.src;
    });
};

export default getImages;

Fix image check if base64
Fix image check if base64
