<commit_before>import http from "axios";
import { Element } from "./workspace/model";


export interface Embedder {
    "name": string,
    "displayName": string,
    "url": string[],
    "logo": string,
    "embed": string
    "example": string
}
export const embedderService = {
    _providers: null as Embedder[],
    async    getProviders() {
        if (embedderService._providers != null) {
            return embedderService._providers;
        }
        const providers = (await http.get('/infra/embed/default')).data;
        const customProviders = (await http.get('/infra/embed/custom')).data;
        for (const custom of customProviders) {
            custom.name = (custom.name || "").toLowerCase().replace(/\ |\:|\?|#|%|\$|£|\^|\*|€|°|\(|\)|\[|\]|§|'|"|&|ç|ù|`|=|\+|<|@/g, '')
        }
        embedderService._providers = [...providers, ...customProviders];
        return embedderService._providers;
    },
    getHtmlForVideoStream(document:Element){
        return `<video controls class="render" src="${document.documentUrl}"></video>`
    },
    getHtmlForVideoStreams(documents:Element[]){
        return documents.map(d=>embedderService.getHtmlForVideoStream(d)).join(' ');
    },
    async getHtmlForUrl(url: string, returnDefault: boolean = false) {
        const providers = await embedderService.getProviders();
        for (const p of providers) {
            const html = embedderService.getHtmlForProvider(p, url);
            if(html && html.length > 0){
                return html;
            }
        }
        if(returnDefault){
            return `<iframe width="560" height="315" src="${url}" frameborder="0" allowfullscreen></iframe>`
        }
        return "";
    },
    getHtmlForProvider(embedder: Embedder, url: string): string {
        for (let pattern of embedder.url) {
            const matchParams = new RegExp('\{[a-zA-Z0-9_.]+\}', "g");
            const params = pattern.match(matchParams);
            const computedEmbed = embedder.embed;

            params.splice(1, params.length);
            for (const param of params) {
                let paramBefore = pattern.split(param)[0];
                const additionnalSplit = paramBefore.split('}')
                if (additionnalSplit.length > 1) {
                    paramBefore = additionnalSplit[additionnalSplit.length - 1];
                }
                let paramValue = url.split(paramBefore)[1];
                if (!paramValue) {
                    continue;
                }
                const paramAfter = pattern.split(param)[1].split('{')[0];
                if (paramAfter) {
                    paramValue = paramValue.split(paramAfter)[0];
                }

                const replace = new RegExp('\\' + param.replace(/}/, '\\}'), 'g');
                return computedEmbed.replace(replace, paramValue);
            };
        }
        return "";
    }
}

if (!window.entcore) {
    window.entcore = {};
}
window.entcore.embedderService = embedderService;<commit_msg>fix(embedder): #34989, Avoid error when embeder pattern dont match<commit_after>import http from "axios";
import { Element } from "./workspace/model";


export interface Embedder {
    "name": string,
    "displayName": string,
    "url": string[],
    "logo": string,
    "embed": string
    "example": string
}
export const embedderService = {
    _providers: null as Embedder[],
    async    getProviders() {
        if (embedderService._providers != null) {
            return embedderService._providers;
        }
        const providers = (await http.get('/infra/embed/default')).data;
        const customProviders = (await http.get('/infra/embed/custom')).data;
        for (const custom of customProviders) {
            custom.name = (custom.name || "").toLowerCase().replace(/\ |\:|\?|#|%|\$|£|\^|\*|€|°|\(|\)|\[|\]|§|'|"|&|ç|ù|`|=|\+|<|@/g, '')
        }
        embedderService._providers = [...providers, ...customProviders];
        return embedderService._providers;
    },
    getHtmlForVideoStream(document:Element){
        return `<video controls class="render" src="${document.documentUrl}"></video>`
    },
    getHtmlForVideoStreams(documents:Element[]){
        return documents.map(d=>embedderService.getHtmlForVideoStream(d)).join(' ');
    },
    async getHtmlForUrl(url: string, returnDefault: boolean = false) {
        const providers = await embedderService.getProviders();
        for (const p of providers) {
            const html = embedderService.getHtmlForProvider(p, url);
            if(html && html.length > 0){
                return html;
            }
        }
        if(returnDefault){
            return `<iframe width="560" height="315" src="${url}" frameborder="0" allowfullscreen></iframe>`
        }
        return "";
    },
    getHtmlForProvider(embedder: Embedder, url: string): string {
        for (let pattern of embedder.url) {
            const matchParams = new RegExp('\{[a-zA-Z0-9_.]+\}', "g");
            const params = pattern.match(matchParams) || [];
            const computedEmbed = embedder.embed;

            params.splice(1, params.length);
            for (const param of params) {
                let paramBefore = pattern.split(param)[0];
                const additionnalSplit = paramBefore.split('}')
                if (additionnalSplit.length > 1) {
                    paramBefore = additionnalSplit[additionnalSplit.length - 1];
                }
                let paramValue = url.split(paramBefore)[1];
                if (!paramValue) {
                    continue;
                }
                const paramAfter = pattern.split(param)[1].split('{')[0];
                if (paramAfter) {
                    paramValue = paramValue.split(paramAfter)[0];
                }

                const replace = new RegExp('\\' + param.replace(/}/, '\\}'), 'g');
                return computedEmbed.replace(replace, paramValue);
            };
        }
        return "";
    }
}

if (!window.entcore) {
    window.entcore = {};
}
window.entcore.embedderService = embedderService;
{'fraction_non_alphanumeric': 0.15122593384893862, 'fraction_numerical': 0.00674675004113872, 'mean_word_length': 2.1475919212843086, 'pattern_counts': {'":': 12, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2803981', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1693, 'n_words': 345}