Merge pull request #356 from Xyphyn/flairs

feat: parse tags from title
This commit is contained in:
Xylight authored and GitHub committed 2024-07-09 13:42:00 -07:00
commit 2da953dd79
7 files changed
+163 -10

No files matched your search

+35 -6
View File
@@ -5,7 +5,10 @@
import PostActions from '$lib/components/lemmy/post/PostActions.svelte'
import { userSettings } from '$lib/settings.js'
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
import PostMeta, {
parseTags,
type Tag,
} from '$lib/components/lemmy/post/PostMeta.svelte'
import { Badge, Material, toast } from 'mono-svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import ExpandableImage from '$lib/components/ui/ExpandableImage.svelte'
@@ -34,6 +37,10 @@
export let view = $userSettings.view
$: type = mediaType(post.post.url, view)
$: tags = parseTags(post.post.name)
$: rule = getTagRule(tags.tags)
$: hideTitle =
$userSettings.posts.deduplicateEmbed &&
post.post.embed_title == post.post.name &&
@@ -45,6 +52,19 @@
post.post.embed_description == post.post.body &&
view != 'compact'
function getTagRule(tags: Tag[]): 'blur' | 'hide' | undefined {
const tagContent = tags.map((t) => t.content.toLowerCase())
let rule: 'blur' | 'hide' | undefined
tagContent.forEach((tag) => {
if ($userSettings.tagRules?.[tag])
rule = $userSettings.tagRules?.[tag] ?? rule
if (rule == 'hide') return rule
})
return rule
}
function onClick(e: any) {
const event = e as MouseEvent
const parent = document.getElementById(post.post.id.toString())
@@ -77,7 +97,8 @@
}}
>
<PostMeta
community={hideCommunity ? undefined : post.community}
community={post.community}
showCommunity={!hideCommunity}
user={post.creator}
published={publishedToDate(post.post.published)}
badges={{
@@ -96,9 +117,10 @@
? 'Subscribed'
: 'NotSubscribed'}
id={post.post.id}
title={hideTitle ? undefined : post.post.name}
title={hideTitle ? undefined : tags?.title ? tags.title : post.post.name}
read={post.read}
style="grid-area: meta;"
tags={tags?.tags}
>
<slot name="badges" slot="badges" />
</PostMeta>
@@ -107,13 +129,20 @@
style="grid-area:embed;"
class={view == 'list' || view == 'compact' ? '' : 'contents'}
>
<PostMedia bind:post={post.post} {view} {type} />
{#if rule != 'hide'}
<PostMedia
bind:post={post.post}
blur={rule == 'blur' ? true : undefined}
{view}
{type}
/>
{/if}
</div>
{#if view == 'list' || view == 'compact'}
<PostMediaCompact
{view}
bind:post={post.post}
bind:type={type}
bind:type
class="{$userSettings.leftAlign
? 'mr-2'
: 'ml-2'} flex-shrink no-list-margin"
@@ -121,7 +150,7 @@
/>
{/if}
{/key}
{#if post.post.body && !post.post.nsfw && view != 'compact' && !hideBody}
{#if post.post.body && !post.post.nsfw && view != 'compact' && !hideBody && rule != 'hide'}
<PostBody body={post.post.body} {view} style="grid-area: body" />
{/if}
{#if actions}
+76 -3
View File
@@ -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'
@@ -22,8 +59,10 @@
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 showCommunity: boolean = true
export let subscribed: SubscribedType | undefined = undefined
export let user: Person | undefined = undefined
@@ -43,6 +82,16 @@
moderator: false,
admin: false,
}
export let tags: Tag[] = []
$: {
if (title && tags.length == 0) {
const result = parseTags(title)
tags = result.tags
title = result.title
}
}
</script>
<!--
@@ -55,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 () => {
@@ -91,7 +140,7 @@
</button>
</Subscribe>
{/if}
{#if community}
{#if showCommunity && community}
<CommunityLink
{community}
style="grid-area: community;"
@@ -103,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" />
@@ -122,6 +171,26 @@
class="flex flex-row items-center self-center flex-wrap gap-2 [&>*]:flex-shrink-0 badges"
style="grid-area: badges;"
>
{#if tags}
{#each tags as tag}
<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" />
{:else}
<Icon src={Tag} micro size="14" />
{/if}
</svelte:fragment>
{tag.content}
</Badge>
</a>
{/each}
{/if}
{#if badges.nsfw}
<Badge label={$t('post.badges.nsfw')} color="red-subtle" allowIconOnly>
<Icon src={ExclamationTriangle} size="14" micro slot="icon" />
@@ -201,4 +270,8 @@
grid-template-rows: auto auto;
grid-template-columns: auto 1fr auto;
}
:global(.badge-tag-color) {
background-color: var(--tag-color) !important;
}
</style>
@@ -19,6 +19,7 @@
export let post: Post
export let type: MediaType = 'none'
export let opened: boolean | undefined = undefined
export let blur: boolean = post.nsfw && $userSettings.nsfwBlur
let showAltText = false
</script>
@@ -74,7 +75,7 @@
transition-opacity duration-300 object-contain mx-auto"
width={512}
height={300}
class:blur-3xl={post.nsfw && $userSettings.nsfwBlur}
class:blur-3xl={blur}
alt={post.alt_text ?? ''}
/>
</picture>
+10
View File
@@ -636,6 +636,16 @@
"title": "Translation",
"description": "The LibreTranslate instance to use for translation.",
"instance": "Domain"
},
"titleTags": {
"title": "Parse tags in title",
"description": "Take text in [brackets] and turn it into tags that you can click to search for.",
"rules": {
"title": "Rules",
"blur": "Blur",
"hide": "Hide",
"show": "Show"
}
}
},
"embeds": {
+10
View File
@@ -91,6 +91,10 @@ interface Settings {
language: string | null
useRtl: boolean
translator: string | undefined
parseTags: boolean
tagRules: {
[key: string]: 'hide' | 'blur'
}
}
export const defaultSettings: Settings = {
@@ -159,6 +163,12 @@ export const defaultSettings: Settings = {
language: env.PUBLIC_LANGUAGE ?? null,
useRtl: false,
translator: env.PUBLIC_TRANSLATOR ?? undefined,
parseTags: true,
tagRules: {
cw: 'blur',
nsfl: 'blur',
nsfw: 'blur',
},
}
export const userSettings = writable(defaultSettings)
@@ -190,6 +190,7 @@
moderator: post.post_view.creator_is_moderator,
}}
published={publishedToDate(post.post_view.post.published)}
bind:title={post.post_view.post.name}
/>
</div>
<Button on:click={() => history.back()} size="square-md">
+29
View File
@@ -295,6 +295,35 @@
pattern={DOMAIN_REGEX_FORMS}
/>
</Setting>
<div>
<ToggleSetting
title={$t('settings.app.titleTags.title')}
description={$t('settings.app.titleTags.description')}
bind:checked={$userSettings.parseTags}
/>
<Setting>
<svelte:fragment slot="title">
{$t('settings.app.titleTags.rules.title')}
</svelte:fragment>
<div
class="flex flex-col divide-y [&>*]:py-2 items-end divide-slate-200 dark:divide-zinc-800"
>
{#each Object.keys($userSettings.tagRules) as rule}
<div class="flex flex-row flex-wrap items-center gap-2">
<span class="text-lg font-medium">{rule}</span>
<MultiSelect
bind:selected={$userSettings.tagRules[rule]}
options={['blur', 'hide']}
optionNames={[
$t('settings.app.titleTags.rules.blur'),
$t('settings.app.titleTags.rules.hide'),
]}
/>
</div>
{/each}
</div>
</Setting>
</div>
</Section>
<Section title={$t('settings.embeds.title')}>