Skip to content

Commit

Permalink
clip words
Browse files Browse the repository at this point in the history
  • Loading branch information
z committed Jun 23, 2023
1 parent 1b9f3b5 commit b5122b6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/lib/components/post/item.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script>
import { bgColor } from '$lib/utils';
import { bgColor, clipWords } from "$lib/utils";
export let p = {};
export let path;
const { banner, slug, title, desc, content, createAt } = p;
const { banner, slug, title, desc, createAt } = p;
const sty = banner ? `background-image:url(/res/_${banner})` : '';
const tm = new Date(createAt);
const y = tm.getFullYear();
Expand All @@ -12,7 +12,7 @@
const showY = new Date().getFullYear() !== y;
let ds;
$: {
ds = (desc || content || '').substring(0, 60);
ds = clipWords(desc, 60);
}
</script>

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/viewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
p,
span {
word-break: break-all;
overflow-wrap:break-word;
}
blockquote {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'],
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit b5122b6

Please sign in to comment.