import { hasProvider , extract , VideoTypeData , setRequestOptions } from 'oembed-parser' ; import * as DOMPurify from 'dompurify' ; const buildDefaultIframe = ( url ) => { return `<iframe src=${ url } allow="fullscreen" style="height:100%;width:100%; aspect-ratio: 16 / 9;"></iframe>` ; } ; export const getIframeGeneratorFromSanitize = ( sanitize ) => async ( url ) : Promise < string > => { const defaultHtml = buildDefaultIframe ( url ) ; if ( ! hasProvider ( url ) ) { return defaultHtml ; } try { const oembedData = await extract ( url ) ; if ( oembedData && oembedData . type !== 'rich' && oembedData . type !== 'video' ) { throw new Error ( 'Not a rich or video type:' + oembedData ) ; } const html = ( oembedData as VideoTypeData ) ?. html ; const cleanedHtml = sanitize ( html , { ALLOWED_TAGS : [ "iframe" ] } ) ; if ( ! cleanedHtml . startsWith ( '<iframe' ) ) { throw new Error ( 'Not an iframe, we currently do not support this: ' + html ) ; } return cleanedHtml ; } catch ( e ) { console . warn ( `Could not get oembed data for ${ url }` , e ) ; return defaultHtml ; } } ; export const getIframe = getIframeGeneratorFromSanitize ( DOMPurify . sanitize ) ;