add stringwrap by words, limit ani description length

This commit is contained in:
bignutty 2025-01-28 03:20:58 +01:00
parent d58fa1fbe4
commit 19979e440e
3 changed files with 26 additions and 5 deletions

View file

@ -4,7 +4,7 @@ const { PERMISSION_GROUPS, OMNI_ANIME_FORMAT_TYPES } = require('#constants');
const { createEmbed, formatPaginationEmbeds, page, hexToEmbedColor } = require('#utils/embed');
const { acknowledge } = require('#utils/interactions');
const { smallPill, link, pill } = require('#utils/markdown');
const { smallPill, link, pill, stringwrapPreserveWords} = require('#utils/markdown');
const { editOrReply } = require('#utils/message');
const { InteractionContextTypes, ApplicationIntegrationTypes, ApplicationCommandOptionTypes } = require('detritus-client/lib/constants');
@ -30,7 +30,7 @@ function renderAnimeResultsPage(context, res){
if(res.subtitle) result.description += `-# ${res.subtitle}\n\n`;
if(res.subtype) result.description += pill(OMNI_ANIME_FORMAT_TYPES[res.subtype]) + " "
if(res.genres) result.description += res.genres.map((r)=>smallPill(r)).join(" ") + "\n\n";
if(res.description) result.description += res.description;
if(res.description) result.description += stringwrapPreserveWords(res.description, 600);
if(res.attribution?.description) result.description += `\n\n-# Source • ${res.attribution.description}`
// Render Images

View file

@ -4,7 +4,7 @@ const { PERMISSION_GROUPS, OMNI_ANIME_FORMAT_TYPES } = require('#constants');
const { createEmbed, formatPaginationEmbeds, page, hexToEmbedColor } = require('#utils/embed');
const { acknowledge } = require('#utils/interactions');
const { smallPill, link, pill } = require('#utils/markdown');
const { smallPill, link, pill, stringwrap, stringwrapPreserveWords} = require('#utils/markdown');
const { editOrReply } = require('#utils/message');
function renderAnimeResultsPage(context, res){
@ -29,7 +29,7 @@ function renderAnimeResultsPage(context, res){
if(res.subtitle) result.description += `-# ${res.subtitle}\n\n`;
if(res.subtype) result.description += pill(OMNI_ANIME_FORMAT_TYPES[res.subtype]) + " "
if(res.genres) result.description += res.genres.map((r)=>smallPill(r)).join(" ") + "\n\n";
if(res.description) result.description += res.description;
if(res.description) result.description += stringwrapPreserveWords(res.description, 600);
if(res.attribution?.description) result.description += `\n\n-# Source • ${res.attribution.description}`
// Render Images

View file

@ -66,7 +66,7 @@ module.exports.timestamp = function(time, flag = "t"){
}
module.exports.stringwrap = function(content = "", length, newlines = true){
if(!newlines) content = content.replace(/\n/, ' ')
if(!newlines) content = content.replace(/\n/g, ' ')
if(content.length > length){
c = content.substring(0, length-1) + '…';
while(c.endsWith(' …')) c = c.substr(0, c.length - 2) + '…';
@ -75,6 +75,27 @@ module.exports.stringwrap = function(content = "", length, newlines = true){
return content;
}
/**
* Limits a string to fit within a certain amount of characters.
* Alternative to {@link stringwrap} that ensures words don't
* get broken up in the middle.
* @param content String Content
* @param length Maximum Length in characters
* @param newlines Remove newlines
* @returns {string} Wrapped String
*/
module.exports.stringwrapPreserveWords = function(content = "", length, newlines = true){
if(!newlines) content = content.replace(/\n/g, ' ')
if(content.length <= length) return content;
content = content.split(" ");
// content size + amount of spaces length + ... char
while(content.join(" ").length + (content.length - 1) > length - 1){
content.pop();
}
return content.join(" ") + "…";
}
module.exports.pill = function(content = ""){
return " **` " + _escapeCodeblock(content) + "  `**"
}