tieba表情函数抽成共通

This commit is contained in:
smallclover
2025-10-15 22:47:48 +09:00
parent a8667ce5e9
commit 215c7077d5
3 changed files with 36 additions and 33 deletions

View File

@@ -76,7 +76,7 @@
{{ article.title }} {{ article.title }}
</NuxtLink> </NuxtLink>
<NuxtLink class="article-item-description main-item" :to="`/posts/${article.id}`"> <NuxtLink class="article-item-description main-item" :to="`/posts/${article.id}`">
<div v-html="sanitizeDescription(article.description)"></div> <div v-html="stripMarkdownWithTiebaMoji(article.description, 500)"></div>
</NuxtLink> </NuxtLink>
<div class="article-info-container main-item"> <div class="article-info-container main-item">
<ArticleCategory :category="article.category" /> <ArticleCategory :category="article.category" />
@@ -143,6 +143,7 @@ import { useIsMobile } from '~/utils/screen'
import BaseUserAvatar from '~/components/BaseUserAvatar.vue' import BaseUserAvatar from '~/components/BaseUserAvatar.vue'
import TimeManager from '~/utils/time' import TimeManager from '~/utils/time'
import { selectedCategoryGlobal, selectedTagsGlobal } from '~/composables/postFilter' import { selectedCategoryGlobal, selectedTagsGlobal } from '~/composables/postFilter'
import { stripMarkdownWithTiebaMoji } from '~/utils/markdown'
useHead({ useHead({
title: 'OpenIsle - 全面开源的自由社区', title: 'OpenIsle - 全面开源的自由社区',
meta: [ meta: [
@@ -378,27 +379,6 @@ onBeforeUnmount(() => {
/** 供 InfiniteLoadMore 重建用的 key筛选/Tab 改变即重建内部状态 */ /** 供 InfiniteLoadMore 重建用的 key筛选/Tab 改变即重建内部状态 */
const ioKey = computed(() => asyncKey.value.join('::')) const ioKey = computed(() => asyncKey.value.join('::'))
// 在首页摘要加载贴吧表情包
const sanitizeDescription = (text) => {
if (!text) return ''
// 1⃣ 先把 Markdown 转成纯文本
const plain = stripMarkdown(text)
// 2⃣ 替换 :tieba123: 为 <img>
const withEmoji = plain.replace(/:tieba(\d+):/g, (match, num) => {
const key = `tieba${num}`
const file = tiebaEmoji[key]
return file
? `<img loading="lazy" class="emoji" src="${file}" alt="${key}">`
: match // 没有匹配到图片则保留原样
})
// 3 可选截断纯文本长度防止撑太长
const truncated = withEmoji.length > 500 ? withEmoji.slice(0, 500) + '…' : withEmoji
return truncated
}
// 页面选项同步到全局状态 // 页面选项同步到全局状态
watch([selectedCategory, selectedTags], ([newCategory, newTags]) => { watch([selectedCategory, selectedTags], ([newCategory, newTags]) => {

View File

@@ -75,7 +75,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.parentComment.id}`" :to="`/posts/${item.post.id}#comment-${item.parentComment.id}`"
> >
{{ stripMarkdownLength(item.parentComment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.parentComment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</span> </span>
回复了 回复了
@@ -85,7 +85,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.comment.id}`" :to="`/posts/${item.post.id}#comment-${item.comment.id}`"
> >
{{ stripMarkdownLength(item.comment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.comment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</span> </span>
</NotificationContainer> </NotificationContainer>
@@ -115,7 +115,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.comment.id}`" :to="`/posts/${item.post.id}#comment-${item.comment.id}`"
> >
{{ stripMarkdownLength(item.comment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.comment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</span> </span>
</NotificationContainer> </NotificationContainer>
@@ -162,7 +162,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.comment.id}`" :to="`/posts/${item.post.id}#comment-${item.comment.id}`"
> >
{{ stripMarkdownLength(item.comment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.comment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</span> </span>
进行了表态 进行了表态
@@ -267,7 +267,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.comment.id}`" :to="`/posts/${item.post.id}#comment-${item.comment.id}`"
> >
{{ stripMarkdownLength(item.comment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.comment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</NotificationContainer> </NotificationContainer>
</template> </template>
@@ -287,7 +287,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.parentComment.id}`" :to="`/posts/${item.post.id}#comment-${item.parentComment.id}`"
> >
{{ stripMarkdownLength(item.parentComment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.parentComment.content, 500)"></div>
</NuxtLink> </NuxtLink>
回复了 回复了
<NuxtLink <NuxtLink
@@ -295,7 +295,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.comment.id}`" :to="`/posts/${item.post.id}#comment-${item.comment.id}`"
> >
{{ stripMarkdownLength(item.comment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.comment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</NotificationContainer> </NotificationContainer>
</template> </template>
@@ -323,7 +323,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.comment.id}`" :to="`/posts/${item.post.id}#comment-${item.comment.id}`"
> >
{{ stripMarkdownLength(item.comment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.comment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</NotificationContainer> </NotificationContainer>
</template> </template>
@@ -342,7 +342,7 @@
@click="markRead(item.id)" @click="markRead(item.id)"
:to="`/posts/${item.post.id}#comment-${item.comment.id}`" :to="`/posts/${item.post.id}#comment-${item.comment.id}`"
> >
{{ stripMarkdownLength(item.comment.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.comment.content, 500)"></div>
</NuxtLink> </NuxtLink>
</NotificationContainer> </NotificationContainer>
</template> </template>
@@ -556,7 +556,7 @@
</template> </template>
删除了您的帖子 删除了您的帖子
<span class="notif-content-text"> <span class="notif-content-text">
{{ stripMarkdownLength(item.content, 100) }} <div v-html="stripMarkdownWithTiebaMoji(item.content, 500)"></div>
</span> </span>
</NotificationContainer> </NotificationContainer>
</template> </template>
@@ -586,7 +586,7 @@ import InfiniteLoadMore from '~/components/InfiniteLoadMore.vue'
import BaseTabs from '~/components/BaseTabs.vue' import BaseTabs from '~/components/BaseTabs.vue'
import { toast } from '~/main' import { toast } from '~/main'
import { authState, getToken } from '~/utils/auth' import { authState, getToken } from '~/utils/auth'
import { stripMarkdownLength } from '~/utils/markdown' import { stripMarkdownWithTiebaMoji } from '~/utils/markdown'
import { import {
fetchNotifications, fetchNotifications,
fetchUnreadCount, fetchUnreadCount,

View File

@@ -265,3 +265,26 @@ export function stripMarkdownLength(text, length) {
} }
return plain.slice(0, length) + '...' return plain.slice(0, length) + '...'
} }
// 朴素文本带贴吧表情
export function stripMarkdownWithTiebaMoji(text, length){
console.error(text)
if (!text) return ''
// Markdown 转成纯文本
const plain = stripMarkdown(text)
console.error(plain)
// 替换 :tieba123: 为 <img>
const withEmoji = plain.replace(/:tieba(\d+):/g, (match, num) => {
const key = `tieba${num}`
const file = tiebaEmoji[key]
return file
? `<img loading="lazy" class="emoji" src="${file}" alt="${key}">`
: match // 没有匹配到图片则保留原样
})
// 截断纯文本长度(防止撑太长)
const truncated = withEmoji.length > length ? withEmoji.slice(0, length) + '...' : withEmoji
return truncated
}