Source.svelte 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <script lang="ts">
  2. export let token;
  3. export let onClick: Function = () => {};
  4. let attributes: Record<string, string | undefined> = {};
  5. function extractAttributes(input: string): Record<string, string> {
  6. const regex = /(\w+)="([^"]*)"/g;
  7. let match;
  8. let attrs: Record<string, string> = {};
  9. // Loop through all matches and populate the attributes object
  10. while ((match = regex.exec(input)) !== null) {
  11. attrs[match[1]] = match[2];
  12. }
  13. return attrs;
  14. }
  15. // Helper function to return only the domain from a URL
  16. function getDomain(url: string): string {
  17. const domain = url.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
  18. return domain;
  19. }
  20. // Helper function to check if text is a URL and return the domain
  21. function formattedTitle(title: string): string {
  22. if (title.startsWith('http')) {
  23. return getDomain(title);
  24. }
  25. return title;
  26. }
  27. $: attributes = extractAttributes(token.text);
  28. </script>
  29. <button
  30. class="text-xs font-medium w-fit translate-y-[2px] px-2 py-0.5 dark:bg-white/5 dark:text-white/60 dark:hover:text-white bg-gray-50 text-black/60 hover:text-black transition rounded-lg"
  31. on:click={() => {
  32. onClick(attributes.data);
  33. }}
  34. >
  35. <span class="line-clamp-1">
  36. {attributes.title ? formattedTitle(attributes.title) : ''}
  37. </span>
  38. </button>