Add top posts functionality

This commit is contained in:
Alex Selimov 2026-06-15 15:49:01 -04:00
parent 2c2a275a95
commit 3ced4c6885
3 changed files with 51 additions and 3 deletions

View file

@ -121,11 +121,11 @@ blockquote {
} }
ul.blog-posts { ul.blog-posts {
list-style-type: none; list-style-type: disc;
padding: unset; padding: unset;
} }
ul.blog-posts li { ul.blog-posts li {
display: flex; display: list-item;
flex-direction: column; flex-direction: column;
} }
ul.blog-posts li span { ul.blog-posts li span {

View file

@ -15,8 +15,16 @@
<p><a href="/posts/">All posts →</a></p> <p><a href="/posts/">All posts →</a></p>
<h2>My top posts</h2>
<ul class="blog-posts" id="top-posts">
</ul>
<h2> Activity map</h2> <h2> Activity map</h2>
{{ partial "git-heatmap.html" . }} {{ partial "git-heatmap.html" . }}
</content> </content>
<script>
window.UPVOTE_API = "{{ .Site.Params.upvoteApi }}";
window.NUM_BEST = "{{ .Site.Params.numBest }}"
</script>
<script src="/top_posts.js"></script>
{{ end }} {{ end }}

40
static/top_posts.js Normal file
View file

@ -0,0 +1,40 @@
(async function () {
console.log("Starting top_posts");
const API = window.UPVOTE_API;
const NUM_BEST = window.NUM_BEST;
const res = await fetch(`${API}/posts/best?top=${NUM_BEST}`, {
credentials: "include",
});
const data = await res.json();
const URL = window.location.origin;
const ul = document.getElementById("top-posts");
for (post of data) {
const postUrl = post.slug.replace("post-", "");
const pageUrl = `/posts/${postUrl}`;
try {
const pageResponse = await fetch(pageUrl);
const html = await pageResponse.text();
const doc = new DOMParser().parseFromString(html, "text/html");
const title =
doc.querySelector('meta[property="og:title"]')?.content ||
doc.title ||
slug;
const li = document.createElement("li");
li.innerHTML = `
<a href="/posts/${postUrl}">
${title}
</a>
`;
ul.appendChild(li);
} catch (err) {
console.error(err);
}
}
})();