2024-11-22 01:58:29 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
export let token;
|
|
|
|
|
export let onClick: Function = () => {};
|
|
|
|
|
|
2025-02-19 00:47:56 +00:00
|
|
|
let attributes: Record<string, string | undefined> = {};
|
2025-02-13 07:55:14 +00:00
|
|
|
|
|
|
|
|
function extractAttributes(input: string): Record<string, string> {
|
|
|
|
|
const regex = /(\w+)="([^"]*)"/g;
|
|
|
|
|
let match;
|
|
|
|
|
let attrs: Record<string, string> = {};
|
|
|
|
|
|
|
|
|
|
// Loop through all matches and populate the attributes object
|
|
|
|
|
while ((match = regex.exec(input)) !== null) {
|
|
|
|
|
attrs[match[1]] = match[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attrs;
|
2024-11-22 01:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-16 01:24:29 +00:00
|
|
|
// Helper function to return only the domain from a URL
|
|
|
|
|
function getDomain(url: string): string {
|
|
|
|
|
const domain = url.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
|
|
|
|
|
return domain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper function to check if text is a URL and return the domain
|
|
|
|
|
function formattedTitle(title: string): string {
|
|
|
|
|
if (title.startsWith('http')) {
|
|
|
|
|
return getDomain(title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return title;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 07:55:14 +00:00
|
|
|
$: attributes = extractAttributes(token.text);
|
2024-11-22 01:58:29 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-02-18 12:56:16 +00:00
|
|
|
{#if attributes.title !== 'N/A'}
|
|
|
|
|
<button
|
|
|
|
|
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"
|
|
|
|
|
on:click={() => {
|
|
|
|
|
onClick(attributes.data);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<span class="line-clamp-1">
|
2025-02-19 03:37:03 +00:00
|
|
|
{attributes.title ? formattedTitle(attributes.title) : ''}
|
2025-02-18 12:56:16 +00:00
|
|
|
</span>
|
|
|
|
|
</button>
|
|
|
|
|
{/if}
|