feat: add tag rules

This commit is contained in:
Xylight committed 2024-07-09 12:58:49 -07:00
1 parent abf52a8f01
commit 3e563ad1cc
6 files changed
+102 -46

No files matched your search

+34 -5
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"
+54 -40
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'
@@ -25,6 +62,7 @@
import type { IconSource } from 'svelte-hero-icons' 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
@@ -45,42 +83,10 @@
admin: false, admin: false,
} }
interface Tag { export let tags: 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[] = []
$: { $: {
if (title) { if (title && tags.length == 0) {
const result = parseTags(title) const result = parseTags(title)
tags = result.tags tags = result.tags
title = result.title title = result.title
@@ -98,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 () => {
@@ -134,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;"
@@ -146,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" />
@@ -167,14 +173,22 @@
> >
{#if tags} {#if tags}
{#each tags as tag} {#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' : ''}> <Badge class={tag.color ? 'badge-tag-color' : ''}>
<svelte:fragment slot="icon"> <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> </svelte:fragment>
{tag.content} {tag.content}
</Badge> </Badge>
</div> </a>
{/each} {/each}
{/if} {/if}
{#if badges.nsfw} {#if badges.nsfw}
@@ -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>
+4
View File
@@ -636,6 +636,10 @@
"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."
} }
}, },
"embeds": { "embeds": {
+7
View File
@@ -91,6 +91,9 @@ interface Settings {
language: string | null language: string | null
useRtl: boolean useRtl: boolean
translator: string | undefined translator: string | undefined
tagRules: {
[key: string]: 'hide' | 'blur'
}
} }
export const defaultSettings: Settings = { export const defaultSettings: Settings = {
@@ -159,6 +162,10 @@ 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,
tagRules: {
cw: 'blur',
nsfl: '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">