提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<div ref="contentRef" class="markdown-view" v-html="renderedMarkdown"></div> |
|
3 |
</template> |
|
4 |
|
|
5 |
<script setup lang="ts"> |
|
6 |
import { useClipboard } from '@vueuse/core' |
|
7 |
import MarkdownIt from 'markdown-it' |
|
8 |
import 'highlight.js/styles/vs2015.min.css' |
|
9 |
import hljs from 'highlight.js' |
|
10 |
|
|
11 |
// 定义组件属性 |
|
12 |
const props = defineProps({ |
|
13 |
content: { |
|
14 |
type: String, |
|
15 |
required: true |
|
16 |
} |
|
17 |
}) |
|
18 |
|
|
19 |
const message = useMessage() // 消息弹窗 |
|
20 |
const { copy } = useClipboard() // 初始化 copy 到粘贴板 |
|
21 |
const contentRef = ref() |
|
22 |
|
|
23 |
const md = new MarkdownIt({ |
|
24 |
highlight: function (str, lang) { |
|
25 |
if (lang && hljs.getLanguage(lang)) { |
|
26 |
try { |
|
27 |
const copyHtml = `<div id="copy" data-copy='${str}' style="position: absolute; right: 10px; top: 5px; color: #fff;cursor: pointer;">复制</div>` |
|
28 |
return `<pre style="position: relative;">${copyHtml}<code class="hljs">${hljs.highlight(lang, str, true).value}</code></pre>` |
|
29 |
} catch (__) {} |
|
30 |
} |
|
31 |
return `` |
|
32 |
} |
|
33 |
}) |
|
34 |
|
|
35 |
/** 渲染 markdown */ |
|
36 |
const renderedMarkdown = computed(() => { |
|
37 |
return md.render(props.content) |
|
38 |
}) |
|
39 |
|
|
40 |
/** 初始化 **/ |
|
41 |
onMounted(async () => { |
|
42 |
// 添加 copy 监听 |
|
43 |
contentRef.value.addEventListener('click', (e: any) => { |
|
44 |
if (e.target.id === 'copy') { |
|
45 |
copy(e.target?.dataset?.copy) |
|
46 |
message.success('复制成功!') |
|
47 |
} |
|
48 |
}) |
|
49 |
}) |
|
50 |
</script> |
|
51 |
|
|
52 |
<style lang="scss"> |
|
53 |
.markdown-view { |
|
54 |
font-family: PingFang SC; |
|
55 |
font-size: 0.95rem; |
|
56 |
font-weight: 400; |
|
57 |
line-height: 1.6rem; |
|
58 |
letter-spacing: 0em; |
|
59 |
text-align: left; |
|
60 |
color: #3b3e55; |
|
61 |
max-width: 100%; |
|
62 |
|
|
63 |
pre { |
|
64 |
position: relative; |
|
65 |
} |
|
66 |
|
|
67 |
pre code.hljs { |
|
68 |
width: auto; |
|
69 |
} |
|
70 |
|
|
71 |
code.hljs { |
|
72 |
border-radius: 6px; |
|
73 |
padding-top: 20px; |
|
74 |
width: auto; |
|
75 |
@media screen and (min-width: 1536px) { |
|
76 |
width: 960px; |
|
77 |
} |
|
78 |
|
|
79 |
@media screen and (max-width: 1536px) and (min-width: 1024px) { |
|
80 |
width: calc(100vw - 400px - 64px - 32px * 2); |
|
81 |
} |
|
82 |
|
|
83 |
@media screen and (max-width: 1024px) and (min-width: 768px) { |
|
84 |
width: calc(100vw - 32px * 2); |
|
85 |
} |
|
86 |
|
|
87 |
@media screen and (max-width: 768px) { |
|
88 |
width: calc(100vw - 16px * 2); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
p, |
|
93 |
code.hljs { |
|
94 |
margin-bottom: 16px; |
|
95 |
} |
|
96 |
|
|
97 |
p { |
|
98 |
//margin-bottom: 1rem !important; |
|
99 |
margin: 0; |
|
100 |
margin-bottom: 3px; |
|
101 |
} |
|
102 |
|
|
103 |
/* 标题通用格式 */ |
|
104 |
h1, |
|
105 |
h2, |
|
106 |
h3, |
|
107 |
h4, |
|
108 |
h5, |
|
109 |
h6 { |
|
110 |
color: var(--color-G900); |
|
111 |
margin: 24px 0 8px; |
|
112 |
font-weight: 600; |
|
113 |
} |
|
114 |
|
|
115 |
h1 { |
|
116 |
font-size: 22px; |
|
117 |
line-height: 32px; |
|
118 |
} |
|
119 |
|
|
120 |
h2 { |
|
121 |
font-size: 20px; |
|
122 |
line-height: 30px; |
|
123 |
} |
|
124 |
|
|
125 |
h3 { |
|
126 |
font-size: 18px; |
|
127 |
line-height: 28px; |
|
128 |
} |
|
129 |
|
|
130 |
h4 { |
|
131 |
font-size: 16px; |
|
132 |
line-height: 26px; |
|
133 |
} |
|
134 |
|
|
135 |
h5 { |
|
136 |
font-size: 16px; |
|
137 |
line-height: 24px; |
|
138 |
} |
|
139 |
|
|
140 |
h6 { |
|
141 |
font-size: 16px; |
|
142 |
line-height: 24px; |
|
143 |
} |
|
144 |
|
|
145 |
/* 列表(有序,无序) */ |
|
146 |
ul, |
|
147 |
ol { |
|
148 |
margin: 0 0 8px 0; |
|
149 |
padding: 0; |
|
150 |
font-size: 16px; |
|
151 |
line-height: 24px; |
|
152 |
color: #3b3e55; // var(--color-CG600); |
|
153 |
} |
|
154 |
|
|
155 |
li { |
|
156 |
margin: 4px 0 0 20px; |
|
157 |
margin-bottom: 1rem; |
|
158 |
} |
|
159 |
|
|
160 |
ol > li { |
|
161 |
list-style-type: decimal; |
|
162 |
margin-bottom: 1rem; |
|
163 |
// 表达式,修复有序列表序号展示不全的问题 |
|
164 |
// &:nth-child(n + 10) { |
|
165 |
// margin-left: 30px; |
|
166 |
// } |
|
167 |
|
|
168 |
// &:nth-child(n + 100) { |
|
169 |
// margin-left: 30px; |
|
170 |
// } |
|
171 |
} |
|
172 |
|
|
173 |
ul > li { |
|
174 |
list-style-type: disc; |
|
175 |
font-size: 16px; |
|
176 |
line-height: 24px; |
|
177 |
margin-right: 11px; |
|
178 |
margin-bottom: 1rem; |
|
179 |
color: #3b3e55; // var(--color-G900); |
|
180 |
} |
|
181 |
|
|
182 |
ol ul, |
|
183 |
ol ul > li, |
|
184 |
ul ul, |
|
185 |
ul ul li { |
|
186 |
// list-style: circle; |
|
187 |
font-size: 16px; |
|
188 |
list-style: none; |
|
189 |
margin-left: 6px; |
|
190 |
margin-bottom: 1rem; |
|
191 |
} |
|
192 |
|
|
193 |
ul ul ul, |
|
194 |
ul ul ul li, |
|
195 |
ol ol, |
|
196 |
ol ol > li, |
|
197 |
ol ul ul, |
|
198 |
ol ul ul > li, |
|
199 |
ul ol, |
|
200 |
ul ol > li { |
|
201 |
list-style: square; |
|
202 |
} |
|
203 |
} |
|
204 |
</style> |