Skip to content

Commit

Permalink
feat: add preference to hide emojis in usernames (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnyTheCarrot authored Feb 4, 2023
1 parent 0258894 commit e92d1c6
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 6 deletions.
3 changes: 3 additions & 0 deletions components/account/AccountDisplayName.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import type { mastodon } from 'masto'
defineProps<{
account: mastodon.v1.Account
}>()
const userSettings = useUserSettings()
</script>

<template>
<ContentRich
:content="getDisplayName(account, { rich: true })"
:emojis="account.emojis"
:show-emojis="!getPreferences(userSettings, 'hideUsernameEmojis')"
:markdown="false"
/>
</template>
3 changes: 3 additions & 0 deletions components/content/ContentRich.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ defineOptions({
const {
content,
emojis,
showEmojis = true,
markdown = true,
} = defineProps<{
content: string
emojis?: mastodon.v1.CustomEmoji[]
showEmojis?: boolean
markdown?: boolean
}>()

Expand All @@ -21,6 +23,7 @@ export default () => h(
{ class: 'content-rich', dir: 'auto' },
contentToVNode(content, {
emojis: emojisObject.value,
showEmojis,
markdown,
}),
)
57 changes: 53 additions & 4 deletions composables/content-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { emojiRegEx, getEmojiAttributes } from '../config/emojis'

export interface ContentParseOptions {
emojis?: Record<string, mastodon.v1.CustomEmoji>
showEmojis?: boolean
mentions?: mastodon.v1.StatusMention[]
markdown?: boolean
replaceUnicodeEmoji?: boolean
Expand Down Expand Up @@ -81,6 +82,7 @@ export function parseMastodonHTML(
replaceUnicodeEmoji = true,
convertMentionLink = false,
collapseMentionLink = false,
showEmojis = true,
mentions,
status,
inReplyToStatus,
Expand Down Expand Up @@ -108,8 +110,16 @@ export function parseMastodonHTML(
...options.astTransforms || [],
]

if (replaceUnicodeEmoji)
transforms.push(transformUnicodeEmoji)
if (showEmojis) {
if (replaceUnicodeEmoji)
transforms.push(transformUnicodeEmoji)

transforms.push(replaceCustomEmoji(options.emojis ?? {}))
}
else {
transforms.push(removeUnicodeEmoji)
transforms.push(removeCustomEmoji(options.emojis ?? {}))
}

if (markdown)
transforms.push(transformMarkdown)
Expand All @@ -120,8 +130,6 @@ export function parseMastodonHTML(
if (convertMentionLink)
transforms.push(transformMentionLink)

transforms.push(replaceCustomEmoji(options.emojis || {}))

transforms.push(transformParagraphs)

if (collapseMentionLink)
Expand Down Expand Up @@ -329,6 +337,25 @@ function filterHref() {
}
}

function removeUnicodeEmoji(node: Node) {
if (node.type !== TEXT_NODE)
return node

let start = 0

const matches = [] as (string | Node)[]
findAndReplaceEmojisInText(emojiRegEx, node.value, (match, result) => {
matches.push(result.slice(start).trimEnd())
start = result.length + match.match.length
return undefined
})
if (matches.length === 0)
return node

matches.push(node.value.slice(start))
return matches.filter(Boolean)
}

function transformUnicodeEmoji(node: Node) {
if (node.type !== TEXT_NODE)
return node
Expand All @@ -350,6 +377,28 @@ function transformUnicodeEmoji(node: Node) {
return matches.filter(Boolean)
}

function removeCustomEmoji(customEmojis: Record<string, mastodon.v1.CustomEmoji>): Transform {
return (node) => {
if (node.type !== TEXT_NODE)
return node

const split = node.value.split(/\s?:([\w-]+?):/g)
if (split.length === 1)
return node

return split.map((name, i) => {
if (i % 2 === 0)
return name

const emoji = customEmojis[name] as mastodon.v1.CustomEmoji
if (!emoji)
return `:${name}:`

return ''
}).filter(Boolean)
}
}

function replaceCustomEmoji(customEmojis: Record<string, mastodon.v1.CustomEmoji>): Transform {
return (node) => {
if (node.type !== TEXT_NODE)
Expand Down
17 changes: 16 additions & 1 deletion composables/content-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@ import ContentCode from '~/components/content/ContentCode.vue'
import ContentMentionGroup from '~/components/content/ContentMentionGroup.vue'
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'

function getTexualAstComponents(astChildren: Node[]): string {
return astChildren
.filter(({ type }) => type === TEXT_NODE)
.map(({ value }) => value)
.reduce((accumulator, current) => accumulator + current, '')
.trim()
}

/**
* Raw HTML to VNodes
*/
export function contentToVNode(
content: string,
options?: ContentParseOptions,
): VNode {
const tree = parseMastodonHTML(content, options)
let tree = parseMastodonHTML(content, options)

const textContents = getTexualAstComponents(tree.children)

// if the username only contains emojis, we should probably show the emojis anyway to avoid a blank name
if (!options?.showEmojis && textContents.length === 0)
tree = parseMastodonHTML(content, { ...options, showEmojis: true })

return h(Fragment, (tree.children as Node[] || []).map(n => treeToVNode(n)))
}

Expand Down
2 changes: 2 additions & 0 deletions composables/settings/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PreferencesSettings {
hideFavoriteCount: boolean
hideFollowerCount: boolean
hideTranslation: boolean
hideUsernameEmojis: boolean
hideAccountHoverCard: boolean
grayscaleMode: boolean
enableAutoplay: boolean
Expand Down Expand Up @@ -72,6 +73,7 @@ export const DEFAULT__PREFERENCES_SETTINGS: PreferencesSettings = {
hideFavoriteCount: false,
hideFollowerCount: false,
hideTranslation: false,
hideUsernameEmojis: false,
hideAccountHoverCard: false,
grayscaleMode: false,
enableAutoplay: true,
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@
"hide_follower_count": "Hide follower count",
"hide_reply_count": "Hide reply count",
"hide_translation": "Hide translation",
"hide_username_emojis": "Hide username emojis",
"label": "Preferences",
"title": "Experimental Features",
"user_picker": "User Picker",
Expand Down
8 changes: 7 additions & 1 deletion pages/[[server]]/@[account]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const { t } = useI18n()
const { data: account, pending, refresh } = $(await useAsyncData(() => fetchAccountByHandle(accountName).catch(() => null), { immediate: process.client, default: () => shallowRef() }))
const relationship = $computed(() => account ? useRelationship(account).value : undefined)
const userSettings = useUserSettings()
onReactivated(() => {
// Silently update data when reentering the page
// The user will see the previous content first, and any changes will be updated to the UI when the request is completed
Expand All @@ -21,7 +23,11 @@ onReactivated(() => {
<template>
<MainContent back>
<template #title>
<ContentRich timeline-title-style :content="account ? getDisplayName(account) : t('nav.profile')" />
<ContentRich
timeline-title-style
:content="account ? getDisplayName(account) : t('nav.profile')"
:show-emojis="!getPreferences(userSettings, 'hideUsernameEmojis')"
/>
</template>

<template v-if="pending" />
Expand Down
6 changes: 6 additions & 0 deletions pages/settings/preferences/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ const userSettings = useUserSettings()
>
{{ $t('settings.preferences.hide_follower_count') }}
</SettingsToggleItem>
<SettingsToggleItem
:checked="getPreferences(userSettings, 'hideUsernameEmojis')"
@click="togglePreferences('hideUsernameEmojis')"
>
{{ $t("settings.preferences.hide_username_emojis") }}
</SettingsToggleItem>
<h2 px6 py4 mt2 font-bold text-xl flex="~ gap-1" items-center>
<div i-ri-flask-line />
{{ $t('settings.preferences.title') }}
Expand Down

1 comment on commit e92d1c6

@Geobert
Copy link

@Geobert Geobert commented on e92d1c6 Feb 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this!

Please sign in to comment.