diff --git a/frontend/src/v2/utils/githubLinks.test.ts b/frontend/src/v2/utils/githubLinks.test.ts index 83a79a6e4..855d42dc5 100644 --- a/frontend/src/v2/utils/githubLinks.test.ts +++ b/frontend/src/v2/utils/githubLinks.test.ts @@ -43,7 +43,24 @@ describe("shortenGithubLinks", () => { // Rewriting these would nest a link inside a link. it("leaves a reference that is already a link alone", () => { - const notes = [`[the fix](${PR})`, `[${PR}](${PR})`, `<${PR}>`].join("\n"); + const notes = [ + `[the fix](${PR})`, + `[${PR}](${PR})`, + `[see ${PR} for the details](${PR})`, + `<${PR}>`, + ].join("\n"); + + expect(shortenGithubLinks(notes, REPO)).toBe(notes); + }); + + // Code reads as written, so a rewrite would show the markdown itself. + it("leaves a reference inside code alone", () => { + const notes = [ + `Compare it with \`${PR}\`:`, + "```sh", + `curl ${PR}`, + "```", + ].join("\n"); expect(shortenGithubLinks(notes, REPO)).toBe(notes); }); @@ -52,6 +69,7 @@ describe("shortenGithubLinks", () => { const notes = [ `${PR}/files`, `${PR}#issuecomment-1`, + `${PR}?diff=split`, "https://github.com/rommapp/romm/compare/5.0.0...5.1.0", ].join("\n"); diff --git a/frontend/src/v2/utils/githubLinks.ts b/frontend/src/v2/utils/githubLinks.ts index a246b2947..f01623098 100644 --- a/frontend/src/v2/utils/githubLinks.ts +++ b/frontend/src/v2/utils/githubLinks.ts @@ -1,7 +1,8 @@ -// A bare pull request or issue URL, not one already inside a markdown link -// (as its text or its target) or an ``, and not a deeper page of it. -const REFERENCE_URL = - /(?\s]*>)|https:\/\/github\.com\/([\w.-]+\/[\w.-]+)\/(?:pull|issues)\/(\d+)(?![\w/#?])/g; /** * Rewrites bare pull request and issue URLs in `markdown` the way GitHub @@ -13,8 +14,9 @@ const REFERENCE_URL = */ export function shortenGithubLinks(markdown: string, repo: string): string { return markdown.replace( - REFERENCE_URL, - (url, target: string, number: string) => { + VERBATIM_OR_REFERENCE, + (url, verbatim: string | undefined, target: string, number: string) => { + if (verbatim !== undefined) return verbatim; const prefix = target.toLowerCase() === repo.toLowerCase() ? "" : target; return `[${prefix}#${number}](${url})`; },