ua-markdown.vue 9.1 KB

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