feat: add tag rules
This commit is contained in:
1 parent
abf52a8f01
commit
3e563ad1cc
6 files changed
+102
-46
No files matched your search
@@ -1,3 +1,39 @@
|
||||
<script lang="ts" context="module">
|
||||
export interface Tag {
|
||||
content: string
|
||||
color?: string
|
||||
icon?: IconSource
|
||||
}
|
||||
|
||||
export const textToTag: Map<string, Tag> = new Map<string, Tag>([
|
||||
['OC', { content: 'OC', color: '#03A8F240' }],
|
||||
['NSFL', { content: 'NSFL', color: '#ff000040' }],
|
||||
['CW', { content: 'CW', color: '#ff000040' }],
|
||||
])
|
||||
|
||||
export 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,
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||
@@ -13,6 +49,7 @@
|
||||
LockClosed,
|
||||
Megaphone,
|
||||
Plus,
|
||||
Tag,
|
||||
Trash,
|
||||
} from 'svelte-hero-icons'
|
||||
import { getInstance } from '$lib/lemmy.js'
|
||||
@@ -25,6 +62,7 @@
|
||||
import type { IconSource } from 'svelte-hero-icons'
|
||||
|
||||
export let community: Community | undefined = undefined
|
||||
export let showCommunity: boolean = true
|
||||
export let subscribed: SubscribedType | undefined = undefined
|
||||
|
||||
export let user: Person | undefined = undefined
|
||||
@@ -45,42 +83,10 @@
|
||||
admin: false,
|
||||
}
|
||||
|
||||
interface Tag {
|
||||
content: string
|
||||
color?: string
|
||||
icon?: IconSource
|
||||
}
|
||||
|
||||
const textToTag: Map<string, Tag> = new Map<string, Tag>([
|
||||
['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[] = []
|
||||
export let tags: Tag[] = []
|
||||
|
||||
$: {
|
||||
if (title) {
|
||||
if (title && tags.length == 0) {
|
||||
const result = parseTags(title)
|
||||
tags = result.tags
|
||||
title = result.title
|
||||
@@ -98,7 +104,7 @@
|
||||
: 'grid-rows-1'} text-xs min-w-0 max-w-full"
|
||||
style={$$props.style ?? ''}
|
||||
>
|
||||
{#if community}
|
||||
{#if showCommunity && community}
|
||||
<Subscribe let:subscribe let:subscribing>
|
||||
<button
|
||||
on:click={async () => {
|
||||
@@ -134,7 +140,7 @@
|
||||
</button>
|
||||
</Subscribe>
|
||||
{/if}
|
||||
{#if community}
|
||||
{#if showCommunity && community}
|
||||
<CommunityLink
|
||||
{community}
|
||||
style="grid-area: community;"
|
||||
@@ -146,7 +152,7 @@
|
||||
style="grid-area: stats;"
|
||||
>
|
||||
{#if user}
|
||||
<UserLink avatarSize={20} {user} avatar={!community}>
|
||||
<UserLink avatarSize={20} {user} avatar={!showCommunity}>
|
||||
<svelte:fragment slot="badges">
|
||||
{#if badges.moderator}
|
||||
<ShieldIcon filled width={14} class="text-green-500" />
|
||||
@@ -167,14 +173,22 @@
|
||||
>
|
||||
{#if tags}
|
||||
{#each tags as tag}
|
||||
<div style={tag.color ? `--tag-color: ${tag.color};` : ''}>
|
||||
<a
|
||||
class="hover:brightness-110"
|
||||
href="/search?community={community?.id}&q=[{tag.content}]&type=Posts"
|
||||
style={tag.color ? `--tag-color: ${tag.color};` : ''}
|
||||
>
|
||||
<Badge class={tag.color ? 'badge-tag-color' : ''}>
|
||||
<svelte:fragment slot="icon">
|
||||
{#if tag.icon}<Icon src={tag.icon} micro size="14" />{/if}
|
||||
{#if tag.icon}
|
||||
<Icon src={tag.icon} micro size="14" />
|
||||
{:else}
|
||||
<Icon src={Tag} micro size="14" />
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
{tag.content}
|
||||
</Badge>
|
||||
</div>
|
||||
</a>
|
||||
{/each}
|
||||
{/if}
|
||||
{#if badges.nsfw}
|
||||
|
||||
Reference in new issue
Block a user