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,6 +19,63 @@ function unindent(str) {
return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), ""); return str.replace(new RegExp(`^ {${minIndent}}`, "gm"), "");
} }
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(
link.lastIndexOf("/") + 1,
link.indexOf("#") == -1 ? link.length : link.indexOf("#")
);
const fileType =
fileName.indexOf(".") == -1
? ""
: fileName.substring(fileName.indexOf(".") + 1);
const lineStr = link.match(/#L\d+(-L?\d+)?/)?.[0];
let startLine, endLine;
let entireFile = false;
if (lineStr) {
const [start, end] = link.match(/\d+/g);
if (!end) {
startLine = endLine = start;
} else {
startLine = start;
endLine = end;
}
} else {
entireFile = true;
startLine = 1;
endLine = lines.length;
}
const whichLines = entireFile
? ""
: startLine == endLine
? "Line " + startLine
: "Lines " + startLine + "-" + endLine;
startLine--;
endLine--;
const targetLines = (
entireFile ? lines : lines.slice(startLine, endLine + 1)
).join("\n");
return (
"**" +
fileName +
": **" +
whichLines +
"\n```" +
fileType +
"\n" +
unindent(targetLines) +
"\n```\n"
);
}
events.add("messageCreate", "codePreviews", async function (msg) { events.add("messageCreate", "codePreviews", async function (msg) {
if (!msg.guildID) return; if (!msg.guildID) return;
if (!hasFlag(msg.guildID, "codePreviews")) return; if (!hasFlag(msg.guildID, "codePreviews")) return;
@ -31,62 +88,19 @@ events.add("messageCreate", "codePreviews", async function (msg) {
if (githubLinks?.length) { if (githubLinks?.length) {
for (const link of githubLinks) { for (const link of githubLinks) {
const rawLink = link.replace("/blob/", "/raw/"); out += await processFile(link.replace("/blob/", "/raw/"));
const file = await fetch(rawLink).then((res) => res.text()); }
const lines = file.replace(/\r/g, "").split("\n"); }
const fileName = link.substring( if (gitlabLinks?.length) {
link.lastIndexOf("/") + 1, for (const link of gitlabLinks) {
link.indexOf("#") == -1 ? link.length : link.indexOf("#") out += await processFile(link.replace("/blob/", "/raw/"));
); }
const fileType = }
fileName.indexOf(".") == -1
? ""
: fileName.substring(fileName.indexOf(".") + 1);
const lineStr = link.match(/#L\d+(-L\d+)?/)?.[0]; if (giteaLinks?.length) {
let startLine, endLine; for (const link of giteaLinks) {
let entireFile = false; out += await processFile(link.replace("/src/", "/raw/"));
if (lineStr) {
let [start, end] = link.match(/L\d+/g);
start = parseInt(start.replace("L", ""));
if (!end) {
startLine = endLine = start;
} else {
end = parseInt(end.replace("L", ""));
startLine = start;
endLine = end;
}
} else {
entireFile = true;
startLine = 1;
endLine = lines.length;
}
const whichLines = entireFile
? ""
: startLine == endLine
? "Line " + startLine
: "Lines " + startLine + "-" + endLine;
startLine--;
endLine--;
const targetLines = (
entireFile ? lines : lines.slice(startLine, endLine + 1)
).join("\n");
out +=
"**" +
fileName +
": **" +
whichLines +
"\n```" +
fileType +
"\n" +
unindent(targetLines) +
"\n```\n";
} }
} }