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 { 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"
+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">
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}
@@ -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>
+4
View File
@@ -636,6 +636,10 @@
"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."
}
},
"embeds": {
+7
View File
@@ -91,6 +91,9 @@ interface Settings {
language: string | null
useRtl: boolean
translator: string | undefined
tagRules: {
[key: string]: 'hide' | 'blur'
}
}
export const defaultSettings: Settings = {
@@ -159,6 +162,10 @@ export const defaultSettings: Settings = {
language: env.PUBLIC_LANGUAGE ?? null,
useRtl: false,
translator: env.PUBLIC_TRANSLATOR ?? undefined,
tagRules: {
cw: 'blur',
nsfl: '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">