微信小程序-富文本转文本

2020-05-29  本文已影响0人  Stonesy
//app.js
convertHtmlToText: function(inputText) {
        var returnText = "" + inputText;
        returnText = returnText.replace(/<\/div>/ig, '\r\n');
        returnText = returnText.replace(/<\/li>/ig, '\r\n');
        returnText = returnText.replace(/<li>/ig, '  *  ');
        returnText = returnText.replace(/<\/ul>/ig, '\r\n');
        //-- remove BR tags and replace them with line break
        returnText = returnText.replace(/<br\s*[\/]?>/g, "\r\n");
    
        //-- remove P and A tags but preserve what's inside of them
        returnText = returnText.replace(/<p.*?>/g, "\r\n");
        returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/g, " $2 ($1)");
    
        //-- remove all inside SCRIPT and STYLE tags
        returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/g, "");
        returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/g, "");
        //-- remove all else
        returnText = returnText.replace(/<(?:.|\s)*?>/g, "");
    
        //-- get rid of more than 2 multiple line breaks:
        returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/g, "\r\n\r\n");
    
        //-- get rid of more than 2 spaces:
        returnText = returnText.replace(/ +(?= )/g, '');
    
        //-- get rid of html-encoded characters:
        returnText = returnText.replace(/ /g, " ");
        returnText = returnText.replace(/&/g, "&");
        returnText = returnText.replace(/&quot;/g, '\'');
        returnText = returnText.replace(/&lt;/g, '<');
        returnText = returnText.replace(/&gt;/g, '>');
        // returnText = returnText.replace(/img/g, 'image');
    
        return returnText;
      },
/**
              * WxParse.wxParse(bindName , type, data, target,imagePadding)
              * 1.bindName绑定的数据名(必填)
              * 2.type可以为html或者md(必填)
              * 3.data为传入的具体数据(必填)
              * 4.target为Page对象,一般为this(必填)
              * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
              *
              * 6.parms.actAsk是一个html代码  
              */
             var neirong = app.convertHtmlToText('内容');
             var parms = JSON.parse("\""+neirong+"\"");
             WxParse.wxParse('article', 'html', parms, this, 0);  //将parms.actAsk转化wxml

<import src="../../../wxParse/wxParse.wxml" /><!--一定要导入否则不显示-->
 <scroll-view scrollY class="kcrzxybd" style="height: 600rpx;">
        <template is="wxParse" data="{{wxParseData:article.nodes}}"></template>
    </scroll-view>
上一篇下一篇

猜你喜欢

热点阅读