refactor: 调整components目录
- 将components根据作用范围/可复用性进行分类
This commit is contained in:
111
app/components/shared/MarkdownRenderer.vue
Normal file
111
app/components/shared/MarkdownRenderer.vue
Normal file
@ -0,0 +1,111 @@
|
||||
<!-- eslint-disable vue/no-v-html -->
|
||||
<template>
|
||||
<!-- 用 v-html 渲染解析后的 HTML -->
|
||||
<div ref="container" class="markdown-body" v-html="safeHtml" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { createApp } from 'vue';
|
||||
import MarkdownTable from './MarkdownTable.vue';
|
||||
interface Props {
|
||||
content: string;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
|
||||
const contentWithAbsoluteUrls = convertMedia(props.content);
|
||||
|
||||
// 将 Markdown 转换成 HTML
|
||||
const safeHtml = computed(() => renderMarkdown(contentWithAbsoluteUrls));
|
||||
// const safeHtml = computed(() => renderMarkdown(props.content))
|
||||
|
||||
const container = ref<HTMLElement | null>(null);
|
||||
|
||||
onMounted(async () => {
|
||||
await nextTick();
|
||||
if (!safeHtml.value) return;
|
||||
console.log(safeHtml.value);
|
||||
|
||||
// 查找所有 table
|
||||
const tables = container.value.querySelectorAll('table');
|
||||
console.log(tables);
|
||||
tables.forEach((table) => {
|
||||
// 1. 提取表头
|
||||
const headers = Array.from(table.querySelectorAll('thead th')).map(
|
||||
(th) => th.textContent?.trim() ?? ''
|
||||
);
|
||||
|
||||
// 2. 提取行数据
|
||||
const rows = Array.from(table.querySelectorAll('tbody tr')).map((tr) => {
|
||||
const cells = Array.from(tr.querySelectorAll('td')).map(
|
||||
(td) => td.textContent?.trim() ?? ''
|
||||
);
|
||||
const obj: Record<string, string> = {};
|
||||
headers.forEach((h, i) => {
|
||||
obj[h] = cells[i];
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
|
||||
// 3. 创建 Vue 子应用,把原生 table 替换成 <md-table>
|
||||
const mountPoint = document.createElement('div');
|
||||
table.replaceWith(mountPoint);
|
||||
|
||||
const app = createApp(MarkdownTable, { headers, rows });
|
||||
app.mount(mountPoint);
|
||||
});
|
||||
});
|
||||
|
||||
// console.log('Rendered HTML:', safeHtml.value);
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.markdown-body {
|
||||
padding: 10px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.markdown-body h1 {
|
||||
color: var(--el-color-primary);
|
||||
font-size: 1.5em;
|
||||
margin-bottom: 0.5em;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.markdown-body h2 {
|
||||
color: var(--el-color-primary);
|
||||
font-size: 1.5em;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
.markdown-body h3 {
|
||||
color: var(--el-color-primary);
|
||||
font-size: 1.2em;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
.markdown-body p {
|
||||
text-indent: 2em;
|
||||
text-align: justify;
|
||||
margin: 0.5em 0;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.markdown-body ol {
|
||||
list-style-type: decimal;
|
||||
padding-left: 2em;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.markdown-body ul {
|
||||
list-style-type: disc;
|
||||
padding-left: 2em;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.markdown-body hr {
|
||||
border: none;
|
||||
border-top: 1px solid var(--el-border-color);
|
||||
margin: 20px 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user