codePreviews: impl gitlab and gitea support

This commit is contained in:
Cynthia Foxwell 2022-05-07 17:42:22 -06:00
parent e8b4ffe358
commit a4d84e6c84

View file

@ -19,20 +19,8 @@ function unindent(str) {
return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
}
events.add("messageCreate", "codePreviews", async function (msg) {
if (!msg.guildID) return;
if (!hasFlag(msg.guildID, "codePreviews")) return;
let out = "";
const githubLinks = msg.content.match(REGEX_GITHUB);
const gitlabLinks = msg.content.match(REGEX_GITLAB);
const giteaLinks = msg.content.match(REGEX_GITEA);
if (githubLinks?.length) {
for (const link of githubLinks) {
const rawLink = link.replace("/blob/", "/raw/");
const file = await fetch(rawLink).then((res) => res.text());
async function processFile(link) {
const file = await fetch(link).then((res) => res.text());
const lines = file.replace(/\r/g, "").split("\n");
const fileName = link.substring(
@ -44,17 +32,15 @@ events.add("messageCreate", "codePreviews", async function (msg) {
? ""
: fileName.substring(fileName.indexOf(".") + 1);
const lineStr = link.match(/#L\d+(-L\d+)?/)?.[0];
const lineStr = link.match(/#L\d+(-L?\d+)?/)?.[0];
let startLine, endLine;
let entireFile = false;
if (lineStr) {
let [start, end] = link.match(/L\d+/g);
start = parseInt(start.replace("L", ""));
const [start, end] = link.match(/\d+/g);
if (!end) {
startLine = endLine = start;
} else {
end = parseInt(end.replace("L", ""));
startLine = start;
endLine = end;
}
@ -77,7 +63,7 @@ events.add("messageCreate", "codePreviews", async function (msg) {
entireFile ? lines : lines.slice(startLine, endLine + 1)
).join("\n");
out +=
return (
"**" +
fileName +
": **" +
@ -86,7 +72,35 @@ events.add("messageCreate", "codePreviews", async function (msg) {
fileType +
"\n" +
unindent(targetLines) +
"\n```\n";
"\n```\n"
);
}
events.add("messageCreate", "codePreviews", async function (msg) {
if (!msg.guildID) return;
if (!hasFlag(msg.guildID, "codePreviews")) return;
let out = "";
const githubLinks = msg.content.match(REGEX_GITHUB);
const gitlabLinks = msg.content.match(REGEX_GITLAB);
const giteaLinks = msg.content.match(REGEX_GITEA);
if (githubLinks?.length) {
for (const link of githubLinks) {
out += await processFile(link.replace("/blob/", "/raw/"));
}
}
if (gitlabLinks?.length) {
for (const link of gitlabLinks) {
out += await processFile(link.replace("/blob/", "/raw/"));
}
}
if (giteaLinks?.length) {
for (const link of giteaLinks) {
out += await processFile(link.replace("/src/", "/raw/"));
}
}