|
| 1 | +--- |
| 2 | +title: Taro3 + react 项目如何使用 towxml 在微信小程序渲染和优化 markdown |
| 3 | +date: 2024-09-26T15:17:39Z |
| 4 | +slug: post-19 |
| 5 | +author: chaseFunny:https://github.com/chaseFunny |
| 6 | +tags: ["bug","微信小程序"] |
| 7 | +--- |
| 8 | + |
| 9 | +首先,我们参考官方教程,生成我们想要的 dist 文件 https://github.com/sbfkcel/towxml/wiki/3.0-%E6%9E%84%E5%BB%BATowxml |
| 10 | + |
| 11 | +然后,我们把文件放到我们项目 src 下,我们正常引入使用即可,这时候,我们会发现 towxml 尤两个问题: |
| 12 | + |
| 13 | +1. 代码没有高亮 |
| 14 | +2. 图片不能点击放大 |
| 15 | + |
| 16 | +我们解决一下这两个问题 |
| 17 | + |
| 18 | +## 代码不能高亮 |
| 19 | + |
| 20 | +这个问题解决其实非常简单,在解决前,我们先看一下为什么不生效,我们是可以发现 markdown 解析其他都是正常的,但就是代码高亮不生效,那么肯定就是代码高亮的样式没有生效,我们找一下代码高亮的样式,发现在这里: |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +发现这里是使用导入的方式,可鞥就是这个问题,我们只需要把对应的 wxss 代码复制过来,不使用导入的形式就好了, |
| 25 | + |
| 26 | +对了,如果你觉得默认的样式不好,那么我们还可以去 https://highlightjs.org/examples 找一个满意的样式,然后把对应的源码复制替换默认的即可 |
| 27 | + |
| 28 | +## 图片不能预览 |
| 29 | + |
| 30 | +这个不算是 bug,towxml 本身就是没有做这个能力,我们实现也很简单,towxml 提供了绑定事件的能力,我们只需要为图片绑定点击事件,然后为在点击的时候,调用微信的预览图片功能即可 |
| 31 | + |
| 32 | +```tsx |
| 33 | + <towxml |
| 34 | + nodes={towxml(value, "markdown", { |
| 35 | + events: { |
| 36 | + tap: (event) => { |
| 37 | + const dataSetData = safeGet( |
| 38 | + event, |
| 39 | + "currentTarget.dataset.data", |
| 40 | + "" |
| 41 | + ); |
| 42 | + if ( |
| 43 | + dataSetData && |
| 44 | + dataSetData.attrs && |
| 45 | + safeGet(dataSetData, "attrs.class", "") === "h2w__img" |
| 46 | + ) { |
| 47 | + const imgList = extractImageUrls(value, contentType); |
| 48 | + const src = safeGet(dataSetData, "attrs.src", ""); |
| 49 | + const previewImageList = |
| 50 | + imgList && imgList.length && imgList.includes(src) |
| 51 | + ? imgList |
| 52 | + : [src]; |
| 53 | + Taro.previewImage({ |
| 54 | + current: src, |
| 55 | + urls: previewImageList, |
| 56 | + }); |
| 57 | + } |
| 58 | + }, |
| 59 | + }, |
| 60 | + })} |
| 61 | +/> |
| 62 | +// 工具函数 |
| 63 | +/** |
| 64 | + * 安全取值函数 |
| 65 | + * @param object 目标对象 |
| 66 | + * @param path 属性路径 |
| 67 | + * @param defaultVal 默认值 |
| 68 | + * @returns 取得的属性值或默认值 |
| 69 | + */ |
| 70 | +export const safeGet = ( |
| 71 | + object: any, |
| 72 | + path: any, |
| 73 | + defaultVal: any = undefined |
| 74 | +): any => { |
| 75 | + let newPath: string[] = []; |
| 76 | + if (Array.isArray(path)) { |
| 77 | + newPath = path; |
| 78 | + } else { |
| 79 | + newPath = path.replace(/\[/g, ".").replace(/\]/g, "").split("."); |
| 80 | + } |
| 81 | + const result = newPath.reduce((o: any, k: string) => { |
| 82 | + return (o || {})[k]; |
| 83 | + }, object); |
| 84 | + if ([undefined, null, "undefined", "null", ""].includes(result)) { |
| 85 | + return defaultVal; |
| 86 | + } |
| 87 | + return result; |
| 88 | +}; |
| 89 | +/** |
| 90 | + * 从文本中提取图片链接 |
| 91 | + * @param {string} text 要解析的文本 |
| 92 | + * @param {string} type 文本类型,'markdown' 或 'html' |
| 93 | + * @returns {string[]} 图片链接数组 |
| 94 | + */ |
| 95 | +export const extractImageUrls = (text, type = 1) => { |
| 96 | + const imgUrls = []; |
| 97 | + let imgRegex; |
| 98 | + if (!text) { |
| 99 | + return imgUrls; |
| 100 | + } |
| 101 | + // 根据类型选择合适的正则表达式 |
| 102 | + if (type === 1) { |
| 103 | + imgRegex = /!\[.*?\]\((.*?)\)/g; |
| 104 | + } else if (type === 0) { |
| 105 | + imgRegex = /<img[^ />]*src\s*=\s*['"]?([^'">]+)['"]?[^>]*>/g; |
| 106 | + } else { |
| 107 | + throw new Error("Unsupported text type."); |
| 108 | + } |
| 109 | + // 使用正则表达式匹配图片链接并加入数组 |
| 110 | + let match; |
| 111 | + while ((match = imgRegex.exec(text)) !== null) { |
| 112 | + imgUrls.push(match[1]); |
| 113 | + } |
| 114 | + return imgUrls; |
| 115 | +}; |
| 116 | +``` |
| 117 | + |
| 118 | +好了,这就是在 Taro3 + React 项目中使用 towxml 的问题解决办法! |
| 119 | + |
| 120 | +--- |
| 121 | +此文自动发布于:<a href="https://github.com/coderPerseus/blog/issues/19" target="_blank">github issues</a> |
0 commit comments