Skip to content

Commit e9d3d86

Browse files
committed
feat: 支持通过html_rander标签 支持script执行
1 parent b3a3025 commit e9d3d86

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<template>
2+
<div ref="htmlRef" :innerHTML="source"></div>
3+
</template>
4+
<script setup lang="ts">
5+
import { onMounted, ref } from 'vue'
6+
const htmlRef = ref<HTMLElement>()
7+
const props = withDefaults(
8+
defineProps<{
9+
source?: string
10+
script_exec?: boolean
11+
}>(),
12+
{
13+
source: '',
14+
script_exec: true
15+
}
16+
)
17+
onMounted(() => {
18+
if (htmlRef.value && props.script_exec) {
19+
const range = document.createRange()
20+
range.selectNode(htmlRef.value)
21+
const scripts = htmlRef.value.getElementsByTagName('script')
22+
if (scripts) {
23+
var documentFragment = range.createContextualFragment(
24+
[...scripts]
25+
.map((item: HTMLElement) => {
26+
htmlRef.value?.removeChild(item)
27+
return item.outerHTML
28+
})
29+
.join('\n')
30+
)
31+
htmlRef.value.appendChild(documentFragment)
32+
}
33+
}
34+
})
35+
</script>
36+
<style lang="scss" scoped></style>

ui/src/components/markdown/MdRenderer.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<el-icon><EditPen /></el-icon>
1010
{{ item.content }}
1111
</div>
12-
<div v-else-if="item.type === 'html_rander'" :innerHTML="item.content"></div>
12+
<HtmlRander v-else-if="item.type === 'html_rander'" :source="item.content"></HtmlRander>
13+
1314
<MdPreview
1415
v-else
1516
noIconfont
@@ -24,12 +25,14 @@
2425
<script setup lang="ts">
2526
import { computed, ref } from 'vue'
2627
import { config } from 'md-editor-v3'
28+
import HtmlRander from './HtmlRander.vue'
2729
config({
2830
markdownItConfig(md) {
2931
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
3032
tokens[idx].attrSet('target', '_blank')
3133
return md.renderer.renderToken(tokens, idx, options)
3234
}
35+
document.appendChild
3336
}
3437
})
3538
const props = withDefaults(

0 commit comments

Comments
 (0)