index.vue 3.3 KB

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