需求
需要后台上传文件,然后在小程序展示文件内容。
实现方案,文档使用Markdown,通过Golang,读取文档内容,转换为html格式。
然后再通过Api接口传给微信端。微信通过rich-text组件,显示html格式的文件内容。
Markdown转html
使用github.com/russross/blackfriday/v2
转换为html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import ( "fmt" "github.com/russross/blackfriday/v2" "html/template" "io/ioutil" )
func main() { f, err := ioutil.ReadFile("./README.md") if err != nil { fmt.Println(err.Error()) } content := template.HTML(blackfriday.Run(f)) fmt.Println(content) }
|
README.md
内容
hello world
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| | 标题 | 内容 | 描述 | | ------ | ---------------- | -------- | | title | content | desc |
# 告知函 **访客**您好:
您已进入公司办公区,应当遵守我司如下要求:
- 未经许可禁止拍照、录音录像;
- 未经许可禁止带走涉及公司的纸质或电子资料;
|
微信小程序
使用小程序组件rich-text
index.wxml
1 2 3
| <view> <rich-text nodes="{{msg}}"></rich-text> </view>
|
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| // logs.js
var msg = ` <h1>标题一</h1>
<h2>标题二</h2>
<h3>标题三</h3>
<pre><code>hello world </code></pre>
<table> <thead> <tr> <th>标题</th> <th>内容</th> <th>描述</th> </tr> </thead>
<tbody> <tr> <td>title</td> <td>content</td> <td>desc</td> </tr> </tbody> </table>
<h1>告知函</h1>
<p><strong>访客</strong>您好:</p>
<p>您已进入公司办公区,应当遵守我司如下要求:</p>
<ul> <li><p>未经许可禁止拍照、录音录像;</p></li>
<li><p>未经许可禁止带走涉及公司的纸质或电子资料;</p></li> </ul> ` Page({ data: { msg: msg, }, onLoad() { } })
|
显示效果如下

参考文档
https://www.cnblogs.com/ibgo/p/8484849.html
https://developers.weixin.qq.com/miniprogram/dev/component/rich-text.html