mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
Merge pull request #4705 from rommapp/feat/changelog-short-pr-links
feat(v2): shorten the changelog's pull request links to #nnnn
This commit is contained in:
@@ -12,6 +12,7 @@ import { computed, inject, onBeforeUnmount, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import type { Events } from "@/types/emitter";
|
||||
import { useThemeMode } from "@/v2/composables/useThemeMode";
|
||||
import { shortenGithubLinks } from "@/v2/utils/githubLinks";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
@@ -25,9 +26,9 @@ type Release = {
|
||||
draft: boolean;
|
||||
};
|
||||
|
||||
const RELEASES_URL =
|
||||
"https://api.github.com/repos/rommapp/romm/releases?per_page=10";
|
||||
const RELEASES_PAGE_URL = "https://github.com/rommapp/romm/releases";
|
||||
const REPO = "rommapp/romm";
|
||||
const RELEASES_URL = `https://api.github.com/repos/${REPO}/releases?per_page=10`;
|
||||
const RELEASES_PAGE_URL = `https://github.com/${REPO}/releases`;
|
||||
|
||||
const { t, locale } = useI18n();
|
||||
const emitter = inject<Emitter<Events>>("emitter");
|
||||
@@ -69,7 +70,9 @@ async function fetchReleases() {
|
||||
});
|
||||
if (!res.ok) throw new Error(`GitHub ${res.status}`);
|
||||
const data: Release[] = await res.json();
|
||||
releases.value = data.filter((r) => !r.draft && !r.prerelease);
|
||||
releases.value = data
|
||||
.filter((r) => !r.draft && !r.prerelease)
|
||||
.map((r) => ({ ...r, body: shortenGithubLinks(r.body ?? "", REPO) }));
|
||||
} catch (e) {
|
||||
console.error("Changelog fetch failed", e);
|
||||
error.value = true;
|
||||
@@ -162,7 +165,7 @@ function closeDialog() {
|
||||
no-highlight
|
||||
no-katex
|
||||
no-mermaid
|
||||
:model-value="r.body || ''"
|
||||
:model-value="r.body"
|
||||
:theme="mdTheme"
|
||||
language="en-US"
|
||||
preview-theme="vuepress"
|
||||
|
||||
89
frontend/src/v2/utils/githubLinks.test.ts
Normal file
89
frontend/src/v2/utils/githubLinks.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { shortenGithubLinks } from "./githubLinks";
|
||||
|
||||
const REPO = "rommapp/romm";
|
||||
const PR = "https://github.com/rommapp/romm/pull/4654";
|
||||
|
||||
describe("shortenGithubLinks", () => {
|
||||
it("shortens a pull request to its number", () => {
|
||||
expect(
|
||||
shortenGithubLinks(`* Fix the list by @contributor in ${PR}`, REPO),
|
||||
).toBe(`* Fix the list by @contributor in [#4654](${PR})`);
|
||||
});
|
||||
|
||||
it("shortens an issue the same way", () => {
|
||||
const issue = "https://github.com/rommapp/romm/issues/942";
|
||||
|
||||
expect(shortenGithubLinks(`Fixes ${issue}`, REPO)).toBe(
|
||||
`Fixes [#942](${issue})`,
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the repository on another repository's reference", () => {
|
||||
const other = "https://github.com/RetroRealm/playmatch/pull/12";
|
||||
|
||||
expect(shortenGithubLinks(other, REPO)).toBe(
|
||||
`[RetroRealm/playmatch#12](${other})`,
|
||||
);
|
||||
});
|
||||
|
||||
it("reads the repository without regard to case", () => {
|
||||
const shouting = "https://github.com/RommApp/RomM/pull/7";
|
||||
|
||||
expect(shortenGithubLinks(shouting, REPO)).toBe(`[#7](${shouting})`);
|
||||
});
|
||||
|
||||
it("shortens every reference on a line", () => {
|
||||
const second = "https://github.com/rommapp/romm/pull/4655";
|
||||
|
||||
expect(shortenGithubLinks(`${PR} and ${second}`, REPO)).toBe(
|
||||
`[#4654](${PR}) and [#4655](${second})`,
|
||||
);
|
||||
});
|
||||
|
||||
// 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})`,
|
||||
`[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);
|
||||
});
|
||||
|
||||
it("leaves a deeper page of a pull request alone", () => {
|
||||
const notes = [
|
||||
`${PR}/files`,
|
||||
`${PR}#issuecomment-1`,
|
||||
`${PR}?diff=split`,
|
||||
"https://github.com/rommapp/romm/compare/5.0.0...5.1.0",
|
||||
].join("\n");
|
||||
|
||||
expect(shortenGithubLinks(notes, REPO)).toBe(notes);
|
||||
});
|
||||
|
||||
// Without the caps on the link label and target this takes ~2s, four times
|
||||
// that for twice the text: every unclosed bracket scans what follows it.
|
||||
it("does not slow to a crawl on a flood of unclosed brackets", () => {
|
||||
const notes = "[x".repeat(50_000);
|
||||
|
||||
const started = performance.now();
|
||||
shortenGithubLinks(notes, REPO);
|
||||
|
||||
expect(performance.now() - started).toBeLessThan(1_000);
|
||||
});
|
||||
});
|
||||
28
frontend/src/v2/utils/githubLinks.ts
Normal file
28
frontend/src/v2/utils/githubLinks.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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.
|
||||
//
|
||||
// A `[` needs no closing bracket, so an unclosed one would scan to the end of
|
||||
// the notes and the search would cost O(n^2); the caps, far above any real
|
||||
// label or URL, hold it to a line's worth of work per bracket.
|
||||
const VERBATIM_OR_REFERENCE =
|
||||
/(```[\s\S]*?```|`[^`]*`|\[[^\]]{0,300}\]\([^)]{0,600}\)|<[^>\s]{0,600}>)|https:\/\/github\.com\/([\w.-]+\/[\w.-]+)\/(?:pull|issues)\/(\d+)(?![\w/#?])/g;
|
||||
|
||||
/**
|
||||
* Rewrites bare pull request and issue URLs in `markdown` the way GitHub
|
||||
* renders them: `#123` for `repo`'s own, `owner/name#123` for another's.
|
||||
*
|
||||
* Args:
|
||||
* markdown: the text to rewrite, such as a release's notes.
|
||||
* repo: the `owner/name` the notes belong to.
|
||||
*/
|
||||
export function shortenGithubLinks(markdown: string, repo: string): string {
|
||||
return markdown.replace(
|
||||
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})`;
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user