你可以使用Vue 3的Options API方法将Markdown字符串转换为普通的HTML。

huangapple go评论109阅读模式
英文:

How can I convert Markdown string to normal HTML in Vue 3 (Options API approach)

问题

我是一名后端开发人员,正在尝试学习Vue.js。我正在编写一个前端应用程序,该应用程序与OpenAI API进行通信,并从GPT-4获取响应。这部分工作非常完美,但是GPT-4返回的响应是用Markdown格式的。

我无法找到如何在我的特定情况下处理这个问题。

我尝试使用以下npm包:https://www.npmjs.com/package/vue-markdown
在我的代码中使用这个包后,我得到了以下结果(请不要评判设计):使用vue-markdown npm包的结果截图

这是我在Chat.vue文件中编写的代码:

  1. <script lang="ts">
  2. import { GPTMessage } from '../types/GPTMessage';
  3. import { ChatAPI } from '../lib/gpt';
  4. import VueMarkdown from 'vue-markdown-render';
  5. export default {
  6. data() {
  7. return {
  8. chatHistory: [] as GPTMessage[],
  9. input: "" as string
  10. }
  11. },
  12. components: {
  13. VueMarkdown
  14. },
  15. methods: {
  16. async askGpt(): Promise<void> {
  17. this.chatHistory.push({ role: "user", content: this.input })
  18. this.input = ""
  19. const resp = await ChatAPI(this.chatHistory)
  20. this.chatHistory.push({ role: resp.role, content: resp.content })
  21. },
  22. handleChange(event: any) {
  23. this.input = event.target.value
  24. }
  25. }
  26. }
  27. </script>
  28. <template>
  29. <div class="flex flex-col h-screen justify-between mb-auto">
  30. <div class="mb-auto">
  31. <vue-markdown v-for="message in chatHistory">{{ message.content }}</vue-markdown>
  32. </div>
  33. <div class="flex mt-5">
  34. <button @click="askGpt">Chat</button>
  35. <input type="text" class="bg-red-500 w-full" @change="handleChange" :value="input" />
  36. </div>
  37. </div>
  38. </template>
英文:

I am a backend developer and trying to get a foot onto vuejs.

I am trying to write a frontend, which contacts the OpenAI API and gives back reponses from GPT-4.
This works perfectly fine, but GPT-4 also returns reponses in markdown.

I cannot find out how to do that in my specific case.

I tried to use the following npm package: https://www.npmjs.com/package/vue-markdown
After using this in my code, I get the following (Please don't judge the design 😅): screenshot of result with the vue-markdown npm package

Here is the code I wrote in my Chat.vue file:

  1. &lt;script lang=&quot;ts&quot;&gt;
  2. import { GPTMessage } from &#39;../types/GPTMessage&#39;;
  3. import { ChatAPI } from &#39;../lib/gpt&#39;;
  4. import VueMarkdown from &#39;vue-markdown-render&#39;
  5. export default {
  6. data() {
  7. return {
  8. chatHistory: [] as GPTMessage[],
  9. input: &quot;&quot; as string
  10. }
  11. },
  12. components: {
  13. VueMarkdown
  14. },
  15. methods: {
  16. async askGpt(): Promise&lt;void&gt; {
  17. this.chatHistory.push({ role: &quot;user&quot;, content: this.input })
  18. this.input = &quot;&quot;
  19. const resp = await ChatAPI(this.chatHistory)
  20. this.chatHistory.push({ role: resp.role, content: resp.content })
  21. },
  22. handleChange(event: any) {
  23. this.input = event.target.value
  24. }
  25. }
  26. }
  27. &lt;/script&gt;
  28. &lt;template&gt;
  29. &lt;div class=&quot;flex flex-col h-screen justify-between mb-auto&quot;&gt;
  30. &lt;div class=&quot;mb-auto&quot;&gt;
  31. &lt;vue-markdown v-for=&quot;message in chatHistory&quot;&gt;{{ message.content }}&lt;/vue-markdown&gt;
  32. &lt;/div&gt;
  33. &lt;div class=&quot;flex mt-5&quot;&gt;
  34. &lt;button @click=&quot;askGpt&quot;&gt;Chat&lt;/button&gt;
  35. &lt;input type=&quot;text&quot; class=&quot;bg-red-500 w-full&quot; @change=&quot;handleChange&quot; :value=&quot;input&quot; /&gt;
  36. &lt;/div&gt;
  37. &lt;/div&gt;
  38. &lt;/template&gt;

答案1

得分: 0

你需要循环遍历每条带有内容的消息,并以以下方式在 Markdown 中呈现:

  1. <div v-for="message in chatHistory" :key="message.content">
  2. <vue-markdown :source="message.content"></vue-markdown>
  3. </div>
英文:

you need to loop through each message with the content and render it in the markdown like so :

  1. &lt;div v-for=&quot;message in chatHistory&quot; :key=&quot;message.content&quot;&gt;
  2. &lt;vue-markdown :source=&quot;message.content&quot;&gt;&lt;/vue-markdown&gt;
  3. &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年8月8日 22:44:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定