mip-utils.js 3.48 KB
const cheerio = require('cheerio');

module.exports = {
    /**
     * 分离 CSS
     * @param {*} $
     * @param {*} prefix
     */
    processStyle($, prefix) {
        let css = [];

        $('*').each(function(index) {
            let $this = $(this);
            let localStyle = $this.attr('style');
            let className = `yoho-inline-style-${prefix}-${index}`;

            if (localStyle) {
                css.push(`.${className}{${localStyle}}`);
                $this.removeAttr('style');
                $this.addClass(className);
            }
        });
        return css.join('');
    },

    /**
     * 替换标签
     * @param {*} $
     */
    processTag($) {
        let css = [];

        // mip-anim
        $('img[src*=".gif"]').each(function() {
            let $this = $(this);
            let mipAnim = `<mip-anim layout="responsive" width="350" height="263" src="${$this.attr('src')}" alt="${$this.attr('alt') || ''}" class="${$this.attr('class') || ''}"></mip-anim>`; // eslint-disable-line

            $this.replaceWith(mipAnim);
        });

        // mip-img
        $('img').each(function() {
            let $this = $(this);
            let mipImg = `<mip-img layout="responsive" width="350" height="263" src="${$this.attr('src')}" alt="${$this.attr('alt') || ''}" class="${$this.attr('class') || ''}"></mip-img>`; // eslint-disable-line

            $this.replaceWith(mipImg);
        });

        // mip-video
        $('video').each(function() {
            let $this = $(this);
            let mipVideo = `<mip-video layout="responsive" width="350" height="263" poster="${$this.attr('poster') || ''}" src="${$this.attr('src')}" alt="${$this.attr('alt') || ''}" class="${$this.attr('class') || ''}"></mip-video>`; // eslint-disable-line

            $this.replaceWith(mipVideo);
        });

        // mip-audio
        $('audio').each(function() {
            let $this = $(this);
            let mipAudio = `<mip-audio src="${$this.attr('src')}"></mip-audio>`; // eslint-disable-line

            $this.replaceWith(mipAudio);
        });

        // mip-iframe
        $('iframe').each(function() {
            let $this = $(this);
            let mipIframe = `<mip-iframe layout="responsive" width="350" height="263" src="${$this.attr('src')}" class="${$this.attr('class') || ''}"></mip-iframe>`; // eslint-disable-line

            $this.replaceWith(mipIframe);
        });

        // mip-link
        // $('a').each(function() {
        //     let $this = $(this);
        //     let mipLink = `<mip-link href="${$this.attr('href')}" class="${$this.attr('class') || ''}"
        //                    title="${$this.attr('title') || ''}">${$this.html()}</mip-link>`; // eslint-disable-line
        //     $this.replaceWith(mipLink);
        // });

        // style
        $('style').each(function() {
            let $this = $(this);

            css.push($this.html());
            $this.remove();
        });

        return {
            mipHtml: $.html(),
            css: css.join('')
        };
    },

    /**
     * 处理 HTML 为 MIP 所需要的格式
     * @param {*} html HTML
     * @param {*} prefix  CSS 前缀
     */

    process(html = '', prefix = 0) {
        let $ = cheerio.load(html, {
            decodeEntities: false
        });
        let inlineStyle = this.processStyle($, prefix); // 必须先处理内联样式
        let tagHtml = this.processTag($);

        return {
            mipHtml: tagHtml.mipHtml,
            css: tagHtml.css + inlineStyle
        };
    }
};