ChatText - old.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <script setup>
  2. import { computed } 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 'markdown-it-katex';
  8. const props = defineProps({
  9. content: {
  10. type: String,
  11. default: ''
  12. }
  13. })
  14. const highlightBlock = (str, lang) => {
  15. return `
  16. <pre class="code-block-wrapper">
  17. <div class="code-block-header">
  18. <span class="code-block-header__copy"></span>
  19. </div>
  20. <code class="hljs code-block-body ${lang}">${str}</code>
  21. </pre>`
  22. }
  23. const mdi = new MarkdownIt({
  24. html: true,
  25. linkify: true,
  26. breaks: true,
  27. typographer: true,
  28. highlight(code, language) {
  29. const validLang = !!(language && hljs.getLanguage(language))
  30. if (validLang) {
  31. const lang = language ?? ''
  32. return highlightBlock(hljs.highlight(code, { language: lang }).value, lang)
  33. }
  34. return highlightBlock(hljs.highlightAuto(code).value, '')
  35. },
  36. })
  37. mdi.use(mila, { attrs: { target: '_blank', rel: 'noopener' } })
  38. mdi.use(markdownItMath, {
  39. inlineOpen: '\\(',
  40. inlineClose: '\\)',
  41. blockOpen: '\\[',
  42. blockClose: '\\]'
  43. });
  44. mdi.use(mdKatex, {
  45. blockClass: 'katexmath-block rounded-md p-[10px]',
  46. errorColor: ' #cc0000',
  47. })
  48. const text = computed(() => {
  49. const value = props.content ?? ""
  50. if (!props.asRawText)
  51. return mdi.render(value)
  52. return value
  53. })
  54. // const markdownText = '$$\mathcal{O}_{2} + h\nu \rightarrow O· + O·O';
  55. // const htmlOutput = mdi.render(markdownText);
  56. const md = new MarkdownIt();
  57. md.use(mdKatex, {
  58. // 可以根据需要调整配置项,这里使用默认配置
  59. });
  60. const markdownText = `
  61. # 数学公式示例
  62. ## 行内公式
  63. 这是行内公式:$E=mc^2$。
  64. ## 块级公式
  65. $$
  66. \\sum_{i=0}^{n} i = \\frac{n(n+1)}{2}
  67. $$
  68. ## 分数
  69. \\[ \\frac{1}{x} + \\frac{1}{y} = \\frac{x+y}{xy} \\]
  70. ## 指数与根号
  71. \\[ x^{n} \\quad \\sqrt[n]{x} \\]
  72. ## 微积分
  73. \\[ \\int_{a}^{b} f(x)dx \\quad \\frac{d}{dx}f(x) \\]
  74. ## 矩阵
  75. \\[
  76. \\begin{bmatrix}
  77. a & b \\
  78. c & d
  79. \\end{bmatrix}
  80. \\]
  81. ## 向量
  82. \\( \\vec{v} = \\langle x, y, z \\rangle \\)
  83. `;
  84. const htmlOutput = md.render(markdownText);
  85. </script>
  86. <template>
  87. <div class="markdown-body text-[15px] break-all" v-if="content">
  88. <div v-html="htmlOutput"></div>
  89. <hr>
  90. <div v-html="text"></div>
  91. </div>
  92. </template>
  93. <style>
  94. .markdown-body p:last-child {
  95. margin-bottom: 0;
  96. }
  97. .markdown-body {
  98. p:last-child {
  99. margin-bottom: 0;
  100. }
  101. /* img {
  102. margin-top: 20px;
  103. }
  104. table {
  105. thead {
  106. strong {
  107. font-size: 12px;
  108. }
  109. }
  110. } */
  111. }
  112. </style>