index.vue 4.1 KB

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