codePreviews: spoiler support
This commit is contained in:
parent
2f4eeee255
commit
460dd83d42
1 changed files with 25 additions and 12 deletions
|
@ -2,13 +2,13 @@ const {MessageFlags} = require("@projectdysnomia/dysnomia").Constants;
|
||||||
|
|
||||||
const events = require("../lib/events.js");
|
const events = require("../lib/events.js");
|
||||||
const {hasFlag} = require("../lib/guildSettings.js");
|
const {hasFlag} = require("../lib/guildSettings.js");
|
||||||
|
|
||||||
const REGEX_GITHUB =
|
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 =
|
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 =
|
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) {
|
function unindent(str) {
|
||||||
str = str.replace(/\t/g, " ");
|
str = str.replace(/\t/g, " ");
|
||||||
|
@ -20,7 +20,7 @@ function unindent(str) {
|
||||||
return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
|
return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processFile(link) {
|
async function processFile(link, spoiler = false) {
|
||||||
const res = await fetch(link);
|
const res = await fetch(link);
|
||||||
if (!res.ok) return "";
|
if (!res.ok) return "";
|
||||||
if (!res.headers.get("Content-Type").startsWith("text/plain")) 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 "";
|
if (entireFile && lines.length > 20) return "";
|
||||||
|
|
||||||
const targetLines = (
|
let targetLines = (
|
||||||
entireFile ? lines : lines.slice(startLine - 1, endLine)
|
entireFile ? lines : lines.slice(startLine - 1, endLine)
|
||||||
).join("\n");
|
).join("\n");
|
||||||
|
|
||||||
return `**${fileName}: **${whichLines}\n\`\`\`${fileType}\n${unindent(
|
let warning = "";
|
||||||
targetLines
|
if (spoiler && targetLines.includes("||")) {
|
||||||
)}\n\`\`\``;
|
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) {
|
events.add("messageCreate", "codePreviews", async function (msg) {
|
||||||
|
@ -86,19 +96,22 @@ events.add("messageCreate", "codePreviews", async function (msg) {
|
||||||
|
|
||||||
if (githubLinks?.length) {
|
if (githubLinks?.length) {
|
||||||
for (const link of githubLinks) {
|
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) {
|
if (gitlabLinks?.length) {
|
||||||
for (const link of gitlabLinks) {
|
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) {
|
if (giteaLinks?.length) {
|
||||||
for (const link of giteaLinks) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue