Skip to content

feat: 支持通过html_rander标签 支持script执行 #1173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions ui/src/components/markdown/HtmlRander.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<div ref="htmlRef" :innerHTML="source"></div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
const htmlRef = ref<HTMLElement>()
const props = withDefaults(
defineProps<{
source?: string
script_exec?: boolean
}>(),
{
source: '',
script_exec: true
}
)
onMounted(() => {
if (htmlRef.value && props.script_exec) {
const range = document.createRange()
range.selectNode(htmlRef.value)
const scripts = htmlRef.value.getElementsByTagName('script')
if (scripts) {
var documentFragment = range.createContextualFragment(
[...scripts]
.map((item: HTMLElement) => {
htmlRef.value?.removeChild(item)
return item.outerHTML
})
.join('\n')
)
htmlRef.value.appendChild(documentFragment)
}
}
})
</script>
<style lang="scss" scoped></style>
5 changes: 4 additions & 1 deletion ui/src/components/markdown/MdRenderer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<el-icon><EditPen /></el-icon>
{{ item.content }}
</div>
<div v-else-if="item.type === 'html_rander'" :innerHTML="item.content"></div>
<HtmlRander v-else-if="item.type === 'html_rander'" :source="item.content"></HtmlRander>

<MdPreview
v-else
noIconfont
Expand All @@ -24,12 +25,14 @@
<script setup lang="ts">
import { computed, ref } from 'vue'
import { config } from 'md-editor-v3'
import HtmlRander from './HtmlRander.vue'
config({
markdownItConfig(md) {
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
tokens[idx].attrSet('target', '_blank')
return md.renderer.renderToken(tokens, idx, options)
}
document.appendChild
}
})
const props = withDefaults(
Expand Down
Loading