Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "virtual:group-icons.css";
import "./custom.css";
import "./landing.css";
import Layout from "./Layout.vue";
import { onMounted } from "vue";
import { onMounted, onUnmounted } from "vue";
import { data as starsData } from "../stars.data";

export default {
Expand Down Expand Up @@ -35,13 +35,18 @@ export default {
};
},
setup() {
let observer: MutationObserver | undefined;
onMounted(() => {
// Add star count to GitHub social link
// The nav bar renders the GitHub link twice: inline, and inside the "..."
// overflow menu that replaces it below 1280px. Both need their own badge.
const addStarCount = () => {
const githubLink = document.querySelector(
if (!starsData.stars) return false;

const githubLinks = document.querySelectorAll(
'.VPSocialLinks a[href*="github.com/jdx/mise"]',
);
if (githubLink && !githubLink.querySelector(".star-count")) {
githubLinks.forEach((githubLink) => {
if (githubLink.querySelector(".star-count")) return;
const starBadge = document.createElement("span");
starBadge.className = "star-count";
starBadge.title = "GitHub Stars";
Expand All @@ -51,16 +56,27 @@ export default {
glyph.setAttribute("aria-hidden", "true");
starBadge.append(glyph, starsData.stars);
githubLink.appendChild(starBadge);
}
});
return (
githubLinks.length > 0 &&
Array.from(githubLinks).every((link) =>
link.querySelector(".star-count"),
)
);
};

// Try immediately and after a short delay to ensure DOM is ready
addStarCount();
setTimeout(addStarCount, 100);
if (addStarCount()) return;

// Also watch for route changes
const observer = new MutationObserver(addStarCount);
observer.observe(document.body, { childList: true, subtree: true });
// The nav mounts after the theme, so wait for it rather than re-running on
// every mutation the page makes for the rest of its life.
observer = new MutationObserver(() => {
if (addStarCount()) observer?.disconnect();
});
observer.observe(document.querySelector(".VPNav") || document.body, {
childList: true,
subtree: true,
});
});
onUnmounted(() => observer?.disconnect());
},
} satisfies Theme;
Loading