codePreviews: fix urls with query params, use URL, allow markdown for not entire file

This commit is contained in:
Cynthia Foxwell 2024-06-01 22:32:56 -06:00
parent d166bbe870
commit f2e62ecf44
1 changed files with 12 additions and 10 deletions

View File

@ -6,11 +6,11 @@ const {getOption} = require("../lib/interactionDispatcher.js");
const events = require("../lib/events.js");
const {hasFlag} = require("../lib/guildSettings.js");
const REGEX_GITHUB =
/(?:\s|^)(\|\|\s*)?https?:\/\/(www\.)?github\.com\/[a-z0-9-]+\/[a-z0-9-]+\/blob\/([a-z0-9-_.#/%]*)(\s*\|\|)?/gi;
/(?:\s|^)(\|\|\s*)?https?:\/\/(www\.)?github\.com\/[a-z0-9-]+\/[a-z0-9-]+\/blob\/([a-z0-9-_.?&=#/%]*)(\s*\|\|)?/gi;
const REGEX_GITLAB =
/(?:\s|^)(\|\|\s*)?https?:\/\/.+?\/[a-z0-9-]+\/[a-z0-9-]+\/-\/blob\/([a-z0-9-_.#/%]*)(\s*\|\|)?/gi;
/(?:\s|^)(\|\|\s*)?https?:\/\/.+?\/[a-z0-9-]+\/[a-z0-9-]+\/-\/blob\/([a-z0-9-_.?&=#/%]*)(\s*\|\|)?/gi;
const REGEX_GITEA =
/(?:\s|^)(\|\|\s*)?https?:\/\/.+?\/[a-z0-9-]+\/[a-z0-9-]+\/src\/(branch|commit)\/([a-z0-9-_.#/%]*)(\s*\|\|)?/gi;
/(?:\s|^)(\|\|\s*)?https?:\/\/.+?\/[a-z0-9-]+\/[a-z0-9-]+\/src\/(branch|commit)\/([a-z0-9-_.?&=#/%]*)(\s*\|\|)?/gi;
const REGEX_SPOILER = /(?:\s|^)\|\|([\s\S]+?)\|\|/;
function unindent(str) {
@ -36,9 +36,10 @@ async function processFile(
const file = await res.text();
const lines = file.replace(/\r/g, "").split("\n");
let fileName = decodeURIComponent(link).substring(
link.lastIndexOf("/") + 1,
link.indexOf("#") == -1 ? link.length : link.indexOf("#")
const urlObj = new URL(link);
let fileName = decodeURIComponent(urlObj.pathname).substring(
link.lastIndexOf("/") + 1
);
const fileType =
fileName.lastIndexOf(".") == -1
@ -49,9 +50,7 @@ async function processFile(
fileName = `[${fileName}](<${originalLink}>)`;
}
if (fileType == "md") return "";
const lineStr = link.match(/#L\d+(-L?\d+)?/)?.[0];
const lineStr = urlObj.hash.match(/#L\d+(-L?\d+)?/)?.[0];
let startLine, endLine;
let entireFile = false;
@ -75,7 +74,10 @@ async function processFile(
? "Line " + startLine
: "Lines " + startLine + "-" + endLine;
if (entireFile && lines.length > 20) return "";
if (entireFile) {
if (fileType == "md") return "";
if (lines.length > 20) return "";
}
let targetLines = (
entireFile ? lines : lines.slice(startLine - 1, endLine)