/**
 * @param {Array<UfmPlugin>} plugins - An array of UFM plugins.
 * @returns {MarkedExtension} A Marked extension object.
 */
export function ufm(plugins = []) {
    return {
        extensions: plugins.map(({ alias, marker, render }) => {
            const prefix = `(${alias}:${marker ? `|${marker}` : ''})`;
            const startPattern = new RegExp(`\\{\\s*${prefix}`);
            return {
                name: alias,
                level: 'inline',
                start: (src) => src.search(startPattern),
                tokenizer: (src) => {
                    const pattern = `^\\{\\s*${prefix}([^}]*)\\}`;
                    const regex = new RegExp(pattern);
                    const match = src.match(regex);
                    if (match) {
                        const [raw, prefix, content = ''] = match;
                        return {
                            type: alias,
                            raw: raw,
                            tokens: [],
                            prefix: prefix,
                            text: content.trim(),
                        };
                    }
                    return undefined;
                },
                renderer: render,
            };
        }),
    };
}
