From 37ab1055e7f82fc4f51b7063cbbbacbc38441cf9 Mon Sep 17 00:00:00 2001
From: Served Smart <195884188+servedsmart@users.noreply.github.com>
Date: Wed, 4 Jun 2025 23:51:03 +0200
Subject: [PATCH 1/3] :recycle: refactor: Move similar javascript inline code
out of line
- Move variables oid, oid_likes out of line
- Move background-blur out of line
- Move fetch-repo code out of line
---
assets/js/background-blur.js | 13 +++++++
assets/js/fetch-repo.js | 30 ++++++++++++++++
assets/js/page.js | 36 ++++++++++---------
layouts/_default/list.html | 6 +---
layouts/_default/single.html | 8 ++---
layouts/_default/term.html | 6 +---
layouts/_default/terms.html | 6 +---
layouts/partials/header/fixed-fill-blur.html | 10 ++----
layouts/partials/header/fixed-gradient.html | 10 ++----
layouts/partials/header/fixed.html | 10 ++----
layouts/partials/hero/background.html | 10 ++----
layouts/partials/hero/thumbAndBackground.html | 10 ++----
layouts/partials/home/background.html | 10 ++----
layouts/shortcodes/codeberg.html | 18 ++--------
layouts/shortcodes/forgejo.html | 18 ++--------
layouts/shortcodes/gitea.html | 18 ++--------
layouts/shortcodes/github.html | 18 ++--------
layouts/shortcodes/gitlab.html | 18 ++--------
18 files changed, 100 insertions(+), 155 deletions(-)
create mode 100644 assets/js/background-blur.js
create mode 100644 assets/js/fetch-repo.js
diff --git a/assets/js/background-blur.js b/assets/js/background-blur.js
new file mode 100644
index 00000000..bf42aa58
--- /dev/null
+++ b/assets/js/background-blur.js
@@ -0,0 +1,13 @@
+function backgroundBlur() {
+ const script = document.currentScript;
+ const targetId = script && script.getAttribute("data-target-id") ? script.getAttribute("data-target-id")
+ : (console.error("data-target-id is null"), null);
+
+ window.addEventListener("scroll", () => {
+ const scroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
+ const backgroundBlur = document.getElementById(targetId);
+ backgroundBlur.style.opacity = scroll / 300;
+ });
+}
+
+backgroundBlur();
diff --git a/assets/js/fetch-repo.js b/assets/js/fetch-repo.js
new file mode 100644
index 00000000..c7ad744e
--- /dev/null
+++ b/assets/js/fetch-repo.js
@@ -0,0 +1,30 @@
+function fetchRepo() {
+ const script = document.currentScript;
+ const repoURL = script && script.getAttribute("data-url") ? script.getAttribute("data-url")
+ : (console.error("data-url is null"), null);
+ const repoId = script && script.getAttribute("data-id") ? script.getAttribute("data-id")
+ : (console.error("data-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.innerHTML for '${repoId}-${requestObject}' is null`), null);
+ });
+ })
+ .catch((error) => console.error(error));
+}
+
+fetchRepo();
diff --git a/assets/js/page.js b/assets/js/page.js
index e3cb9794..0aef831e 100644
--- a/assets/js/page.js
+++ b/assets/js/page.js
@@ -1,15 +1,17 @@
-var liked_page = false
-var id = oid ? oid.replaceAll("/", "-") : oid
-var id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes
+const pageScriptElement = document.currentScript;
+const oid = pageScriptElement && pageScriptElement.getAttribute("data-oid") ? pageScriptElement.getAttribute("data-oid") : (console.error("data-oid is null"), null);
+const oid_likes = pageScriptElement && pageScriptElement.getAttribute("data-oid-likes") ? pageScriptElement.getAttribute("data-oid-likes") : (console.error("data-oid-likes is null"), null);
+const liked_page = false;
+const id = oid ? oid.replaceAll("/", "-") : oid;
+const id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes;
-if (typeof auth !== 'undefined') {
-
- var viewed = localStorage.getItem(id);
+if (typeof auth !== "undefined") {
+ const viewed = localStorage.getItem(id);
if (!viewed) {
auth.signInAnonymously()
.then(() => {
- var docRef = db.collection('views').doc(id)
+ const docRef = db.collection('views').doc(id)
localStorage.setItem(id, true);
docRef.get().then((doc) => {
if (doc.exists) {
@@ -24,13 +26,13 @@ if (typeof auth !== 'undefined') {
});
})
.catch((error) => {
- var errorCode = error.code;
- var errorMessage = error.message;
+ const errorCode = error.code;
+ const errorMessage = error.message;
console.error(errorCode, errorMessage)
});
}
- var liked = localStorage.getItem(id_likes);
+ const liked = localStorage.getItem(id_likes);
if (liked) {
liked_page = true
@@ -44,7 +46,7 @@ if (typeof auth !== 'undefined') {
function like_article(id_likes) {
auth.signInAnonymously()
.then(() => {
- var docRef = db.collection('likes').doc(id_likes)
+ const docRef = db.collection('likes').doc(id_likes)
docRef.get().then((doc) => {
liked_page = true
localStorage.setItem(id_likes, true);
@@ -63,8 +65,8 @@ function like_article(id_likes) {
});
})
.catch((error) => {
- var errorCode = error.code;
- var errorMessage = error.message;
+ const errorCode = error.code;
+ const errorMessage = error.message;
console.error(errorCode, errorMessage)
});
}
@@ -72,7 +74,7 @@ function like_article(id_likes) {
function remove_like_article(id_likes) {
auth.signInAnonymously()
.then(() => {
- var docRef = db.collection('likes').doc(id_likes)
+ const docRef = db.collection('likes').doc(id_likes)
docRef.get().then((doc) => {
liked_page = false
localStorage.removeItem(id_likes);
@@ -91,8 +93,8 @@ function remove_like_article(id_likes) {
});
})
.catch((error) => {
- var errorCode = error.code;
- var errorMessage = error.message;
+ const errorCode = error.code;
+ const errorMessage = error.message;
console.error(errorCode, errorMessage)
});
}
@@ -103,4 +105,4 @@ function process_article() {
} else {
remove_like_article(id_likes)
}
-}
\ No newline at end of file
+}
diff --git a/layouts/_default/list.html b/layouts/_default/list.html
index d236e9b0..6533b347 100644
--- a/layouts/_default/list.html
+++ b/layouts/_default/list.html
@@ -26,13 +26,9 @@
{{ $lang := print "." .Lang ".md" }}
{{ $path = replace $path $lang ".md" }}
{{end}}
-
{{ $jsPage := resources.Get "js/page.js" }}
{{ $jsPage = $jsPage | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{ end }}
-
+
{{ $translations := .AllTranslations }}
{{ with .File }}
{{ $path := .Path }}
@@ -140,13 +140,9 @@
{{ $lang := print "." .Lang ".md" }}
{{ $path = replace $path $lang ".md" }}
{{end}}
-
{{ $jsPage := resources.Get "js/page.js" }}
{{ $jsPage = $jsPage | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{ end }}
diff --git a/layouts/_default/term.html b/layouts/_default/term.html
index e8448aa1..40c8cd04 100644
--- a/layouts/_default/term.html
+++ b/layouts/_default/term.html
@@ -26,13 +26,9 @@
{{ .Content }}
-
{{ $jsPage := resources.Get "js/page.js" }}
{{ $jsPage = $jsPage | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{ end }}
diff --git a/layouts/_default/terms.html b/layouts/_default/terms.html
index 510f24ed..85b6ab73 100644
--- a/layouts/_default/terms.html
+++ b/layouts/_default/terms.html
@@ -28,13 +28,9 @@
{{ .Content }}
-
{{ $jsPage := resources.Get "js/page.js" }}
{{ $jsPage = $jsPage | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{ end }}
{{ if .Site.Params.taxonomy.cardView }}
diff --git a/layouts/partials/header/fixed-fill-blur.html b/layouts/partials/header/fixed-fill-blur.html
index 02160bfe..1a0ceb01 100644
--- a/layouts/partials/header/fixed-fill-blur.html
+++ b/layouts/partials/header/fixed-fill-blur.html
@@ -5,10 +5,6 @@
{{ partial "header/basic.html" . }}
-
+{{ $backgroundBlur := resources.Get "js/background-blur.js" }}
+{{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
diff --git a/layouts/partials/header/fixed-gradient.html b/layouts/partials/header/fixed-gradient.html
index 2e1301a4..6d8b95ee 100644
--- a/layouts/partials/header/fixed-gradient.html
+++ b/layouts/partials/header/fixed-gradient.html
@@ -6,10 +6,6 @@
{{ partial "header/basic.html" . }}
-
+{{ $backgroundBlur := resources.Get "js/background-blur.js" }}
+{{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
diff --git a/layouts/partials/header/fixed.html b/layouts/partials/header/fixed.html
index eee26d9d..7f1d8c4b 100644
--- a/layouts/partials/header/fixed.html
+++ b/layouts/partials/header/fixed.html
@@ -5,10 +5,6 @@
{{ partial "header/basic.html" . }}
-
+{{ $backgroundBlur := resources.Get "js/background-blur.js" }}
+{{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
diff --git a/layouts/partials/hero/background.html b/layouts/partials/hero/background.html
index 66799507..c3adf2e0 100644
--- a/layouts/partials/hero/background.html
+++ b/layouts/partials/hero/background.html
@@ -55,12 +55,8 @@
{{ if $shouldBlur | default false }}
-
+{{ $backgroundBlur := resources.Get "js/background-blur.js" }}
+{{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{ end }}
{{- end -}}
diff --git a/layouts/partials/hero/thumbAndBackground.html b/layouts/partials/hero/thumbAndBackground.html
index 036ab514..d4cff87b 100644
--- a/layouts/partials/hero/thumbAndBackground.html
+++ b/layouts/partials/hero/thumbAndBackground.html
@@ -79,12 +79,8 @@
{{ if $shouldBlur | default false }}
-
+{{ $backgroundBlur := resources.Get "js/background-blur.js" }}
+{{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{ end }}
diff --git a/layouts/partials/home/background.html b/layouts/partials/home/background.html
index 7bba6103..c011d816 100644
--- a/layouts/partials/home/background.html
+++ b/layouts/partials/home/background.html
@@ -78,11 +78,7 @@
{{ if .Site.Params.homepage.layoutBackgroundBlur | default false }}
-
+{{ $backgroundBlur := resources.Get "js/background-blur.js" }}
+{{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{ end }}
diff --git a/layouts/shortcodes/codeberg.html b/layouts/shortcodes/codeberg.html
index 49fe7380..13763044 100644
--- a/layouts/shortcodes/codeberg.html
+++ b/layouts/shortcodes/codeberg.html
@@ -49,21 +49,9 @@
-
+ {{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
+ {{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{- end -}}
diff --git a/layouts/shortcodes/forgejo.html b/layouts/shortcodes/forgejo.html
index 6bd3de2b..21c61354 100644
--- a/layouts/shortcodes/forgejo.html
+++ b/layouts/shortcodes/forgejo.html
@@ -49,21 +49,9 @@
-
+ {{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
+ {{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{- end -}}
diff --git a/layouts/shortcodes/gitea.html b/layouts/shortcodes/gitea.html
index 23bcb874..30dd6162 100644
--- a/layouts/shortcodes/gitea.html
+++ b/layouts/shortcodes/gitea.html
@@ -49,21 +49,9 @@
-
+ {{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
+ {{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{- end -}}
diff --git a/layouts/shortcodes/github.html b/layouts/shortcodes/github.html
index 02b9f84d..dfcfaf36 100644
--- a/layouts/shortcodes/github.html
+++ b/layouts/shortcodes/github.html
@@ -65,21 +65,9 @@
-
+ {{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
+ {{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{- end -}}
diff --git a/layouts/shortcodes/gitlab.html b/layouts/shortcodes/gitlab.html
index 3bdcd2c7..2467f9e0 100644
--- a/layouts/shortcodes/gitlab.html
+++ b/layouts/shortcodes/gitlab.html
@@ -39,21 +39,9 @@
-
+ {{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
+ {{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
+
{{- end -}}
From 4ff3d07d30b9dde9fa65830fb6ce1beb36d9dd18 Mon Sep 17 00:00:00 2001
From: Served Smart <195884188+servedsmart@users.noreply.github.com>
Date: Thu, 5 Jun 2025 16:05:22 +0200
Subject: [PATCH 2/3] :art: structure: Use more explicit naming in fetchRepo()
---
assets/js/fetch-repo.js | 12 ++++++------
layouts/shortcodes/codeberg.html | 2 +-
layouts/shortcodes/forgejo.html | 2 +-
layouts/shortcodes/gitea.html | 2 +-
layouts/shortcodes/github.html | 2 +-
layouts/shortcodes/gitlab.html | 2 +-
6 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/assets/js/fetch-repo.js b/assets/js/fetch-repo.js
index c7ad744e..689b1e96 100644
--- a/assets/js/fetch-repo.js
+++ b/assets/js/fetch-repo.js
@@ -1,9 +1,9 @@
function fetchRepo() {
- const script = document.currentScript;
- const repoURL = script && script.getAttribute("data-url") ? script.getAttribute("data-url")
- : (console.error("data-url is null"), null);
- const repoId = script && script.getAttribute("data-id") ? script.getAttribute("data-id")
- : (console.error("data-id is null"), null);
+ 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"]
@@ -21,7 +21,7 @@ function fetchRepo() {
element = document.getElementById(`${repoId}-stargazers`);
}
element ? (element.innerHTML = data[requestObject])
- : (console.error(`element.innerHTML for '${repoId}-${requestObject}' is null`), null);
+ : (console.error(`Element '${repoId}-${requestObject}' not found`), null);
});
})
.catch((error) => console.error(error));
diff --git a/layouts/shortcodes/codeberg.html b/layouts/shortcodes/codeberg.html
index 13763044..4f9a251c 100644
--- a/layouts/shortcodes/codeberg.html
+++ b/layouts/shortcodes/codeberg.html
@@ -51,7 +51,7 @@
{{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
{{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{- end -}}
diff --git a/layouts/shortcodes/forgejo.html b/layouts/shortcodes/forgejo.html
index 21c61354..2424fd9a 100644
--- a/layouts/shortcodes/forgejo.html
+++ b/layouts/shortcodes/forgejo.html
@@ -51,7 +51,7 @@
{{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
{{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{- end -}}
diff --git a/layouts/shortcodes/gitea.html b/layouts/shortcodes/gitea.html
index 30dd6162..79418776 100644
--- a/layouts/shortcodes/gitea.html
+++ b/layouts/shortcodes/gitea.html
@@ -51,7 +51,7 @@
{{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
{{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{- end -}}
diff --git a/layouts/shortcodes/github.html b/layouts/shortcodes/github.html
index dfcfaf36..0d471898 100644
--- a/layouts/shortcodes/github.html
+++ b/layouts/shortcodes/github.html
@@ -67,7 +67,7 @@
{{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
{{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{- end -}}
diff --git a/layouts/shortcodes/gitlab.html b/layouts/shortcodes/gitlab.html
index 2467f9e0..e7b9d9fe 100644
--- a/layouts/shortcodes/gitlab.html
+++ b/layouts/shortcodes/gitlab.html
@@ -41,7 +41,7 @@
{{ $fetchRepo := resources.Get "js/fetch-repo.js" }}
{{ $fetchRepo = $fetchRepo | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }}
-
+
{{- end -}}
From 9cb0db3e0828aa1e714d01b67d6e54d8c16faaee Mon Sep 17 00:00:00 2001
From: Served Smart <195884188+servedsmart@users.noreply.github.com>
Date: Thu, 5 Jun 2025 16:05:30 +0200
Subject: [PATCH 3/3] :art: structure: Use more explicit naming in
setBackgroundBlur()
---
assets/js/background-blur.js | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/assets/js/background-blur.js b/assets/js/background-blur.js
index bf42aa58..57ba149d 100644
--- a/assets/js/background-blur.js
+++ b/assets/js/background-blur.js
@@ -1,6 +1,6 @@
-function backgroundBlur() {
- const script = document.currentScript;
- const targetId = script && script.getAttribute("data-target-id") ? script.getAttribute("data-target-id")
+function setBackgroundBlur() {
+ const scriptElement = document.currentScript;
+ const targetId = scriptElement && scriptElement.getAttribute("data-target-id") ? scriptElement.getAttribute("data-target-id")
: (console.error("data-target-id is null"), null);
window.addEventListener("scroll", () => {
@@ -10,4 +10,4 @@ function backgroundBlur() {
});
}
-backgroundBlur();
+setBackgroundBlur();