import { SEGMENT_EXTERNAL_IMAGE, SEGMENT_URL } from '../../data/enum/SegmentType';
import { IMAGE_REGEX } from '../../data/regexPatterns';
/**
* Detects External image urls in each SEGMENT_URL message segment provided to `input`. If a image
* url is detected, will convert it to a SEGMENT_EXTERNAL_IMAGE and attach a image object.
* @param input {Array<Object>} An array of message segments
* @returns {Array<Object>} The processed array of message segments
*/
const detectImageUrl = () => input =>
input.map(segment => {
const { type, data } = segment;
switch (type) {
case SEGMENT_URL: {
const match = data.url.match(IMAGE_REGEX);
if (match) {
return {
type: SEGMENT_EXTERNAL_IMAGE,
data: {
text: data.text,
url: data.url,
image: {
src: match[0],
alt: 'external image',
},
},
};
}
return segment;
}
default:
return segment;
}
});
export default detectImageUrl;