diff --git a/src/lib/components/post/item.svelte b/src/lib/components/post/item.svelte index ce3b1da..30fe0f7 100644 --- a/src/lib/components/post/item.svelte +++ b/src/lib/components/post/item.svelte @@ -1,9 +1,9 @@ @@ -118,8 +118,8 @@ margin-bottom: 10px; display: flex; overflow: hidden; - word-break: break-all; white-space: normal; + overflow-wrap:break-word; text-overflow: ellipsis; line-height: 1.5; margin-top: 3px; diff --git a/src/lib/components/viewer.svelte b/src/lib/components/viewer.svelte index 2546eaf..f935430 100644 --- a/src/lib/components/viewer.svelte +++ b/src/lib/components/viewer.svelte @@ -116,7 +116,7 @@ p, span { - word-break: break-all; + overflow-wrap:break-word; } blockquote { diff --git a/src/lib/server/api.ts b/src/lib/server/api.ts index ec42b4a..94fb286 100644 --- a/src/lib/server/api.ts +++ b/src/lib/server/api.ts @@ -40,7 +40,7 @@ import { Tag, TokenInfo } from '$lib/server/model'; -import { arrFilter, diffObj, enc, filter, getPain, trim } from "$lib/utils"; +import { arrFilter, clipWords, diffObj, enc, filter, getPain, trim } from "$lib/utils"; import { permission } from '$lib/enum'; import path from 'path'; import fs from 'fs'; @@ -694,7 +694,7 @@ const apis: APIRoutes = { if (next) p._n = next; readManager.set(p.id, req); p._r = readManager.get(p.id); - if(!p.desc)p._d=(await getPain(p.content)).substring(0, 140) + if(!p.desc)p._d=clipWords(await getPain(p.content),140) return filter( patchPostTags([p])[0], ['banner', '_cm', 'desc', 'content', '_d','createAt', '_tag', 'title', '_u', '_n', '_r'], diff --git a/src/lib/server/posts.ts b/src/lib/server/posts.ts index 9960fe1..dae1824 100644 --- a/src/lib/server/posts.ts +++ b/src/lib/server/posts.ts @@ -3,7 +3,7 @@ import { tags } from '$lib/server/store'; import { combine, patchPostReqs, patchPostTags, tagPostCache } from '$lib/server/cache'; import { model, pageBuilder, resp, sqlFields } from '$lib/server/utils'; import { Post } from '$lib/server/model'; -import { getPain } from '$lib/utils'; +import { clipWords, getPain } from "$lib/utils"; import { db } from '$lib/server/index'; import { DiffMatchPatch } from 'diff-match-patch-typescript'; import type { PatchObject } from 'diff-match-patch-typescript'; @@ -45,7 +45,7 @@ export const pubPostList = async ( patchPostTags ); for (const a of o.items) { - a.desc = (a.desc || (await getPain(a.content))).substring(0, 140); + a.desc = a.desc || clipWords(await getPain(a.content),140); delete a.content; } if (!tagInfo) return o; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 9e1b276..3760e61 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -517,6 +517,16 @@ export const getColor = (a: number | string, opacity = 1) => { return c; }; +export const clipWords = (str:string,len:number)=> { + let i=0 + let e = len + while (e>0){ + if(/[\u4e00-\u9fa5]/.test(str[i++]))e-- + else e-=0.5 + } + return str.substring(0,i) +} + export const getPain = async (src?: string) => { const { marked } = await import('marked'); const { convert } = await import('html-to-text');