当前位置:网站首页>Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
2022-07-25 16:22:00 【Bitter summer Bluegrass】
Applet native rich-text Rendering video is not supported , So it needs to be displayed after processing , It mainly uses string cutting and matching
The problem of rich text image adaptation
When we get rich text data from the backend , We make use of replace Method to modify the style of the picture , Make sure the picture is consistent with the width of the phone
//richtext Is rich text
let content = richtext.replace(/<img/gi, '<img style="max-width:100%;height:auto"') // Picture adaptation
If there are inexplicable blank areas in rich text , It may be that the rich text contains a newline label , We will <br> Label replaced
//richtext For the rich text passed from the back end
dealRichText(richtext) {
let content = richtext.replace(/<img/gi, '<img style="max-width:100%;height:auto"').replace(/<br\/>/g, '');
return content;
}
Rendering video
There are also many ways online , But my rich text is generated by Baidu rich text editor , It's a little complicated , That's true :
<p>
Casual words
</p>
<p>
<img src="https://weiqing.kuxia.top/uploads/20220720/0fa5869614f227c88c7c89737836814c.png" title="" alt=""/>
</p>
<p>
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
</p>
<p style="white-space: normal;">
Casual words
</p>
<p style="white-space: normal;">
<img src="https://xxx.xxx.top/uploads/20220720/0fa5869614f227c88c7c89737836814c.png" title="" alt=""/>
</p>
<p style="white-space: normal;">
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
<video class="edui-upload-video vjs-default-skin video-js" controls="" preload="none" width="420" height="280" src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" data-setup="{}">
<source src="https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4" type="video/mp4"/>
</video>
</p>
<p>
<br/>
</p>
This is my casual call , The video link has also changed ( Project confidentiality ), Structurally, each p A tag is a paragraph , There may be multiple videos in a paragraph , Treatment scheme :
1、 Handle
dealRichText(richtext) {
let content = richtext.replace(/<img/gi, '<img style="max-width:100%;height:auto"').replace(/<br\/>/g, ''); // Replace the width and height of the picture
let textArr = content.split(/<p/); // Split the string by paragraph
let contentArr = [];
// Prevent one or more videos , For cutting
textArr.forEach(val => {
let arr = val.split(/<video/)
contentArr.push(...arr)
})
let newArr = []; // Processed array
contentArr.forEach((val, i) => {
if (val != '' && val != ">") {
// Prevent empty strings
let matchResult = val.match(/(?<= class=\"edui-upload-video).+(?=\<\/video\>)/); // Whether there is video in the matching string
if (matchResult) {
let a = matchResult[0].match(/(?<=src=\").+(?=\" data)/); // Match video links
newArr.push({
type: 'video',
content: a[0]
})
} else {
newArr.push({
type: 'text',
content: "<p" + val
})
}
}
})
return newArr;
},
The final result ( Array ):
0: {
type: "text", content: "<p> Casual words </p>"}
1: {
type: "text", content: "<p><img style="max-width:100%;height:auto" src="ht…f227c88c7c89737836814c.png" title="" alt=""/></p>"}
2: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
3: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
4: {
type: "text", content: "<p style="white-space: normal;"> Casual words </p>"}
5: {
type: "text", content: "<p style="white-space: normal;"><img style="max-wi…f227c88c7c89737836814c.png" title="" alt=""/></p>"}
6: {
type: "text", content: "<p style="white-space: normal;">"}
7: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
8: {
type: "video", content: "https://xxx.xxx.top/uploads/20220720/888a8ab9fdafa65bca08375fb4f6fb1f.mp4"}
9: {
type: "text", content: "<p></p>"}
There are still some minor flaws in the data , But it doesn't affect the use
2、 Rendering
<!-- config.remark It's the last data , That is, the array above -->
<view wx:for="{
{config.remark}}" wx:key="index">
<rich-text nodes="{
{item.content}}" wx:if="{
{item.type == 'text'}}"></rich-text>
<video src="{
{item.content}}" wx:if="{
{item.type == 'video'}}" style="width: 100%;" controls></video>
</view>
边栏推荐
- How does win11's own drawing software display the ruler?
- 【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
- 解决Win10磁盘占用100%
- Is the win11 dynamic tile gone? Method of restoring dynamic tile in Win 11
- Waterfall flow layout
- MYSQL导入sqllite表格的两种方法
- 记得那两句话
- doGet与doPost
- R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
- MySQL metadata lock (MDL)
猜你喜欢

使用 Terraform 在 AWS 上快速部署 MQTT 集群

Recommended collection, which is probably the most comprehensive coding method summary of category type features

0x80131500打不开微软商店的解决办法

【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码

吴恩达逻辑回归2

Verifiable random function VRF

论文笔记:Highly accurate protein structure prediction with AlphaFold (AlphaFold 2 & appendix)
![[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code](/img/2a/b5214e9fa206f1872293c9b9d7bdb6.png)
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code

终极套娃 2.0 | 云原生交付的封装

Talk about how to use redis to realize distributed locks?
随机推荐
What is the shortcut key for win11 Desktop Switching? Win11 fast desktop switching method
【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
MySQL explicit lock
微信小程序不使用插件,渲染富文本中的视频,图片自适应,plus版本
Recommended collection, which is probably the most comprehensive coding method summary of category type features
Understanding service governance in distributed development
01. A simpler way to deliver a large number of props
Upgrade esxi6.7.0 to 7.0u3f (updated on July 12, 2022)
mysql 隔离级别事务
共享锁(Shared Lock)
The second revolution of reporting tools
可验证随机函数 VRF
哪个led显示屏厂家更好
Record Locks(记录锁)
MySQL intent lock
今天睡眠质量记录84分
R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
mysql 表读锁
Leetcode:528. select randomly according to the weight [ordinary random failure + prefix and dichotomy]