From 91fe08d205b83ebc88b52433ccf2524eb420f4fe Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Fri, 20 Jun 2025 21:41:29 +0800 Subject: [PATCH 1/2] refactor(repo-card): reduce coupling and indention --- assets/js/fetch-repo.js | 89 ++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/assets/js/fetch-repo.js b/assets/js/fetch-repo.js index f94c9614..b47678ae 100644 --- a/assets/js/fetch-repo.js +++ b/assets/js/fetch-repo.js @@ -1,36 +1,59 @@ -function fetchRepo() { - const scriptElement = document.currentScript; - const repoURL = - scriptElement && scriptElement.getAttribute("data-repo-url") - ? scriptElement.getAttribute("data-repo-url") - : (console.error("data-repo-url is null"), null); - const repoId = - scriptElement && scriptElement.getAttribute("data-repo-id") - ? scriptElement.getAttribute("data-repo-id") - : (console.error("data-repo-id is null"), null); - const requestObjects = repoId.startsWith("github") - ? ["full_name", "description", "stargazers_count", "forks"] - : repoId.startsWith("gitlab") - ? ["name_with_namespace", "description", "star_count", "forks_count"] - : ["full_name", "description", "stars_count", "forks_count"]; - fetch(repoURL, { - headers: new Headers({ - "User-agent": "Mozilla/4.0 Custom User Agent", - }), - }) - .then((response) => response.json()) - .then((data) => { - requestObjects.forEach((requestObject) => { - let element = document.getElementById(`${repoId}-${requestObject}`); - if (requestObject === "stargazers_count" && repoId.startsWith("github")) { - element = document.getElementById(`${repoId}-stargazers`); - } - element - ? (element.innerHTML = data[requestObject]) - : (console.error(`Element '${repoId}-${requestObject}' not found`), null); - }); - }) - .catch((error) => console.error(error)); +async function fetchRepo() { + const script = document.currentScript; + const repoURL = script?.getAttribute("data-repo-url"); + const repoId = script?.getAttribute("data-repo-id"); + + if (!repoURL || !repoId) return; + + const platforms = { + github: { + full_name: "full_name", + description: "description", + stargazers_count: "stargazers", + forks: "forks", + }, + gitlab: { + name_with_namespace: "name_with_namespace", + description: "description", + star_count: "star_count", + forks_count: "forks_count", + }, + gitea: { + full_name: "full_name", + description: "description", + stars_count: "stars_count", + forks_count: "forks_count", + }, + codeberg: { + full_name: "full_name", + description: "description", + stars_count: "stars_count", + forks_count: "forks_count", + }, + forgejo: { + full_name: "full_name", + description: "description", + stars_count: "stars_count", + forks_count: "forks_count", + }, + }; + + const platform = Object.keys(platforms).find((p) => repoId.startsWith(p)) || "github"; + const mapping = platforms[platform]; + + try { + const response = await fetch(repoURL, { + headers: { "User-agent": "Mozilla/4.0 Custom User Agent" }, + }); + const data = await response.json(); + + Object.entries(mapping).forEach(([dataField, elementSuffix]) => { + const element = document.getElementById(`${repoId}-${elementSuffix}`); + if (element) element.innerHTML = data[dataField]; + }); + } catch (error) { + console.error(error); + } } fetchRepo(); From d39a64afe17662fb7ec6e108c323c3654d384adc Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Fri, 20 Jun 2025 22:15:27 +0800 Subject: [PATCH 2/2] refactor(repo-card): use iife and async loading --- assets/js/fetch-repo.js | 6 ++---- layouts/shortcodes/codeberg.html | 1 + layouts/shortcodes/forgejo.html | 1 + layouts/shortcodes/gitea.html | 1 + layouts/shortcodes/github.html | 1 + layouts/shortcodes/gitlab.html | 1 + 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/assets/js/fetch-repo.js b/assets/js/fetch-repo.js index b47678ae..41ee97fb 100644 --- a/assets/js/fetch-repo.js +++ b/assets/js/fetch-repo.js @@ -1,4 +1,4 @@ -async function fetchRepo() { +(async () => { const script = document.currentScript; const repoURL = script?.getAttribute("data-repo-url"); const repoId = script?.getAttribute("data-repo-id"); @@ -54,6 +54,4 @@ async function fetchRepo() { } catch (error) { console.error(error); } -} - -fetchRepo(); +})(); diff --git a/layouts/shortcodes/codeberg.html b/layouts/shortcodes/codeberg.html index 15faea91..0fa21e78 100644 --- a/layouts/shortcodes/codeberg.html +++ b/layouts/shortcodes/codeberg.html @@ -52,6 +52,7 @@ {{ $fetchRepo := resources.Get "js/fetch-repo.js" }} {{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}