uni-ai-msg.vue 1.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
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 51 52 53 54 55 56 57 58 59 60
<template>
	<view class="rich-text-box">
		<rich-text v-if="nodes&&nodes.length" space="nbsp" :nodes="nodes"></rich-text>
	</view>
</template>

<script>
	import parseHtml from './html-parser.js';
	const MarkdownIt = require('markdown-it')
	const hljs = require('highlight.js')
	const md = new MarkdownIt({
		html: false,
		highlight: function(str, lang) {
			if (lang && hljs.getLanguage(lang)) {
				try {
					return `<pre class="hljs"><code>${hljs.highlight(lang, str, true).value}</code></pre>`
				} catch (__) {}
			}
			return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
		}
	})
	export default {
		name: "msg",
		data() {
			return {

			};
		},
		props: {
			md: {
				type: String,
				default () {
					return '# H1 \r ```js' + '\r alert(1); \r```'
				}
			},
		},
		computed: {
			html() {
				// req.body.content 代表md代码
				return md.render(this.md)
			},
			nodes() {
				return parseHtml(this.html)
			}
		},
	}
</script>

<style lang="scss">
	@import "highlight.js/styles/agate.css";

	/* #ifndef APP-NVUE */
	.hljs code {
		padding: 5px;
		margin: 5px 0;
		display: flex;
	}

	/* #endif */
</style>