From 460dd83d425e5e83f528dbe7a312593777ca28c2 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 5 Dec 2023 11:17:35 -0700 Subject: [PATCH] codePreviews: spoiler support --- src/modules/codePreviews.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/modules/codePreviews.js b/src/modules/codePreviews.js index 7a560bc..f782655 100644 --- a/src/modules/codePreviews.js +++ b/src/modules/codePreviews.js @@ -2,13 +2,13 @@ const {MessageFlags} = require("@projectdysnomia/dysnomia").Constants; const events = require("../lib/events.js"); const {hasFlag} = require("../lib/guildSettings.js"); - const REGEX_GITHUB = - /(?:\s|^)https?:\/\/(www\.)?github\.com\/.+?\/.+?\/blob\/([a-zA-Z0-9-_.#/]*)/g; + /(?:\s|^)(\|\|\s*)?https?:\/\/(www\.)?github\.com\/[a-z0-9-]+\/[a-z0-9-]+\/blob\/([a-z0-9-_.#/]*)(\s+\|\|)?/gi; const REGEX_GITLAB = - /(?:\s|^)https?:\/\/.+?\/.+?\/.+?\/-\/blob\/([a-zA-Z0-9-_.#/]*)/g; + /(?:\s|^)(\|\|\s*)?https?:\/\/.+?\/[a-z0-9-]+\/[a-z0-9-]+\/-\/blob\/([a-z0-9-_.#/]*)(\s+\|\|)?/gi; const REGEX_GITEA = - /(?:\s|^)https?:\/\/.+?\/.+?\/.+?\/src\/branch\/([a-zA-Z0-9-_.#/]*)/g; + /(?:\s|^)(\|\|\s*)?https?:\/\/.+?\/[a-z0-9-]+\/[a-z0-9-]+\/src\/branch\/([a-z0-9-_.#/]*)(\s+\|\|)?/gi; +const REGEX_SPOILER = /(?:\s|^)\|\|([\s\S]+?)\|\|/; function unindent(str) { str = str.replace(/\t/g, " "); @@ -20,7 +20,7 @@ function unindent(str) { return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), ""); } -async function processFile(link) { +async function processFile(link, spoiler = false) { const res = await fetch(link); if (!res.ok) return ""; if (!res.headers.get("Content-Type").startsWith("text/plain")) return ""; @@ -64,13 +64,23 @@ async function processFile(link) { if (entireFile && lines.length > 20) return ""; - const targetLines = ( + let targetLines = ( entireFile ? lines : lines.slice(startLine - 1, endLine) ).join("\n"); - return `**${fileName}: **${whichLines}\n\`\`\`${fileType}\n${unindent( - targetLines - )}\n\`\`\``; + let warning = ""; + if (spoiler && targetLines.includes("||")) { + targetLines = targetLines.replaceAll("||", "|\u200b|"); + warning = " - :warning: Zero width spaces present"; + } + if (targetLines.includes("``")) { + targetLines = targetLines.replaceAll("``", "`\u200b`"); + warning = " - :warning: Zero width spaces present"; + } + + return `**${fileName}: **${whichLines}${warning}\n${ + spoiler ? "||" : "" + }\`\`\`${fileType}\n${unindent(targetLines)}\n\`\`\`${spoiler ? "||" : ""}`; } events.add("messageCreate", "codePreviews", async function (msg) { @@ -86,19 +96,22 @@ events.add("messageCreate", "codePreviews", async function (msg) { if (githubLinks?.length) { for (const link of githubLinks) { - files.push(await processFile(link.replace("/blob/", "/raw/"))); + const spoiler = REGEX_SPOILER.test(link); + files.push(await processFile(link.replace("/blob/", "/raw/"), spoiler)); } } if (gitlabLinks?.length) { for (const link of gitlabLinks) { - files.push(await processFile(link.replace("/blob/", "/raw/"))); + const spoiler = REGEX_SPOILER.test(link); + files.push(await processFile(link.replace("/blob/", "/raw/"), spoiler)); } } if (giteaLinks?.length) { for (const link of giteaLinks) { - files.push(await processFile(link.replace("/src/", "/raw/"))); + const spoiler = REGEX_SPOILER.test(link); + files.push(await processFile(link.replace("/src/", "/raw/"), spoiler)); } }