u-markdown.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <view class="up-markdown" :class="theme">
  3. <up-parse :content="parsedContent" :previewImg="previewImg"></up-parse>
  4. </view>
  5. </template>
  6. <script>
  7. import { marked } from './marked.esm.js';
  8. export default {
  9. name: 'up-markdown',
  10. props: {
  11. // markdown内容
  12. content: {
  13. type: String,
  14. default: ''
  15. },
  16. // 是否启用图片预览
  17. previewImg: {
  18. type: Boolean,
  19. default: true
  20. },
  21. // 是否显示代码块行号
  22. showLineNumber: {
  23. type: Boolean,
  24. default: false
  25. },
  26. // 主题样式 'light' | 'dark'
  27. theme: {
  28. type: String,
  29. default: 'light'
  30. }
  31. },
  32. data() {
  33. return {
  34. parsedContent: ''
  35. };
  36. },
  37. watch: {
  38. content: {
  39. handler(newVal) {
  40. this.parseMarkdown(newVal);
  41. },
  42. immediate: true
  43. }
  44. },
  45. methods: {
  46. // 解析markdown内容
  47. parseMarkdown(content) {
  48. if (!content) {
  49. this.parsedContent = '';
  50. return;
  51. }
  52. // 使用marked解析markdown
  53. let parsed = marked(content);
  54. // 处理代码块
  55. parsed = this.handleCodeBlock(parsed);
  56. // 应用主题样式
  57. parsed = this.applyTheme(parsed);
  58. this.parsedContent = parsed;
  59. },
  60. // 处理代码块
  61. handleCodeBlock(html) {
  62. // 添加代码块样式和行号
  63. return html.replace(/<pre><code([^>]*)>([^<]+)<\/code><\/pre>/g, (match, lang, code) => {
  64. const language = lang.match(/class="language-([^"]+)"/);
  65. const langClass = language ? `language-${language[1]}` : '';
  66. let result = `<pre class="up-markdown-code ${langClass}">`;
  67. if (this.showLineNumber) {
  68. // 添加行号
  69. const lines = code.split('\n').filter(line => line.trim() !== '');
  70. result += '<span class="up-markdown-line-numbers">';
  71. lines.push('');
  72. lines.forEach((_, index) => {
  73. result += `<span class="up-markdown-line-number">${index + 1}</span>`;
  74. });
  75. result += '</span>';
  76. }
  77. result += `<code class='code-lang ${langClass}'>${code}</code></pre>`;
  78. return result;
  79. });
  80. },
  81. // 应用主题样式
  82. applyTheme(html) {
  83. // 可以根据theme属性添加不同的样式类
  84. return html;
  85. }
  86. }
  87. };
  88. </script>
  89. <style lang="scss" scoped>
  90. .up-markdown {
  91. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  92. font-size: 16px;
  93. line-height: 1.6;
  94. color: #333;
  95. padding: 16px;
  96. word-wrap: break-word;
  97. /* 标题样式 */
  98. ::v-deep h1 {
  99. font-size: 32px;
  100. margin: 8px 0;
  101. font-weight: bold;
  102. }
  103. ::v-deep h2 {
  104. font-size: 24px;
  105. margin: 8px 0;
  106. font-weight: bold;
  107. }
  108. ::v-deep h3 {
  109. font-size: 18px;
  110. margin: 7px 0;
  111. font-weight: bold;
  112. }
  113. ::v-deep h4 {
  114. font-size: 16px;
  115. margin: 7px 0;
  116. font-weight: bold;
  117. }
  118. ::v-deep h5 {
  119. font-size: 13px;
  120. margin: 6px 0;
  121. font-weight: bold;
  122. }
  123. ::v-deep h6 {
  124. font-size: 10px;
  125. margin: 5px 0;
  126. font-weight: bold;
  127. }
  128. /* 段落样式 */
  129. ::v-deep p {
  130. margin: 16px 0;
  131. }
  132. /* 链接样式 */
  133. ::v-deep a {
  134. color: #007AFF;
  135. text-decoration: none;
  136. &:hover {
  137. text-decoration: underline;
  138. }
  139. }
  140. /* 列表样式 */
  141. ::v-deep ul,
  142. ::v-deep ol {
  143. margin: 16px 0;
  144. padding-left: 32px;
  145. li {
  146. margin: 8px 0;
  147. }
  148. }
  149. ::v-deep ul li {
  150. list-style-type: disc;
  151. }
  152. ::v-deep ol li {
  153. list-style-type: decimal;
  154. }
  155. /* 引用样式 */
  156. ::v-deep blockquote {
  157. margin: 8px 0;
  158. padding: 0 10px;
  159. border-left: 4px solid #ccc;
  160. color: #666;
  161. }
  162. /* 代码样式 */
  163. ::v-deep &-code {
  164. font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
  165. font-size: 14px;
  166. background-color: #f6f8fa;
  167. padding: 3px 6px;
  168. border-radius: 3px;
  169. display: flex;
  170. }
  171. ::v-deep .code-lang {
  172. width: 100%;
  173. overflow-x: auto;
  174. }
  175. ::v-deep pre {
  176. font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
  177. font-size: 14px;
  178. background-color: #f6f8fa;
  179. padding: 16px;
  180. overflow: auto;
  181. border-radius: 6px;
  182. margin: 16px 0;
  183. ::v-deep code {
  184. background: none;
  185. padding: 0;
  186. }
  187. }
  188. /* 表格样式 */
  189. ::v-deep table {
  190. border-collapse: collapse;
  191. margin: 16px 0;
  192. width: 100%;
  193. th,
  194. td {
  195. padding: 6px 13px;
  196. border: 1px solid #dfe2e5;
  197. }
  198. th {
  199. font-weight: 600;
  200. }
  201. tr:nth-child(2n) {
  202. background-color: #f6f8fa;
  203. }
  204. }
  205. /* 图片样式 */
  206. ::v-deep img {
  207. max-width: 100%;
  208. box-sizing: content-box;
  209. background-color: #fff;
  210. margin: 16px 0;
  211. }
  212. /* 分割线样式 */
  213. ::v-deep hr {
  214. height: 1px;
  215. padding: 0;
  216. margin: 24px 0;
  217. background-color: #e1e4e8;
  218. border: 0;
  219. }
  220. /* 深色主题 */
  221. &.dark {
  222. color: #ccc;
  223. background-color: #1e1e1e;
  224. ::v-deep &-code {
  225. background-color: #2d2d2d;
  226. color: #dcdcdc;
  227. }
  228. ::v-deep pre {
  229. background-color: #2d2d2d;
  230. color: #dcdcdc;
  231. }
  232. ::v-deep blockquote {
  233. margin: 8px 0;
  234. padding: 0 10px;
  235. border-left: 4px solid #ccc;
  236. color: #bbb;
  237. }
  238. ::v-deep a {
  239. color: #4da6ff;
  240. }
  241. }
  242. }
  243. /* 代码块行号样式 */
  244. ::v-deep .up-markdown-line-numbers {
  245. display: flex;
  246. flex-direction: column;
  247. align-items: flex-start;
  248. padding-right: 10px;
  249. margin-right: 10px;
  250. border-right: 1px solid #ddd;
  251. user-select: none;
  252. .up-markdown-line-number {
  253. color: #999;
  254. font-size: 14px;
  255. line-height: 1.6;
  256. }
  257. }
  258. </style>