Skip to content

Commit b746356

Browse files
committed
update views numbers
1 parent 0224a38 commit b746356

6 files changed

Lines changed: 124 additions & 1 deletion

File tree

_data/view_counts.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Blog Post View Counts
2+
# This file stores view counts for blog posts
3+
# Format: post_slug: view_count
4+
5+
# Example entries (will be populated by JavaScript)
6+
# 2025-10-22-probability-for-ml: 0
7+
# 2025-10-23-hill-climbing-search: 0

_layouts/post.liquid

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ layout: default
2121
{% if page.author %}by {{ page.author }}{% endif %}
2222
{% if page.last_updated %}, last updated on {{ page.last_updated | date: '%B %d, %Y' }}{% endif %}
2323
{% if page.meta %}• {{ page.meta }}{% endif %}
24+
{% if url_beginning == '/blog/' %}• <span id="view-counter" class="view-counter">0 views</span>{% endif %}
2425
</p>
2526
<p class="post-tags">
2627
{% if url_beginning == '/blog/' %}
@@ -94,3 +95,7 @@ layout: default
9495
{% include giscus.liquid %}
9596
{% endif %}
9697
</div>
98+
99+
{% if url_beginning == '/blog/' %}
100+
<script src="{{ '/assets/js/view-counter.js' | relative_url }}"></script>
101+
{% endif %}

_pages/blog.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pagination:
8484
{{ read_time }} min read &nbsp; &middot; &nbsp;
8585
<a href="{{ year | prepend: '/blog/' | relative_url }}">
8686
<i class="fa-solid fa-calendar fa-sm"></i> {{ year }} </a>
87+
&nbsp; &middot; &nbsp; <span id="view-counter-{{ post.slug }}" class="view-counter">0 views</span>
8788
</p>
8889
</div>
8990
</div>
@@ -140,6 +141,7 @@ pagination:
140141
{% if post.external_source %}
141142
&nbsp; &middot; &nbsp; {{ post.external_source }}
142143
{% endif %}
144+
&nbsp; &middot; &nbsp; <span id="view-counter-{{ post.slug }}" class="view-counter">0 views</span>
143145
</p>
144146
<p class="post-tags">
145147
<a href="{{ year | prepend: '/blog/' | relative_url }}">
@@ -186,3 +188,19 @@ pagination:
186188

187189
</div>
188190

191+
<script src="{{ '/assets/js/view-counter.js' | relative_url }}"></script>
192+
<script>
193+
// Load view counts for all posts on blog page
194+
document.addEventListener('DOMContentLoaded', function() {
195+
if (window.viewCounter) {
196+
// Get all post slugs from the page
197+
const postElements = document.querySelectorAll('[id^="view-counter-"]');
198+
postElements.forEach(function(element) {
199+
const postSlug = element.id.replace('view-counter-', '');
200+
const viewCount = window.viewCounter.getViewCount(postSlug);
201+
element.textContent = `${viewCount} view${viewCount !== 1 ? 's' : ''}`;
202+
});
203+
}
204+
});
205+
</script>
206+

_posts/2025-10-23-hill-climbing-search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,5 @@ Use keyboard shortcuts:
8989

9090
## References
9191

92-
- [Predicate Invention for Bilevel Planning](https://arxiv.org/abs/2203.09634). Silver*, Chitnis*, Kumar, McClinton, Lozano-Perez, Kaelbling, Tenenbaum. AAAI 2023.
92+
- [Predicate Invention for Bilevel Planning](https://arxiv.org/abs/2203.09634). Silver\*, Chitnis\*, Kumar, McClinton, Lozano-Perez, Kaelbling, Tenenbaum. AAAI 2023.
9393

assets/css/main.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,20 @@ $max-content-width: {{ site.max_width }};
2323
"tabler-icons/tabler-icons-filled.scss",
2424
"tabler-icons/tabler-icons-outline.scss"
2525
;
26+
27+
// View Counter Styles
28+
.view-counter {
29+
color: #666;
30+
font-size: 0.9em;
31+
font-weight: 500;
32+
33+
&::before {
34+
content: "👁️ ";
35+
margin-right: 2px;
36+
}
37+
}
38+
39+
.post-meta .view-counter {
40+
color: #888;
41+
font-size: 0.85em;
42+
}

assets/js/view-counter.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// View Counter for Blog Posts
2+
// Tracks page views and stores them in localStorage
3+
4+
class ViewCounter {
5+
constructor() {
6+
this.storageKey = 'blog_view_counts';
7+
this.currentPost = this.getCurrentPostSlug();
8+
this.init();
9+
}
10+
11+
getCurrentPostSlug() {
12+
// Extract post slug from URL
13+
const path = window.location.pathname;
14+
const match = path.match(/\/blog\/(\d{4}-\d{2}-\d{2}-[^\/]+)/);
15+
return match ? match[1] : null;
16+
}
17+
18+
init() {
19+
if (this.currentPost) {
20+
this.incrementViewCount();
21+
this.updateViewDisplay();
22+
}
23+
}
24+
25+
getViewCounts() {
26+
try {
27+
const stored = localStorage.getItem(this.storageKey);
28+
return stored ? JSON.parse(stored) : {};
29+
} catch (e) {
30+
console.warn('Could not load view counts from localStorage:', e);
31+
return {};
32+
}
33+
}
34+
35+
setViewCounts(counts) {
36+
try {
37+
localStorage.setItem(this.storageKey, JSON.stringify(counts));
38+
} catch (e) {
39+
console.warn('Could not save view counts to localStorage:', e);
40+
}
41+
}
42+
43+
incrementViewCount() {
44+
const counts = this.getViewCounts();
45+
counts[this.currentPost] = (counts[this.currentPost] || 0) + 1;
46+
this.setViewCounts(counts);
47+
}
48+
49+
getViewCount(postSlug) {
50+
const counts = this.getViewCounts();
51+
return counts[postSlug] || 0;
52+
}
53+
54+
updateViewDisplay() {
55+
const viewCount = this.getViewCount(this.currentPost);
56+
const viewElement = document.getElementById('view-counter');
57+
if (viewElement) {
58+
viewElement.textContent = `${viewCount} view${viewCount !== 1 ? 's' : ''}`;
59+
}
60+
}
61+
62+
// Method to get all view counts for blog listing
63+
getAllViewCounts() {
64+
return this.getViewCounts();
65+
}
66+
}
67+
68+
// Initialize view counter when DOM is loaded
69+
document.addEventListener('DOMContentLoaded', function() {
70+
window.viewCounter = new ViewCounter();
71+
});
72+
73+
// Export for use in other scripts
74+
if (typeof module !== 'undefined' && module.exports) {
75+
module.exports = ViewCounter;
76+
}

0 commit comments

Comments
 (0)