ChatText.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <script setup>
  2. import { computed, onMounted , ref} from 'vue';
  3. import MarkdownIt from 'markdown-it';
  4. import hljs from 'highlight.js';
  5. import mila from 'markdown-it-link-attributes';
  6. import markdownItMath from 'markdown-it-math';
  7. import mdKatex from '@iktakahiro/markdown-it-katex';
  8. const props = defineProps({
  9. content: {
  10. type: String,
  11. default: ''
  12. }
  13. })
  14. const highlightBlock = (str, lang) => {
  15. return `
  16. <code class="hljs code-block-body ${lang}">${str}</code>
  17. `
  18. }
  19. const mdi = new MarkdownIt({
  20. html: true,
  21. linkify: true,
  22. breaks: true,
  23. typographer: true,
  24. highlight(code, language) {
  25. const validLang = !!(language && hljs.getLanguage(language))
  26. if (validLang) {
  27. const lang = language ?? ''
  28. return highlightBlock(hljs.highlight(code, { language: lang }).value, lang)
  29. }
  30. return highlightBlock(hljs.highlightAuto(code).value, '')
  31. },
  32. })
  33. mdi.use(mila, { attrs: { target: '_blank', rel: 'noopener' } });
  34. mdi.use(markdownItMath, {
  35. inlineOpen: '\\(',
  36. inlineClose: '\\)',
  37. blockOpen: '\\[',
  38. blockClose: '\\]'
  39. });
  40. mdi.use(mdKatex, {
  41. blockClass: 'katexmath-block rounded-md p-[10px]',
  42. errorColor: ' #cc0000',
  43. })
  44. mdi.renderer.rules.table_open = function (tokens, idx, options, env, self) { return '<div class="custom-table-wrapper"><table>'; };
  45. mdi.renderer.rules.table_close = function () { return '</table></div>'; };
  46. const text = computed(() => {
  47. const value = props.content ?? ""
  48. if (!props.asRawText)
  49. return mdi.render(value)
  50. return value
  51. })
  52. </script>
  53. <template>
  54. <div class="markdown-body text-[15px] break-all" v-if="content">
  55. <div v-html="text"></div>
  56. </div>
  57. </template>
  58. <style>
  59. .markdown-body p:last-child {
  60. margin-bottom: 0;
  61. }
  62. .markdown-body {
  63. .custom-table-wrapper {
  64. width: 710px;
  65. padding: 10px 10px 4px 10px;
  66. background: #eff8fc;
  67. margin-bottom: 16px;
  68. overflow-x: scroll;
  69. &::-webkit-scrollbar {
  70. height: 6px;
  71. }
  72. &::-webkit-scrollbar-thumb {
  73. background: #bfbfbf;
  74. }
  75. &::-webkit-scrollbar-track {
  76. background: #eff8fc;
  77. }
  78. }
  79. p:last-child {
  80. margin-bottom: 0;
  81. }
  82. /* img {
  83. margin-top: 20px;
  84. }
  85. table {
  86. thead {
  87. strong {
  88. font-size: 12px;
  89. }
  90. }
  91. } */
  92. }
  93. </style>