mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-06-30 02:05:57 -04:00
clean up type declarations
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import fetch from "node-fetch";
|
||||
|
||||
// don't continue if there isn't a span#meta-hits element on this page
|
||||
const wrapper = document.getElementById("meta-hits");
|
||||
|
||||
if (wrapper) {
|
||||
// javascript is enabled so show the loading indicator
|
||||
wrapper.style.display = "inline-block";
|
||||
|
||||
// deduce a consistent identifier for this page, no matter the URL
|
||||
const canonical = document.createElement("a");
|
||||
canonical.href = document.querySelector("link[rel='canonical']").href;
|
||||
|
||||
// strip beginning and ending forward slash
|
||||
const slug = canonical.pathname.slice(1, -1);
|
||||
|
||||
fetch("/api/hits/?slug=" + slug)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
if (typeof data.hits !== "undefined") {
|
||||
// finally inject the hits and hide the loading spinner
|
||||
const spinner = document.getElementById("hit-spinner");
|
||||
const counter = document.getElementById("hit-counter");
|
||||
|
||||
if (spinner) spinner.style.display = "none";
|
||||
if (counter) counter.appendChild(document.createTextNode(data.pretty_hits));
|
||||
wrapper.title = data.pretty_hits + " " + data.pretty_unit;
|
||||
} else {
|
||||
// something went horribly wrong, initiate coverup
|
||||
wrapper.style.display = "none";
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
// something went horribly wrong, initiate coverup
|
||||
wrapper.style.display = "none";
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*! Dark mode switcheroo | MIT License | jrvs.io/darkmode */
|
||||
|
||||
// improve variable mangling when minifying
|
||||
var win = window;
|
||||
var doc = win.document;
|
||||
var body = doc.body;
|
||||
var classes = body.classList;
|
||||
var storage = localStorage;
|
||||
|
||||
// check for preset `dark_mode_pref` preference in local storage
|
||||
var pref_key = "dark_mode_pref";
|
||||
var pref = storage.getItem(pref_key);
|
||||
|
||||
// change CSS via these <body> classes:
|
||||
var dark = "dark";
|
||||
var light = "light";
|
||||
|
||||
// which class is <body> set to initially?
|
||||
// eslint-disable-next-line
|
||||
var default_theme = "{{ .Site.Params.Theme.defaultTheme }}";
|
||||
|
||||
// use an element with class `dark-mode-toggle` to trigger swap when clicked
|
||||
var toggle = doc.querySelector(".dark-mode-toggle");
|
||||
|
||||
// keep track of current state no matter how we got there
|
||||
var active = default_theme === dark;
|
||||
|
||||
// receives a class name and switches <body> to it
|
||||
var activateTheme = function (theme) {
|
||||
classes.remove(dark, light);
|
||||
classes.add(theme);
|
||||
active = theme === dark;
|
||||
};
|
||||
|
||||
// if user already explicitly toggled in the past, restore their preference
|
||||
if (pref === dark) activateTheme(dark);
|
||||
if (pref === light) activateTheme(light);
|
||||
|
||||
// user has never clicked the button, so go by their OS preference until/if they do so
|
||||
if (!pref) {
|
||||
// returns media query selector syntax
|
||||
var prefers = function (theme) {
|
||||
return "(prefers-color-scheme: " + theme + ")";
|
||||
};
|
||||
|
||||
// check for OS dark/light mode preference and switch accordingly
|
||||
// default to `default_theme` set above if unsupported
|
||||
if (win.matchMedia(prefers(dark)).matches) activateTheme(dark);
|
||||
else if (win.matchMedia(prefers(light)).matches) activateTheme(light);
|
||||
else activateTheme(default_theme);
|
||||
|
||||
// real-time switching if supported by OS/browser
|
||||
win.matchMedia(prefers(dark)).addListener(function (e) {
|
||||
if (e.matches) activateTheme(dark);
|
||||
});
|
||||
win.matchMedia(prefers(light)).addListener(function (e) {
|
||||
if (e.matches) activateTheme(light);
|
||||
});
|
||||
}
|
||||
|
||||
// don't freak out if page happens not to have a toggle
|
||||
if (toggle) {
|
||||
// toggle re-appears now that we know user has JS enabled
|
||||
toggle.style.display = "block";
|
||||
|
||||
// handle toggle click
|
||||
toggle.addEventListener(
|
||||
"click",
|
||||
function () {
|
||||
// switch to the opposite theme & save preference in local storage
|
||||
if (active) {
|
||||
activateTheme(light);
|
||||
storage.setItem(pref_key, light);
|
||||
} else {
|
||||
activateTheme(dark);
|
||||
storage.setItem(pref_key, dark);
|
||||
}
|
||||
},
|
||||
true
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import fetch from "node-fetch";
|
||||
|
||||
// don't continue if there isn't a span#meta-hits element on this page
|
||||
const wrapper = document.getElementById("github-cards");
|
||||
|
||||
if (wrapper) {
|
||||
fetch("/api/projects/?top")
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
data.forEach((repo) => {
|
||||
let html = `
|
||||
<a class="repo-name" href="${repo.url}" target="_blank" rel="noopener">${repo.name}</a>
|
||||
<p class="repo-description">${repo.description}</p>`;
|
||||
|
||||
if (repo.primaryLanguage) {
|
||||
html += `
|
||||
<div class="repo-meta">
|
||||
<span class="repo-language-color" style="background-color: ${repo.primaryLanguage.color}"></span>
|
||||
<span>${repo.primaryLanguage.name}</span>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
if (repo.stargazerCount > 0) {
|
||||
html += `
|
||||
<div class="repo-meta">
|
||||
<svg viewBox="0 0 16 16" height="16" width="16"><path fill-rule="evenodd" d="M8 .25a.75.75 0 01.673.418l1.882 3.815 4.21.612a.75.75 0 01.416 1.279l-3.046 2.97.719 4.192a.75.75 0 01-1.088.791L8 12.347l-3.766 1.98a.75.75 0 01-1.088-.79l.72-4.194L.818 6.374a.75.75 0 01.416-1.28l4.21-.611L7.327.668A.75.75 0 018 .25zm0 2.445L6.615 5.5a.75.75 0 01-.564.41l-3.097.45 2.24 2.184a.75.75 0 01.216.664l-.528 3.084 2.769-1.456a.75.75 0 01.698 0l2.77 1.456-.53-3.084a.75.75 0 01.216-.664l2.24-2.183-3.096-.45a.75.75 0 01-.564-.41L8 2.694v.001z"></path></svg>
|
||||
<span>${repo.stargazerCount_pretty}</span>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
if (repo.forkCount > 0) {
|
||||
html += `
|
||||
<div class="repo-meta">
|
||||
<svg viewBox="0 0 16 16" height="16" width="16"><path fill-rule="evenodd" d="M5 3.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm0 2.122a2.25 2.25 0 10-1.5 0v.878A2.25 2.25 0 005.75 8.5h1.5v2.128a2.251 2.251 0 101.5 0V8.5h1.5a2.25 2.25 0 002.25-2.25v-.878a2.25 2.25 0 10-1.5 0v.878a.75.75 0 01-.75.75h-4.5A.75.75 0 015 6.25v-.878zm3.75 7.378a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm3-8.75a.75.75 0 100-1.5.75.75 0 000 1.5z"></path></svg>
|
||||
<span>${repo.forkCount_pretty}</span>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
html += `
|
||||
<div class="repo-meta">
|
||||
<span title="${repo.pushedAt}">Updated ${repo.pushedAt_relative}</span>
|
||||
</div>`;
|
||||
|
||||
const div = document.createElement("div");
|
||||
div.classList.add("github-card");
|
||||
div.innerHTML = html;
|
||||
wrapper.appendChild(div);
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
// something went horribly wrong, initiate coverup
|
||||
wrapper.style.display = "none";
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user