MarkdownInlineTokens.svelte 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <script lang="ts">
  2. import DOMPurify from 'dompurify';
  3. import type { Token } from 'marked';
  4. import { revertSanitizedResponseContent, unescapeHtml } from '$lib/utils';
  5. import { onMount } from 'svelte';
  6. import Image from '$lib/components/common/Image.svelte';
  7. import KatexRenderer from './KatexRenderer.svelte';
  8. export let id: string;
  9. export let tokens: Token[];
  10. </script>
  11. {#each tokens as token}
  12. {#if token.type === 'escape'}
  13. {unescapeHtml(token.text)}
  14. {:else if token.type === 'html'}
  15. {@const html = DOMPurify.sanitize(token.text)}
  16. {#if html && html.includes('<video')}
  17. {@html html}
  18. {:else if token.text.includes(`<iframe src="${WEBUI_BASE_URL}/api/v1/files/`)}
  19. {@html `${token.text}`}
  20. {:else}
  21. {token.text}
  22. {/if}
  23. {:else if token.type === 'link'}
  24. <a href={token.href} target="_blank" rel="nofollow" title={token.title}>{token.text}</a>
  25. {:else if token.type === 'image'}
  26. <Image src={token.href} alt={token.text} />
  27. {:else if token.type === 'strong'}
  28. <strong>
  29. <svelte:self id={`${id}-strong`} tokens={token.tokens} />
  30. </strong>
  31. {:else if token.type === 'em'}
  32. <em>
  33. <svelte:self id={`${id}-em`} tokens={token.tokens} />
  34. </em>
  35. {:else if token.type === 'codespan'}
  36. <code class="codespan">{token.text}</code>
  37. {:else if token.type === 'br'}
  38. <br />
  39. {:else if token.type === 'del'}
  40. <del>
  41. <svelte:self id={`${id}-del`} tokens={token.tokens} />
  42. </del>
  43. {:else if token.type === 'inlineKatex'}
  44. {#if token.text}
  45. <KatexRenderer content={revertSanitizedResponseContent(token.text)} displayMode={false} />
  46. {/if}
  47. {:else if token.type === 'iframe'}
  48. <iframe
  49. src="{WEBUI_BASE_URL}/api/v1/files/{token.fileId}/content"
  50. title={token.fileId}
  51. width="100%"
  52. frameborder="0"
  53. onload="this.style.height=(this.contentWindow.document.body.scrollHeight+20)+'px';"
  54. ></iframe>
  55. {:else if token.type === 'text'}
  56. {token.raw}
  57. {/if}
  58. {/each}