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