ChatText - last.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 'markdown-it-katex';
  8. const formulaContainer = ref(null);
  9. const props = defineProps({
  10. content: {
  11. type: String,
  12. default: ''
  13. }
  14. })
  15. const highlightBlock = (str, lang) => {
  16. return `
  17. <code class="hljs code-block-body ${lang}">${str}</code>
  18. `
  19. }
  20. const mdi = new MarkdownIt({
  21. html: true,
  22. linkify: true,
  23. breaks: true,
  24. typographer: true,
  25. highlight(code, language) {
  26. const validLang = !!(language && hljs.getLanguage(language))
  27. if (validLang) {
  28. const lang = language ?? ''
  29. return highlightBlock(hljs.highlight(code, { language: lang }).value, lang)
  30. }
  31. return highlightBlock(hljs.highlightAuto(code).value, '')
  32. },
  33. })
  34. mdi.renderer.rules.table_open = function (tokens, idx, options, env, self) { return '<div class="custom-table-wrapper"><table>'; };
  35. mdi.renderer.rules.table_close = function () { return '</table></div>'; };
  36. mdi.use(mila, { attrs: { target: '_blank', rel: 'noopener' } });
  37. mdi.use(markdownItMath, {
  38. inlineOpen: '\\(',
  39. inlineClose: '\\)',
  40. blockOpen: '\\[',
  41. blockClose: '\\]'
  42. });
  43. mdi.use(mdKatex, {
  44. blockClass: 'katexmath-block rounded-md p-[10px]',
  45. errorColor: ' #cc0000',
  46. })
  47. const text = computed(() => {
  48. const value = props.content ?? ""
  49. if (!props.asRawText)
  50. return mdi.render(value)
  51. return value
  52. })
  53. const defaultImageRender = mdi.renderer.rules.image;
  54. function modifyImageSrc(src) {
  55. if (src.startsWith('http')) {
  56. // 图片是绝对路径,不做修改
  57. return src;
  58. } else {
  59. // 假设所有相对路径图片都需要添加前缀
  60. return `https://your-cdn.com/images/${src}`;
  61. }
  62. }
  63. const md = new MarkdownIt();
  64. // 自定义规则以匹配 AA 开头的内容
  65. md.core.ruler.push('custom_aa_rule', function (state) {
  66. state.tokens.forEach(function (token, index) {
  67. // console.log( token, index );
  68. if (token.type === 'inline' && token.content.match(/^AA/)) {
  69. // 创建一个新的 token 来表示自定义的渲染
  70. let customToken = new state.Token('custom_aa_open', 'div', 1);
  71. customToken.block = true;
  72. customToken.attrs = [['class', 'aa-custom-class']];
  73. let textToken = new state.Token('text', '', 0);
  74. textToken.content = token.content;
  75. let closeToken = new state.Token('custom_aa_close', 'div', -1);
  76. closeToken.block = true;
  77. // 插入新 tokens 并移除原始 token
  78. state.tokens.splice(index, 1, customToken, textToken, closeToken);
  79. }
  80. });
  81. });
  82. md.renderer.rules['custom_aa_open'] = function () { return '<div class="aa-custom-class">'; };
  83. md.renderer.rules['custom_aa_close'] = function () { return '</div>'; };
  84. let result = md.render(`臭氧层的形成与耗损是一个涉及化学反应的关键过程,它对于地球生物的生存至关重要。臭氧(O3)主要通过以下两个化学反应来调控其在大气层中的浓度:
  85. 1. 形成过程(紫外线催化):
  86. 在紫外线(UV)的作用下,氧气分子(O2)接受能量(hν,表示光子能量),经历一个激发态的过程,发生以下反应:
  87. \[
  88. \mathcal{O}_2 + h\nu \rightarrow O^{\cdot} + O^{\cdot}
  89. \]
  90. 紧接着,两个单个氧原子(O•)在存在中间物质M(通常为大气中的其他分子如氮气或氢)的情况下,结合生成臭氧分子:
  91. \[
  92. O^{\cdot} + O^{\cdot} + M \rightarrow O_3 + M
  93. \]
  94. 2. 耗损过程:
  95. 臭氧在吸收更多紫外线后会发生分解,首先是单线程反应:
  96. \[
  97. O_3 + h\nu \rightarrow O_2 + O^{\cdot}
  98. \]
  99. 而在大气条件下,进一步的反应会导致臭氧分子与一个单个氧原子结合,形成两个氧气分子:
  100. \[
  101. O_3 + O^{\cdot} \rightarrow 2O_2
  102. \]
  103. 这两个反应(反应2和3)在平流层中持续循环,它们吸收大量的紫外线辐射,起到了地球生物的天然遮阳伞作用。然而,这个动态平衡一旦被破坏,例如由于人类活动产生的化学物质(如氯氟碳化物CFCs)的排放,臭氧层的稳定性就会受到威胁。
  104. 总之,臭氧层的形成与耗损是一个复杂的光化学过程,它在平流层中维持着微妙的平衡,确保地球免受有害紫外线的直接照射。任何影响这个平衡的因素都可能对生态系统产生深远影响。`);
  105. // md.renderer.rules['custom_aa_open'] = function () { return '<div class="aa-custom-class">'; };
  106. // md.renderer.rules['custom_aa_close'] = function () { return '</div>'; };
  107. const img = mdi.render('![](https://image.pollinations.ai/prompt/%E4%B8%80%E5%8F%AA%E7%8C%AB) 这是一只我为你画的猫。![](https://image.pollinations.ai/prompt/%E4%B8%80%E5%8F%AA%E7%8C%AB) 第二只第二只。')
  108. const res123 = mdi.render(`$$P_{待估} = \\frac{1/a}{1/a+1/b+1/c}*P_A+\\frac{1/b}{1/a+1/b+1/c}*P_B+\\frac{1/c}{1/a+1/b+1/c}*P_C$$`)
  109. </script>
  110. <template>
  111. <div ref="formulaContainer" class="formula-container"></div>
  112. <div class="markdown-body text-[15px] break-all" v-if="content">
  113. <!-- <div v-html="result"></div> -->
  114. <div v-html="text"></div>
  115. </div>
  116. </template>
  117. <style>
  118. .markdown-body p:last-child {
  119. margin-bottom: 0;
  120. }
  121. .markdown-body {
  122. .custom-table-wrapper {
  123. width: 710px;
  124. margin-bottom: 16px;
  125. overflow-x: scroll;
  126. &::-webkit-scrollbar {
  127. height: 4px;
  128. }
  129. &::-webkit-scrollbar-thumb {
  130. background: #ebf0f9;
  131. }
  132. &::-webkit-scrollbar-track {
  133. background: #f7f8fc;
  134. }
  135. }
  136. p:last-child {
  137. margin-bottom: 0;
  138. }
  139. /* img {
  140. margin-top: 20px;
  141. }
  142. table {
  143. thead {
  144. strong {
  145. font-size: 12px;
  146. }
  147. }
  148. } */
  149. }
  150. </style>