u-markdown.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <template>
  2. <view class="u-markdown">
  3. <rich-text space="nbsp" :nodes="parsedNodes" @itemclick="handleItemClick"></rich-text>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * markdown
  9. * @description 该组件用于解析markdown内容
  10. * @tutorial https://uviewui.com/components/markdown.html
  11. * @property {String} content 渲染解析内容
  12. * @property {Boolean} showLine 是否显示代码块行号
  13. * @example <u-markdown :content="content" :showLine="showLine" ></u-markdown>
  14. */
  15. import props from './props.js';
  16. import mixin from '../../libs/mixin/mixin'
  17. import mpMixin from '../../libs/mixin/mpMixin'
  18. import MarkdownIt from './markdown/markdown-it.min.js'
  19. import hljs from './highlight/uni-highlight.min.js'
  20. import './highlight/atom-one-dark.css'
  21. // #ifdef APP-NVUE
  22. import parseHtml from './parser.js'
  23. // #endif
  24. export default {
  25. name: "u-markdown",
  26. mixins: [mpMixin, mixin, props],
  27. data() {
  28. return {
  29. copyCodeData: [],
  30. markdown: null,
  31. highlight: null,
  32. latex: null,
  33. }
  34. },
  35. computed: {
  36. parsedNodes() {
  37. return this.parseNodes(this.content)
  38. }
  39. },
  40. created() {
  41. this.init()
  42. },
  43. methods: {
  44. init() {
  45. let that = this;
  46. that.markdown = MarkdownIt({
  47. html: true,
  48. highlight: function (str, lang) {
  49. let preCode = ''
  50. try {
  51. preCode = hljs.highlightAuto(str).value
  52. } catch (err) {
  53. preCode = that.markdown.utils.escapeHtml(str)
  54. }
  55. const lines = preCode.split(/\n/).slice(0, -1)
  56. // 添加自定义行号
  57. let html = lines
  58. .map((item, index) => {
  59. if (item == '') {
  60. return ''
  61. }
  62. return (
  63. '<li><span class="line-num" data-line="' +
  64. (index + 1) +
  65. '"></span>' +
  66. item +
  67. '</li>'
  68. )
  69. })
  70. .join('')
  71. if (that.showLine) {
  72. html = '<ol style="padding: 0px 30px;">' + html + '</ol>'
  73. } else {
  74. html = '<ol style="padding: 0px 7px;list-style:none;">' + html + '</ol>'
  75. }
  76. that.copyCodeData = str
  77. let htmlCode = `<div class="markdown-wrap">`
  78. // #ifndef MP-WEIXIN
  79. htmlCode += `<div style="color: #aaa;text-align: right;font-size: 12px;padding:8px;">`
  80. htmlCode += `${lang}<a class="copy-btn" code-data-index="${that.copyCodeData.length - 1}" style="margin-left: 8px;">复制代码</a>`
  81. htmlCode += `</div>`
  82. // #endif
  83. htmlCode += `<pre class="hljs"><code>${html}</code></pre>`
  84. htmlCode += '</div>'
  85. return htmlCode
  86. }
  87. });
  88. // 自定义表格渲染规则,添加横向滚动包装器
  89. that.markdown.renderer.rules.table_open = function (tokens, idx, options, env, slf) {
  90. return '<div class="table-wrapper"><table class="table">';
  91. };
  92. that.markdown.renderer.rules.table_close = function (tokens, idx, options, env, slf) {
  93. return '</table></div>';
  94. };
  95. // 自定义表格行渲染规则
  96. that.markdown.renderer.rules.tr_open = function (tokens, idx, options, env, slf) {
  97. return '<tr class="tr">';
  98. };
  99. that.markdown.renderer.rules.tr_close = function (tokens, idx, options, env, slf) {
  100. return '</tr>';
  101. };
  102. // 自定义表格单元格渲染规则
  103. that.markdown.renderer.rules.td_open = function (tokens, idx, options, env, slf) {
  104. return '<td class="td">';
  105. };
  106. that.markdown.renderer.rules.td_close = function (tokens, idx, options, env, slf) {
  107. return '</td>';
  108. };
  109. // 自定义表头单元格渲染规则
  110. that.markdown.renderer.rules.th_open = function (tokens, idx, options, env, slf) {
  111. return '<th class="th">';
  112. };
  113. that.markdown.renderer.rules.th_close = function (tokens, idx, options, env, slf) {
  114. return '</th>';
  115. };
  116. },
  117. parseNodes(value) {
  118. if (!value) return
  119. // 解析<br />到\n
  120. value = value.replace(/<br>|<br\/>|<br \/>/g, '\n')
  121. value = value.replace(/&nbsp;/g, ' ')
  122. let htmlString = ''
  123. if (value.split('```').length % 2) {
  124. let mdtext = value
  125. if (mdtext[mdtext.length - 1] != '\n') {
  126. mdtext += '\n'
  127. }
  128. htmlString = this.markdown.render(mdtext)
  129. } else {
  130. htmlString = this.markdown.render(value)
  131. }
  132. // #ifndef APP-NVUE
  133. return htmlString
  134. // #endif
  135. // 将htmlString转成htmlArray,反之使用rich-text解析
  136. // #ifdef APP-NVUE
  137. return parseHtml(htmlString)
  138. // #endif
  139. },
  140. handleItemClick(e) {
  141. let { attrs } = e.detail.node
  142. let { 'code-data-index': codeDataIndex, class: className } = attrs
  143. if (className == 'copy-btn') {
  144. uni.setClipboardData({
  145. data: this.copyCodeData[codeDataIndex],
  146. showToast: false,
  147. success() {
  148. uni.showToast({
  149. title: '复制成功',
  150. icon: 'none'
  151. })
  152. }
  153. })
  154. }
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss" scoped>
  160. @import '../../libs/css/components.scss';
  161. .u-markdown {
  162. font-size: 14px;
  163. line-height: 1.5;
  164. word-break: break-all;
  165. h1,
  166. h2,
  167. h3,
  168. h4,
  169. h5,
  170. h6 {
  171. font-family: inherit;
  172. font-weight: 500;
  173. line-height: 1.1;
  174. color: inherit;
  175. }
  176. h1,
  177. h2,
  178. h3 {
  179. margin-top: 20px;
  180. margin-bottom: 10px;
  181. }
  182. h4,
  183. h5,
  184. h6 {
  185. margin-top: 10px;
  186. margin-bottom: 10px;
  187. }
  188. .h1,
  189. h1 {
  190. font-size: 36px;
  191. }
  192. .h2,
  193. h2 {
  194. font-size: 30px;
  195. }
  196. .h3,
  197. h3 {
  198. font-size: 24px;
  199. }
  200. .h4,
  201. h4 {
  202. font-size: 18px;
  203. }
  204. .h5,
  205. h5 {
  206. font-size: 14px;
  207. }
  208. .h6,
  209. h6 {
  210. font-size: 12px;
  211. }
  212. a {
  213. background-color: transparent;
  214. color: #2196f3;
  215. text-decoration: none;
  216. }
  217. hr,
  218. ::v-deep .hr {
  219. margin-top: 20px;
  220. margin-bottom: 20px;
  221. border: 0;
  222. border-top: 1px solid #e5e5e5;
  223. }
  224. img {
  225. max-width: 35%;
  226. }
  227. p {
  228. margin: 0 0 10px;
  229. }
  230. em {
  231. font-style: italic;
  232. font-weight: inherit;
  233. }
  234. ol,
  235. ul {
  236. margin-top: 0;
  237. margin-bottom: 10px;
  238. padding-left: 40px;
  239. }
  240. ol ol,
  241. ol ul,
  242. ul ol,
  243. ul ul {
  244. margin-bottom: 0;
  245. }
  246. ol ol,
  247. ul ol {
  248. list-style-type: lower-roman;
  249. }
  250. ol ol ol,
  251. ul ul ol {
  252. list-style-type: lower-alpha;
  253. }
  254. dl {
  255. margin-top: 0;
  256. margin-bottom: 20px;
  257. }
  258. dt {
  259. font-weight: 600;
  260. }
  261. dt,
  262. dd {
  263. line-height: 1.4;
  264. }
  265. .task-list-item {
  266. list-style-type: none;
  267. }
  268. .task-list-item input {
  269. margin: 0 0.2em 0.25em -1.6em;
  270. vertical-align: middle;
  271. }
  272. pre {
  273. position: relative;
  274. z-index: 11;
  275. }
  276. code,
  277. kbd,
  278. pre,
  279. samp {
  280. font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;
  281. }
  282. code:not(.hljs) {
  283. padding: 2px 4px;
  284. font-size: 90%;
  285. color: #c7254e;
  286. background-color: #ffe7ee;
  287. border-radius: 4px;
  288. }
  289. code:empty {
  290. display: none;
  291. }
  292. pre code.hljs {
  293. color: var(--vg__text-1);
  294. border-radius: 16px;
  295. background: var(--vg__bg-1);
  296. font-size: 12px;
  297. }
  298. .markdown-wrap {
  299. font-size: 12px;
  300. margin-bottom: 10px;
  301. }
  302. pre.code-block-wrapper {
  303. background: #2b2b2b;
  304. color: #f8f8f2;
  305. border-radius: 4px;
  306. overflow-x: auto;
  307. padding: 1em;
  308. position: relative;
  309. }
  310. pre.code-block-wrapper code {
  311. padding: auto;
  312. font-size: inherit;
  313. color: inherit;
  314. background-color: inherit;
  315. border-radius: 0;
  316. }
  317. .code-block-header__copy {
  318. font-size: 16px;
  319. margin-left: 5px;
  320. }
  321. abbr[data-original-title],
  322. abbr[title] {
  323. cursor: help;
  324. border-bottom: 1px dotted #777;
  325. }
  326. blockquote {
  327. padding: 10px 20px;
  328. margin: 0 0 20px;
  329. font-size: 17.5px;
  330. border-left: 5px solid #e5e5e5;
  331. }
  332. blockquote ol:last-child,
  333. blockquote p:last-child,
  334. blockquote ul:last-child {
  335. margin-bottom: 0;
  336. }
  337. blockquote .small,
  338. blockquote footer,
  339. blockquote small {
  340. display: block;
  341. font-size: 80%;
  342. line-height: 1.42857143;
  343. color: #777;
  344. }
  345. blockquote .small:before,
  346. blockquote footer:before,
  347. blockquote small:before {
  348. content: '\2014 \00A0';
  349. }
  350. .blockquote-reverse,
  351. blockquote.pull-right {
  352. padding-right: 15px;
  353. padding-left: 0;
  354. text-align: right;
  355. border-right: 5px solid #eee;
  356. border-left: 0;
  357. }
  358. .blockquote-reverse .small:before,
  359. .blockquote-reverse footer:before,
  360. .blockquote-reverse small:before,
  361. blockquote.pull-right .small:before,
  362. blockquote.pull-right footer:before,
  363. blockquote.pull-right small:before {
  364. content: '';
  365. }
  366. .blockquote-reverse .small:after,
  367. .blockquote-reverse footer:after,
  368. .blockquote-reverse small:after,
  369. blockquote.pull-right .small:after,
  370. blockquote.pull-right footer:after,
  371. blockquote.pull-right small:after {
  372. content: '\00A0 \2014';
  373. }
  374. .footnotes {
  375. -moz-column-count: 2;
  376. -webkit-column-count: 2;
  377. column-count: 2;
  378. }
  379. .footnotes-list {
  380. padding-left: 2em;
  381. }
  382. ::v-deep .table-wrapper {
  383. width: 100%;
  384. overflow-x: auto;
  385. border-radius: 8px;
  386. border: 1px solid #e5e5e5;
  387. }
  388. ::v-deep .table-wrapper::-webkit-scrollbar {
  389. display: none;
  390. }
  391. ::v-deep .table-wrapper .table {
  392. border-spacing: 0;
  393. border-collapse: collapse;
  394. width: 100%;
  395. min-width: 100%;
  396. }
  397. ::v-deep .table-wrapper .table .th,
  398. ::v-deep .table-wrapper .table .td {
  399. padding: 8px 10px;
  400. border-bottom: 1px solid #e5e5e5;
  401. border-right: 1px solid #e5e5e5;
  402. white-space: nowrap;
  403. text-align: left;
  404. vertical-align: middle;
  405. min-width: 100%;
  406. }
  407. ::v-deep .table-wrapper .table .tr:last-child .td {
  408. border-bottom: none;
  409. }
  410. ::v-deep .table-wrapper .table .tr .td:last-child {
  411. border-right: none;
  412. }
  413. ::v-deep .table-wrapper .table .th {
  414. font-weight: 600;
  415. background-color: #f8f9fa;
  416. position: sticky;
  417. top: 0;
  418. z-index: 10;
  419. }
  420. ::v-deep .hljs{
  421. padding:10px 8px 0;
  422. margin-bottom:5px;
  423. overflow: auto;
  424. display: block;
  425. border-radius: 5px;
  426. }
  427. .hljs[class*='language-']:before {
  428. position: absolute;
  429. z-index: 3;
  430. top: 0.8em;
  431. right: 1em;
  432. font-size: 0.8em;
  433. color: #999;
  434. }
  435. .hljs[class~='language-js']:before {
  436. content: 'js';
  437. }
  438. .hljs[class~='language-ts']:before {
  439. content: 'ts';
  440. }
  441. .hljs[class~='language-html']:before {
  442. content: 'html';
  443. }
  444. .hljs[class~='language-md']:before {
  445. content: 'md';
  446. }
  447. .hljs[class~='language-vue']:before {
  448. content: 'vue';
  449. }
  450. .hljs[class~='language-css']:before {
  451. content: 'css';
  452. }
  453. .hljs[class~='language-sass']:before {
  454. content: 'sass';
  455. }
  456. .hljs[class~='language-scss']:before {
  457. content: 'scss';
  458. }
  459. .hljs[class~='language-less']:before {
  460. content: 'less';
  461. }
  462. .hljs[class~='language-stylus']:before {
  463. content: 'stylus';
  464. }
  465. .hljs[class~='language-go']:before {
  466. content: 'go';
  467. }
  468. .hljs[class~='language-java']:before {
  469. content: 'java';
  470. }
  471. .hljs[class~='language-c']:before {
  472. content: 'c';
  473. }
  474. .hljs[class~='language-sh']:before {
  475. content: 'sh';
  476. }
  477. .hljs[class~='language-yaml']:before {
  478. content: 'yaml';
  479. }
  480. .hljs[class~='language-py']:before {
  481. content: 'py';
  482. }
  483. .hljs[class~='language-docker']:before {
  484. content: 'docker';
  485. }
  486. .hljs[class~='language-dockerfile']:before {
  487. content: 'dockerfile';
  488. }
  489. .hljs[class~='language-makefile']:before {
  490. content: 'makefile';
  491. }
  492. .hljs[class~='language-javascript']:before {
  493. content: 'js';
  494. }
  495. .hljs[class~='language-typescript']:before {
  496. content: 'ts';
  497. }
  498. .hljs[class~='language-markup']:before {
  499. content: 'html';
  500. }
  501. .hljs[class~='language-markdown']:before {
  502. content: 'md';
  503. }
  504. .hljs[class~='language-json']:before {
  505. content: 'json';
  506. }
  507. .hljs[class~='language-ruby']:before {
  508. content: 'rb';
  509. }
  510. .hljs[class~='language-python']:before {
  511. content: 'py';
  512. }
  513. .hljs[class~='language-bash']:before {
  514. content: 'sh';
  515. }
  516. .hljs[class~='language-php']:before {
  517. content: 'php';
  518. }
  519. }
  520. </style>