fix(v2): keep the changelog's link rewrite out of code and queries

Addresses Greptile's review of #4705.

The rewrite matched a reference anywhere but right after a link's
bracket, so one written inside code was turned into markdown the preview
then printed verbatim, and one inside a longer link label nested a link
in a link. Code spans, fenced blocks and whole existing links are now
matched first and handed back untouched. A trailing query no longer
splits a reference either: `?` joins the guard that already kept
`/files` and `#issuecomment-1` whole.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016bZ7R3xC73MZu1vTNuatPK
This commit is contained in:
zurdi
2026-09-22 20:15:00 +02:00
parent ed3d9a8e11
commit a717e2e81a
2 changed files with 27 additions and 7 deletions

View File

@@ -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");

View File

@@ -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 `<autolink>`, and not a deeper page of it.
const REFERENCE_URL =
/(?<!\]\(|<|\[)https:\/\/github\.com\/([\w.-]+\/[\w.-]+)\/(?:pull|issues)\/(\d+)(?![\w/#])/g;
// Either a span that reads as written (code, or a link that is already one),
// which the first alternative claims before the second can reach into it, or a
// bare pull request / issue URL, which the trailing guard keeps to a plain one.
const VERBATIM_OR_REFERENCE =
/(```[\s\S]*?```|`[^`]*`|\[[^\]]*\]\([^)]*\)|<[^>\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})`;
},