From abf52a8f01d7ff136d202fe3664f9b57084b658e Mon Sep 17 00:00:00 2001 From: Xylight Date: Sun, 7 Jul 2024 12:55:14 -0700 Subject: [PATCH] feat: add basic flair parsing --- src/lib/components/lemmy/post/PostMeta.svelte | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/lib/components/lemmy/post/PostMeta.svelte b/src/lib/components/lemmy/post/PostMeta.svelte index f0a32c98..35ba01ef 100644 --- a/src/lib/components/lemmy/post/PostMeta.svelte +++ b/src/lib/components/lemmy/post/PostMeta.svelte @@ -22,6 +22,7 @@ import { userSettings } from '$lib/settings' import Markdown from '$lib/components/markdown/Markdown.svelte' import { t } from '$lib/translations' + import type { IconSource } from 'svelte-hero-icons' export let community: Community | undefined = undefined export let subscribed: SubscribedType | undefined = undefined @@ -43,6 +44,48 @@ moderator: false, admin: false, } + + interface Tag { + content: string + color?: string + icon?: IconSource + } + + const textToTag: Map = new Map([ + ['OC', { content: 'OC', color: '#03A8F240' }], + ['NSFL', { content: 'NSFL', color: '#ff000040' }], + ['CW', { content: 'CW', color: '#ff000040' }], + ]) + + const parseTags = (title?: string): { tags: Tag[]; title?: string } => { + if (!title) return { tags: [] } + + let extracted: Tag[] = [] + + title = title.replace(/\[(.*?)\]/g, (match, content) => { + extracted.push( + textToTag.get(content) ?? { + content: content, + } + ) + return '' + }) + + return { + tags: extracted, + title: title, + } + } + + let tags: Tag[] = [] + + $: { + if (title) { + const result = parseTags(title) + tags = result.tags + title = result.title + } + }