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 PostActions from '$lib/components/lemmy/post/PostActions.svelte'
import { userSettings } from '$lib/settings.js' import { userSettings } from '$lib/settings.js'
import PostLink from '$lib/components/lemmy/post/PostLink.svelte' 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 { Badge, Material, toast } from 'mono-svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte' import Markdown from '$lib/components/markdown/Markdown.svelte'
import ExpandableImage from '$lib/components/ui/ExpandableImage.svelte' import ExpandableImage from '$lib/components/ui/ExpandableImage.svelte'
@@ -34,6 +37,10 @@
export let view = $userSettings.view export let view = $userSettings.view
$: type = mediaType(post.post.url, view) $: type = mediaType(post.post.url, view)
$: tags = parseTags(post.post.name)
$: rule = getTagRule(tags.tags)
$: hideTitle = $: hideTitle =
$userSettings.posts.deduplicateEmbed && $userSettings.posts.deduplicateEmbed &&
post.post.embed_title == post.post.name && post.post.embed_title == post.post.name &&
@@ -45,6 +52,19 @@
post.post.embed_description == post.post.body && post.post.embed_description == post.post.body &&
view != 'compact' 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) { function onClick(e: any) {
const event = e as MouseEvent const event = e as MouseEvent
const parent = document.getElementById(post.post.id.toString()) const parent = document.getElementById(post.post.id.toString())
@@ -77,7 +97,8 @@
}} }}
> >
<PostMeta <PostMeta
community={hideCommunity ? undefined : post.community} community={post.community}
showCommunity={!hideCommunity}
user={post.creator} user={post.creator}
published={publishedToDate(post.post.published)} published={publishedToDate(post.post.published)}
badges={{ badges={{
@@ -96,9 +117,10 @@
? 'Subscribed' ? 'Subscribed'
: 'NotSubscribed'} : 'NotSubscribed'}
id={post.post.id} id={post.post.id}
title={hideTitle ? undefined : post.post.name} title={hideTitle ? undefined : tags?.title ? tags.title : post.post.name}
read={post.read} read={post.read}
style="grid-area: meta;" style="grid-area: meta;"
tags={tags?.tags}
> >
<slot name="badges" slot="badges" /> <slot name="badges" slot="badges" />
</PostMeta> </PostMeta>
@@ -107,13 +129,20 @@
style="grid-area:embed;" style="grid-area:embed;"
class={view == 'list' || view == 'compact' ? '' : 'contents'} 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> </div>
{#if view == 'list' || view == 'compact'} {#if view == 'list' || view == 'compact'}
<PostMediaCompact <PostMediaCompact
{view} {view}
bind:post={post.post} bind:post={post.post}
bind:type={type} bind:type
class="{$userSettings.leftAlign class="{$userSettings.leftAlign
? 'mr-2' ? 'mr-2'
: 'ml-2'} flex-shrink no-list-margin" : 'ml-2'} flex-shrink no-list-margin"
@@ -121,7 +150,7 @@
/> />
{/if} {/if}
{/key} {/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" /> <PostBody body={post.post.body} {view} style="grid-area: body" />
{/if} {/if}
{#if actions} {#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"> <script lang="ts">
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte' import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte' import Avatar from '$lib/components/ui/Avatar.svelte'
@@ -13,6 +49,7 @@
LockClosed, LockClosed,
Megaphone, Megaphone,
Plus, Plus,
Tag,
Trash, Trash,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { getInstance } from '$lib/lemmy.js' import { getInstance } from '$lib/lemmy.js'
@@ -22,8 +59,10 @@
import { userSettings } from '$lib/settings' import { userSettings } from '$lib/settings'
import Markdown from '$lib/components/markdown/Markdown.svelte' import Markdown from '$lib/components/markdown/Markdown.svelte'
import { t } from '$lib/translations' import { t } from '$lib/translations'
import type { IconSource } from 'svelte-hero-icons'
export let community: Community | undefined = undefined export let community: Community | undefined = undefined
export let showCommunity: boolean = true
export let subscribed: SubscribedType | undefined = undefined export let subscribed: SubscribedType | undefined = undefined
export let user: Person | undefined = undefined export let user: Person | undefined = undefined
@@ -43,6 +82,16 @@
moderator: false, moderator: false,
admin: false, admin: false,
} }
export let tags: Tag[] = []
$: {
if (title && tags.length == 0) {
const result = parseTags(title)
tags = result.tags
title = result.title
}
}
</script> </script>
<!-- <!--
@@ -55,7 +104,7 @@
: 'grid-rows-1'} text-xs min-w-0 max-w-full" : 'grid-rows-1'} text-xs min-w-0 max-w-full"
style={$$props.style ?? ''} style={$$props.style ?? ''}
> >
{#if community} {#if showCommunity && community}
<Subscribe let:subscribe let:subscribing> <Subscribe let:subscribe let:subscribing>
<button <button
on:click={async () => { on:click={async () => {
@@ -91,7 +140,7 @@
</button> </button>
</Subscribe> </Subscribe>
{/if} {/if}
{#if community} {#if showCommunity && community}
<CommunityLink <CommunityLink
{community} {community}
style="grid-area: community;" style="grid-area: community;"
@@ -103,7 +152,7 @@
style="grid-area: stats;" style="grid-area: stats;"
> >
{#if user} {#if user}
<UserLink avatarSize={20} {user} avatar={!community}> <UserLink avatarSize={20} {user} avatar={!showCommunity}>
<svelte:fragment slot="badges"> <svelte:fragment slot="badges">
{#if badges.moderator} {#if badges.moderator}
<ShieldIcon filled width={14} class="text-green-500" /> <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" class="flex flex-row items-center self-center flex-wrap gap-2 [&>*]:flex-shrink-0 badges"
style="grid-area: 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} {#if badges.nsfw}
<Badge label={$t('post.badges.nsfw')} color="red-subtle" allowIconOnly> <Badge label={$t('post.badges.nsfw')} color="red-subtle" allowIconOnly>
<Icon src={ExclamationTriangle} size="14" micro slot="icon" /> <Icon src={ExclamationTriangle} size="14" micro slot="icon" />
@@ -201,4 +270,8 @@
grid-template-rows: auto auto; grid-template-rows: auto auto;
grid-template-columns: auto 1fr auto; grid-template-columns: auto 1fr auto;
} }
:global(.badge-tag-color) {
background-color: var(--tag-color) !important;
}
</style> </style>
@@ -19,6 +19,7 @@
export let post: Post export let post: Post
export let type: MediaType = 'none' export let type: MediaType = 'none'
export let opened: boolean | undefined = undefined export let opened: boolean | undefined = undefined
export let blur: boolean = post.nsfw && $userSettings.nsfwBlur
let showAltText = false let showAltText = false
</script> </script>
@@ -74,7 +75,7 @@
transition-opacity duration-300 object-contain mx-auto" transition-opacity duration-300 object-contain mx-auto"
width={512} width={512}
height={300} height={300}
class:blur-3xl={post.nsfw && $userSettings.nsfwBlur} class:blur-3xl={blur}
alt={post.alt_text ?? ''} alt={post.alt_text ?? ''}
/> />
</picture> </picture>
+10
View File
@@ -636,6 +636,16 @@
"title": "Translation", "title": "Translation",
"description": "The LibreTranslate instance to use for translation.", "description": "The LibreTranslate instance to use for translation.",
"instance": "Domain" "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": { "embeds": {
+10
View File
@@ -91,6 +91,10 @@ interface Settings {
language: string | null language: string | null
useRtl: boolean useRtl: boolean
translator: string | undefined translator: string | undefined
parseTags: boolean
tagRules: {
[key: string]: 'hide' | 'blur'
}
} }
export const defaultSettings: Settings = { export const defaultSettings: Settings = {
@@ -159,6 +163,12 @@ export const defaultSettings: Settings = {
language: env.PUBLIC_LANGUAGE ?? null, language: env.PUBLIC_LANGUAGE ?? null,
useRtl: false, useRtl: false,
translator: env.PUBLIC_TRANSLATOR ?? undefined, translator: env.PUBLIC_TRANSLATOR ?? undefined,
parseTags: true,
tagRules: {
cw: 'blur',
nsfl: 'blur',
nsfw: 'blur',
},
} }
export const userSettings = writable(defaultSettings) export const userSettings = writable(defaultSettings)
@@ -190,6 +190,7 @@
moderator: post.post_view.creator_is_moderator, moderator: post.post_view.creator_is_moderator,
}} }}
published={publishedToDate(post.post_view.post.published)} published={publishedToDate(post.post_view.post.published)}
bind:title={post.post_view.post.name}
/> />
</div> </div>
<Button on:click={() => history.back()} size="square-md"> <Button on:click={() => history.back()} size="square-md">
+29
View File
@@ -295,6 +295,35 @@
pattern={DOMAIN_REGEX_FORMS} pattern={DOMAIN_REGEX_FORMS}
/> />
</Setting> </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>
<Section title={$t('settings.embeds.title')}> <Section title={$t('settings.embeds.title')}>