From c7aaf4b5ced26e2ee1b7210cd840209434848411 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sat, 16 Feb 2019 02:41:44 +0000 Subject: [PATCH] Clean Anilist HTML --- commands/search/anime.js | 4 ++-- commands/search/character.js | 6 ++---- commands/search/manga.js | 4 ++-- util/Util.js | 12 ++++++++++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/commands/search/anime.js b/commands/search/anime.js index c84ba0dd..61d18cba 100644 --- a/commands/search/anime.js +++ b/commands/search/anime.js @@ -2,7 +2,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); const { stripIndents } = require('common-tags'); -const { shorten } = require('../../util/Util'); +const { shorten, cleanAnilistHTML } = require('../../util/Util'); const searchGraphQL = stripIndents` query ($search: String, $type: MediaType, $isAdult: Boolean) { anime: Page (perPage: 1) { @@ -77,7 +77,7 @@ module.exports = class AnimeCommand extends Command { .setURL(anime.siteUrl) .setThumbnail(anime.coverImage.large || anime.coverImage.medium || null) .setTitle(anime.title.english || anime.title.userPreferred) - .setDescription(anime.description ? shorten(anime.description.replace(/(
)+/g, '\n')) : 'No description.') + .setDescription(anime.description ? shorten(cleanAnilistHTML(anime.description)) : 'No description.') .addField('❯ Status', statuses[anime.status], true) .addField('❯ Episodes', anime.episodes || '???', true) .addField('❯ Season', anime.season ? `${seasons[anime.season]} ${anime.startDate.year}` : '???', true) diff --git a/commands/search/character.js b/commands/search/character.js index c736bb36..c7804ca7 100644 --- a/commands/search/character.js +++ b/commands/search/character.js @@ -2,7 +2,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); const { stripIndents } = require('common-tags'); -const { shorten } = require('../../util/Util'); +const { shorten, cleanAnilistHTML } = require('../../util/Util'); const searchGraphQL = stripIndents` query ($search: String) { characters: Page (perPage: 1) { @@ -74,9 +74,7 @@ module.exports = class CharacterCommand extends Command { .setURL(character.siteUrl) .setThumbnail(character.image.large || character.image.medium || null) .setTitle(`${character.name.first || ''}${character.name.last ? ` ${character.name.last}` : ''}`) - .setDescription(character.description - ? shorten(character.description.replace(/(
)+/g, '\n')) - : 'No description.') + .setDescription(character.description ? shorten(cleanAnilistHTML(character.description)) : 'No description.') .addField('❯ Appearances', character.media.edges.map(edge => { const title = edge.node.title.english || edge.node.title.userPreferred; return `[${title} (${types[edge.node.type]})](${edge.node.siteUrl})`; diff --git a/commands/search/manga.js b/commands/search/manga.js index eba7200e..558c2ebc 100644 --- a/commands/search/manga.js +++ b/commands/search/manga.js @@ -2,7 +2,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); const { stripIndents } = require('common-tags'); -const { shorten } = require('../../util/Util'); +const { shorten, cleanAnilistHTML } = require('../../util/Util'); const searchGraphQL = stripIndents` query ($search: String, $type: MediaType, $isAdult: Boolean) { anime: Page (perPage: 1) { @@ -68,7 +68,7 @@ module.exports = class MangaCommand extends Command { .setURL(manga.siteUrl) .setThumbnail(manga.coverImage.large || manga.coverImage.medium || null) .setTitle(manga.title.english || manga.title.userPreferred) - .setDescription(manga.description ? shorten(manga.description.replace(/(
)+/g, '\n')) : 'No description.') + .setDescription(manga.description ? shorten(cleanAnilistHTML(manga.description)) : 'No description.') .addField('❯ Status', statuses[manga.status], true) .addField('❯ Chapters / Volumes', `${manga.chapters || '???'}/${manga.volumes || '???'}`, true) .addField('❯ Year', manga.startDate.year || '???', true) diff --git a/util/Util.js b/util/Util.js index c0a90751..de93715f 100644 --- a/util/Util.js +++ b/util/Util.js @@ -116,4 +116,16 @@ module.exports = class Util { if (no.includes(choice)) return false; return false; } + + static cleanAnilistHTML(html) { + let clean = html + .replace(/(
)+/g, '\n') + .replace(/'/g, '\'') + .replace(/"/g, '"') + .replace(/<\/?i>/g, '*') + .replace(/~!|!~/g, '||'); + const spoilers = (clean.substr(0, 1997).match(/\|\|/g) || []).length; + if (spoilers !== 0 && (spoilers && (spoilers % 2))) clean += '||'; + return clean; + } };