diff --git a/commands/interaction/slash/search/anime.js b/commands/interaction/slash/search/anime.js index 6082190..a7486a6 100644 --- a/commands/interaction/slash/search/anime.js +++ b/commands/interaction/slash/search/anime.js @@ -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 diff --git a/commands/message/search/anime.js b/commands/message/search/anime.js index 53230ba..f8e8b4a 100644 --- a/commands/message/search/anime.js +++ b/commands/message/search/anime.js @@ -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 diff --git a/labscore/utils/markdown.js b/labscore/utils/markdown.js index ffb16be..0801d95 100644 --- a/labscore/utils/markdown.js +++ b/labscore/utils/markdown.js @@ -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) + "  `**" }