app: limit title length by bytes, close #663

This commit is contained in:
DIYgod 2018-09-10 01:29:37 +08:00
parent 571636b4a8
commit 2e122373e0
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
7 changed files with 15 additions and 8 deletions

View File

@ -31,7 +31,13 @@ module.exports = async (ctx, next) => {
}
// trim title length
ctx.state.data.item.forEach((item) => {
item.title = item.title.length > 80 ? `${item.title.slice(0, 80)}...` : item.title;
for (let length = 0, i = 0; i < item.title.length; i++) {
length += Buffer.from(item.title[i]).length !== 1 ? 2 : 1;
if (length > 100) {
item.title = `${item.title.slice(0, i)}...`;
break;
}
}
});
const data = {

View File

@ -40,7 +40,7 @@ module.exports = async (ctx) => {
item: data.map((item) => {
const thread = threads.filter((i) => i.id === item.thread)[0];
return {
title: `${item.author.name}: ${item.raw_message > 24 ? item.raw_message.slice(0, 24) + '...' : item.raw_message}`,
title: `${item.author.name}: ${item.raw_message}`,
description: `${item.author.name} 在《${thread.clean_title}》中发表评论: ${item.message}`,
pubDate: new Date(item.createdAt).toUTCString(),
link: `${thread.link}/#comment-${item.id}`,

View File

@ -65,9 +65,10 @@ module.exports = async (ctx) => {
for (let i = 0; i < images[index].length; i++) {
imgTPL += `<img referrerpolicy="no-referrer" src="${images[index][i]}"><br>`;
}
const title = (item.edge_media_to_caption.edges && item.edge_media_to_caption.edges[0] && item.edge_media_to_caption.edges[0].node.text) || '无题/Untitled';
return {
title: `${type}${(item.edge_media_to_caption.edges && item.edge_media_to_caption.edges[0] && item.edge_media_to_caption.edges[0].node.text) || '无题/Untitled'}`,
description: `${tip}${imgTPL}`,
title: `${type}${title}`,
description: `${title}<br>${tip}${imgTPL}`,
pubDate: new Date(item.taken_at_timestamp * 1000).toUTCString(),
link: `https://www.instagram.com/p/${item.shortcode}/`,
};

View File

@ -83,7 +83,7 @@ module.exports = async (ctx) => {
}
return {
title: text.length > 24 ? text.slice(0, 24) + '...' : text,
title: text,
description: html,
pubDate: new Date(item.date * 1000).toUTCString(),
link: `https://t.me/${username}/${item.message_id}`,

View File

@ -30,7 +30,7 @@ module.exports = async (ctx) => {
url += `<a href="${u.expanded_url}" target="_blank"></a>`;
});
return {
title: `${item.in_reply_to_screen_name ? 'Re ' : ''}${item.full_text.length > 30 ? item.full_text.slice(0, 30) + '...' : item.full_text}`,
title: `${item.in_reply_to_screen_name ? 'Re ' : ''}${item.full_text}`,
description: `${item.in_reply_to_screen_name ? 'Re ' : ''}${item.full_text}${url}${img}`,
pubDate: new Date(item.created_at).toUTCString(),
link: `https://twitter.com/${id}/status/${item.id_str}`,

View File

@ -20,7 +20,7 @@ module.exports = async (ctx) => {
item: data.map((item) => {
const title = item.mblog.text.replace(/<img.*?>/g, '[图片]').replace(/<.*?>/g, '');
return {
title: `${item.mblog.user.screen_name}: ${title.length > 24 ? title.slice(0, 24) + '...' : title}`,
title: `${item.mblog.user.screen_name}: ${title}`,
description: `${item.mblog.user.screen_name}: ${weiboUtils.format(item.mblog)}`,
pubDate: weiboUtils.getTime(item.mblog.created_at),
link: `https://weibo.com/${item.mblog.user.id}/${item.mblog.bid}`,

View File

@ -43,7 +43,7 @@ module.exports = async (ctx) => {
url = `https://zhuanlan.zhihu.com/p/${detail.id}`;
break;
case 'pin':
title = detail.excerpt_title.length > 17 ? detail.excerpt_title.slice(0, 17) + '...' : detail.excerpt_title;
title = detail.excerpt_title;
detail.content.forEach((contentItem) => {
if (contentItem.type === 'text') {
text = `<p>${contentItem.own_text}</p>`;