UnisKB/ui/src/components/markdown/MdRenderer.vue

49 lines
1.4 KiB
Vue
Raw Normal View History

2023-12-26 04:04:24 +00:00
<template>
<MdPreview
2024-07-01 01:45:59 +00:00
noIconfont
2023-12-26 04:04:24 +00:00
ref="editorRef"
editorId="preview-only"
:modelValue="item"
2023-12-26 08:56:14 +00:00
v-for="(item, index) in md_view_list"
:key="index"
2024-03-07 02:53:52 +00:00
class="maxkb-md"
2023-12-26 04:04:24 +00:00
/>
</template>
<script setup lang="ts">
2024-05-07 02:34:38 +00:00
import { computed, ref } from 'vue'
2024-07-01 01:45:59 +00:00
import { config } from 'md-editor-v3'
2024-03-14 11:23:35 +00:00
config({
2024-05-07 02:34:38 +00:00
markdownItConfig(md) {
2024-03-14 11:23:35 +00:00
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
tokens[idx].attrSet('target', '_blank')
return md.renderer.renderToken(tokens, idx, options)
}
}
})
2023-12-26 04:04:24 +00:00
const props = withDefaults(defineProps<{ source?: string; inner_suffix?: boolean }>(), {
source: ''
})
2024-03-07 02:53:52 +00:00
const editorRef = ref()
2023-12-26 04:04:24 +00:00
const md_view_list = computed(() => {
const temp_source = props.source
const temp_md_img_list = temp_source.match(/(!\[.*?\]\(img\/.*?\){.*?})|(!\[.*?\]\(img\/.*?\))/g)
const md_img_list = temp_md_img_list ? temp_md_img_list.filter((i) => i) : []
const split_img_value = temp_source
.split(/(!\[.*?\]\(img\/.*?\){.*?})|(!\[.*?\]\(img\/.*?\))/g)
.filter((item) => item !== undefined)
.filter((item) => !md_img_list?.includes(item))
const result = Array.from(
{ length: md_img_list.length + split_img_value.length },
(v, i) => i
).map((index) => {
if (index % 2 == 0) {
return split_img_value[Math.floor(index / 2)]
} else {
return md_img_list[Math.floor(index / 2)]
}
})
return result
})
</script>
<style lang="scss" scoped></style>