diff --git a/src/lib/images.ts b/src/lib/images.ts index ef0559b..5aa7d2c 100644 --- a/src/lib/images.ts +++ b/src/lib/images.ts @@ -135,98 +135,7 @@ function getImages(doc: unknown, targetWidths: number[] = DEFAULT_TARGET_WIDTHS) }; } - if (isLegacyTerminusImageDocument(doc)) { - const images = doc.media.image.primary.complete; - const { title, alt, id: cmid, caption, attribution, canonicalURL } = doc; - return { - alt, - cmid, - title, - caption, - attribution, - canonicalURL, - renditions: images.map(({ ratio, width, height, url }) => { - return { - ratio: ratio || getRatioString(width, height), - width, - height, - url, - isUndersizedBinary: false - }; - }) - }; - } - throw new Error('The document passed was not a valid terminus image document'); } -/* - * ======================================================== - * Stuff below here is transitional and can be removed once - * the transition to CM10 and Terminus v2 is complete. - * ======================================================== - */ -type LegacyTerminusImageDocument = { - id: string; - title?: string; - alt?: string; - caption?: string; - attribution?: string; - canonicalURL: string; - media: { - image: { - primary: { - complete: LegacyTerminusImageDocumentImages; - }; - }; - }; -}; - -type LegacyTerminusImageDocumentImages = LegacyTerminusImageDocumentImage[]; -type LegacyTerminusImageDocumentImage = { - width: number; - height: number; - url: string; - ratio?: string; -}; - -const greatestCommonFactor = (a: number, b: number) => { - while (a) { - const t = a; - a = b % a; - b = t; - } - return b; -}; - -const getRatioString = (width: number, height: number) => { - const gcf = greatestCommonFactor(width, height); - return `${width / gcf}x${height / gcf}`; -}; - -const isLegacyTerminusImageDocument = (obj: unknown): obj is LegacyTerminusImageDocument => { - if (!isObject(obj)) return false; - if (obj.docType !== 'Image' && obj.docType !== 'ImageProxy' && obj.docType !== 'CustomImage') return false; - if (typeof obj.id !== 'string') return false; - if (!isObject(obj.media)) return false; - if (!isObject(obj.media.image)) return false; - if (!isObject(obj.media.image.primary)) return false; - if (!isLegacyTerminusImageDocumentImages(obj.media.image.primary.complete)) return false; - return true; -}; - -const isLegacyTerminusImageDocumentImage = (obj: unknown): obj is LegacyTerminusImageDocumentImage => { - if (!isObject(obj)) return false; - if (typeof obj.width !== 'number') return false; - if (typeof obj.height !== 'number') return false; - if (typeof obj.url !== 'string') return false; - return true; -}; - -const isLegacyTerminusImageDocumentImages = (obj: unknown): obj is LegacyTerminusImageDocumentImages => { - if (!Array.isArray(obj)) return false; - if (!obj.every(isLegacyTerminusImageDocumentImage)) return false; - return true; -}; - export { getImages };