mirror of
https://github.com/privacyguides/privacyguides.org.git
synced 2024-10-01 01:35:57 -04:00
feat: Generate homepage topics on server side
This commit is contained in:
parent
ec0701bfa8
commit
7f4339a0d7
104
index-generation.sh
Executable file
104
index-generation.sh
Executable file
@ -0,0 +1,104 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
DATE_CMD="date"
|
||||||
|
|
||||||
|
# Check if the script is running on macOS
|
||||||
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||||
|
DATE_CMD="gdate"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# URL of the Discourse top.json
|
||||||
|
DISCOURSE_URL="https://discuss.privacyguides.net/top.json?period=weekly"
|
||||||
|
|
||||||
|
# Fetch the JSON data
|
||||||
|
json_data=$(curl -s $DISCOURSE_URL)
|
||||||
|
|
||||||
|
# Extract the first 3 topics
|
||||||
|
topics=$(echo $json_data | jq -r '.topic_list.topics[:3]')
|
||||||
|
|
||||||
|
# Generate HTML for the first 3 posts
|
||||||
|
html_output=""
|
||||||
|
for row in $(echo "${topics}" | jq -r '.[] | @base64'); do
|
||||||
|
_jq() {
|
||||||
|
echo ${row} | base64 --decode | jq -r ${1}
|
||||||
|
}
|
||||||
|
|
||||||
|
title=$(_jq '.title')
|
||||||
|
id=$(_jq '.id')
|
||||||
|
like_count=$(_jq '.like_count')
|
||||||
|
reply_count=$(_jq '.posts_count')
|
||||||
|
views=$(_jq '.views')
|
||||||
|
last_poster_username=$(_jq '.last_poster_username')
|
||||||
|
last_posted_at=$(_jq '.last_posted_at')
|
||||||
|
readable_date=$($DATE_CMD -d "$last_posted_at" +"%b %d, %Y %H:%M")
|
||||||
|
|
||||||
|
html_output+="<li class='discourse-topic'>"
|
||||||
|
html_output+="<p class='discourse-title'><a href='https://discuss.privacyguides.net/t/${id}'><strong>${title}</strong></a></p>"
|
||||||
|
html_output+="<hr>"
|
||||||
|
html_output+="<p class='discourse-author'>"
|
||||||
|
html_output+="<span class='discourse-author'>"
|
||||||
|
html_output+="<img src='https://forum-cdn.privacyguides.net/user_avatar/discuss.privacyguides.net/${last_poster_username}/48/1.png' alt='${last_poster_username}' width='20' height='20' class='middle'>"
|
||||||
|
html_output+="<span> Last updated: ${readable_date}</span>"
|
||||||
|
html_output+="</span>"
|
||||||
|
html_output+="</p>"
|
||||||
|
html_output+="<p class='discourse-data'>"
|
||||||
|
html_output+="<span class='twemoji'><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><title>eye</title><path d='M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9M12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17M12,4.5C7,4.5 2.73,7.61 1,12C2.73,16.39 7,19.5 12,19.5C17,19.5 21.27,16.39 23,12C21.27,7.61 17,4.5 12,4.5Z' /></svg></span>"
|
||||||
|
html_output+="<span class='discourse-views'> ${views} </span>"
|
||||||
|
html_output+="<span class='twemoji pg-red'><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><title>heart</title><path d='M12,21.35L10.55,20.03C5.4,15.36 2,12.27 2,8.5C2,5.41 4.42,3 7.5,3C9.24,3 10.91,3.81 12,5.08C13.09,3.81 14.76,3 16.5,3C19.58,3 22,5.41 22,8.5C22,12.27 18.6,15.36 13.45,20.03L12,21.35Z' /></svg></span>"
|
||||||
|
html_output+="<span class='discourse-likes'> ${like_count} </span>"
|
||||||
|
html_output+="<span class='twemoji'><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><title>reply</title><path d='M10,9V5L3,12L10,19V14.9C15,14.9 18.5,16.5 21,20C20,15 17,10 10,9Z' /></svg></span>"
|
||||||
|
html_output+="<span class='discourse-replies'> ${reply_count} </span>"
|
||||||
|
html_output+="</p>"
|
||||||
|
html_output+="</li>"
|
||||||
|
done
|
||||||
|
|
||||||
|
# Insert the HTML output between the comments in index.html
|
||||||
|
sed -i '' '/<!-- start top posts -->/,/<!-- end top posts -->/{//!d;}; /<!-- start top posts -->/r /dev/stdin' /Volumes/Git/privacyguides.org/site/en/index.html <<< "$html_output"
|
||||||
|
|
||||||
|
# URL of the Discourse latest.json
|
||||||
|
DISCOURSE_URL="https://discuss.privacyguides.net/latest.json"
|
||||||
|
|
||||||
|
# Fetch the JSON data
|
||||||
|
json_data=$(curl -s $DISCOURSE_URL)
|
||||||
|
|
||||||
|
# Extract the first 12 topics
|
||||||
|
topics=$(echo $json_data | jq -r '.topic_list.topics[:12]')
|
||||||
|
|
||||||
|
# Generate HTML for the first 12 posts
|
||||||
|
html_output=""
|
||||||
|
for row in $(echo "${topics}" | jq -r '.[] | @base64'); do
|
||||||
|
_jq() {
|
||||||
|
echo ${row} | base64 --decode | jq -r ${1}
|
||||||
|
}
|
||||||
|
|
||||||
|
title=$(_jq '.title')
|
||||||
|
id=$(_jq '.id')
|
||||||
|
like_count=$(_jq '.like_count')
|
||||||
|
reply_count=$(_jq '.posts_count')
|
||||||
|
views=$(_jq '.views')
|
||||||
|
last_poster_username=$(_jq '.last_poster_username')
|
||||||
|
last_posted_at=$(_jq '.last_posted_at')
|
||||||
|
readable_date=$($DATE_CMD -d "$last_posted_at" +"%B %d, %Y %H:%M")
|
||||||
|
|
||||||
|
html_output+="<li class='discourse-topic'>"
|
||||||
|
html_output+="<p class='discourse-title'><a href='https://discuss.privacyguides.net/t/${id}'><strong>${title}</strong></a></p>"
|
||||||
|
html_output+="<hr>"
|
||||||
|
html_output+="<p class='discourse-author'>"
|
||||||
|
html_output+="<span class='discourse-author'>"
|
||||||
|
html_output+="<img src='https://forum-cdn.privacyguides.net/user_avatar/discuss.privacyguides.net/${last_poster_username}/48/1.png' alt='${last_poster_username}' width='20' height='20' class='middle'>"
|
||||||
|
html_output+="<span> Last updated: ${readable_date}</span>"
|
||||||
|
html_output+="</span>"
|
||||||
|
html_output+="</p>"
|
||||||
|
html_output+="<p class='discourse-data'>"
|
||||||
|
html_output+="<span class='twemoji'><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><title>eye</title><path d='M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9M12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17M12,4.5C7,4.5 2.73,7.61 1,12C2.73,16.39 7,19.5 12,19.5C17,19.5 21.27,16.39 23,12C21.27,7.61 17,4.5 12,4.5Z' /></svg></span>"
|
||||||
|
html_output+="<span class='discourse-views'> ${views} </span>"
|
||||||
|
html_output+="<span class='twemoji pg-red'><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><title>heart</title><path d='M12,21.35L10.55,20.03C5.4,15.36 2,12.27 2,8.5C2,5.41 4.42,3 7.5,3C9.24,3 10.91,3.81 12,5.08C13.09,3.81 14.76,3 16.5,3C19.58,3 22,5.41 22,8.5C22,12.27 18.6,15.36 13.45,20.03L12,21.35Z' /></svg></span>"
|
||||||
|
html_output+="<span class='discourse-likes'> ${like_count} </span>"
|
||||||
|
html_output+="<span class='twemoji'><svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><title>reply</title><path d='M10,9V5L3,12L10,19V14.9C15,14.9 18.5,16.5 21,20C20,15 17,10 10,9Z' /></svg></span>"
|
||||||
|
html_output+="<span class='discourse-replies'> ${reply_count} </span>"
|
||||||
|
html_output+="</p>"
|
||||||
|
html_output+="</li>"
|
||||||
|
done
|
||||||
|
|
||||||
|
# Insert the HTML output between the comments in index.html
|
||||||
|
sed -i '' '/<!-- start latest posts -->/,/<!-- end latest posts -->/{//!d;}; /<!-- start latest posts -->/r /dev/stdin' /Volumes/Git/privacyguides.org/site/en/index.html <<< "$html_output"
|
@ -1,167 +0,0 @@
|
|||||||
/**
|
|
||||||
* @overview Generates a list of topics on a Discourse forum.
|
|
||||||
* @author Jonah Aragon <jonah@triplebit.net>
|
|
||||||
* @version 3.1.0
|
|
||||||
* @license
|
|
||||||
* Copyright (c) 2023 - 2024 Jonah Aragon
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
async function getData(url) {
|
|
||||||
const response = await fetch(url);
|
|
||||||
return response.json()
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const elements = document.querySelectorAll("ul[data-forum]");
|
|
||||||
|
|
||||||
for (let j = 0; j < elements.length; j++) {
|
|
||||||
|
|
||||||
var topics = elements[j];
|
|
||||||
var dataset = topics.dataset;
|
|
||||||
|
|
||||||
console.log("Fetching data from " + dataset.feed)
|
|
||||||
const data = await getData(dataset.feed);
|
|
||||||
var list = data['topic_list']['topics'];
|
|
||||||
var profiles = data['users'];
|
|
||||||
var count = dataset.count;
|
|
||||||
|
|
||||||
for (var i = 0; i < count; i++) {
|
|
||||||
|
|
||||||
if (list[i]['pinned'] == true) {
|
|
||||||
count++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var title = list[i]['title'];
|
|
||||||
var id = list[i]['id'];
|
|
||||||
|
|
||||||
var topic = document.createElement("li");
|
|
||||||
topic.className = "discourse-topic";
|
|
||||||
|
|
||||||
var h3 = document.createElement('p');
|
|
||||||
h3.className = "discourse-title";
|
|
||||||
var a1 = document.createElement('a');
|
|
||||||
|
|
||||||
a1.href = dataset.forum + '/t/' + id;
|
|
||||||
|
|
||||||
var boldTitle = document.createElement('strong');
|
|
||||||
boldTitle.innerText = title;
|
|
||||||
a1.appendChild(boldTitle);
|
|
||||||
h3.appendChild(a1);
|
|
||||||
|
|
||||||
var authorinfo = document.createElement('p');
|
|
||||||
authorinfo.className = "discourse-author";
|
|
||||||
|
|
||||||
var author_id = list[i]['posters'][0]['user_id'];
|
|
||||||
var author_data = profiles.find(profile => profile['id'] == author_id);
|
|
||||||
var author = document.createElement('span');
|
|
||||||
author.className = "discourse-author";
|
|
||||||
var avatar = document.createElement('img');
|
|
||||||
avatar.src = dataset.forum + author_data['avatar_template'].replace("{size}", "40");
|
|
||||||
avatar.width = 20;
|
|
||||||
avatar.height = 20;
|
|
||||||
avatar.className = "middle";
|
|
||||||
author.appendChild(avatar);
|
|
||||||
var namespan = document.createElement('span');
|
|
||||||
namespan.innerText = " Posted by " + author_data['username'];
|
|
||||||
author.appendChild(namespan);
|
|
||||||
authorinfo.appendChild(author);
|
|
||||||
|
|
||||||
var postinfo = document.createElement('p');
|
|
||||||
postinfo.className = "discourse-data";
|
|
||||||
|
|
||||||
var dateIcon = document.createElement('span');
|
|
||||||
dateIcon.className = "twemoji";
|
|
||||||
dateIcon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1-2.1-2M12.5 7v5.2l4 2.4-1 1L11 13V7h1.5M11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2v1.8Z"></path></svg>';
|
|
||||||
|
|
||||||
var date = document.createElement('span');
|
|
||||||
date.className = "discourse-date";
|
|
||||||
var datestring = list[i]['bumped_at'];
|
|
||||||
var dateobject = new Date(datestring);
|
|
||||||
var now = new Date();
|
|
||||||
var diff = now - dateobject;
|
|
||||||
var minutes = Math.floor(diff / 60000);
|
|
||||||
var hours = Math.floor(minutes / 60);
|
|
||||||
var days = Math.floor(hours / 24);
|
|
||||||
if (days > 0) {
|
|
||||||
if (days == 1) {
|
|
||||||
date.innerText = " 1 day ago ";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
date.innerText = " " + days + " days ago ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (hours > 0){
|
|
||||||
if (hours == 1) {
|
|
||||||
date.innerText = " 1 hour ago ";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
date.innerText = " " + hours + " hours ago ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (minutes == 1) {
|
|
||||||
date.innerText = " 1 minute ago ";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
date.innerText = " " + minutes + " minutes ago ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
postinfo.appendChild(dateIcon);
|
|
||||||
postinfo.appendChild(date);
|
|
||||||
|
|
||||||
var likesicon = document.createElement('span');
|
|
||||||
likesicon.classList = "twemoji pg-red";
|
|
||||||
likesicon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m12 21.35-1.45-1.32C5.4 15.36 2 12.27 2 8.5 2 5.41 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.08C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.41 22 8.5c0 3.77-3.4 6.86-8.55 11.53L12 21.35Z"></path></svg>';
|
|
||||||
|
|
||||||
var likes = document.createElement('span');
|
|
||||||
likes.className = "discourse-likes";
|
|
||||||
likes.innerText = " " + list[i]['like_count'] + " ";
|
|
||||||
postinfo.appendChild(likesicon);
|
|
||||||
postinfo.appendChild(likes);
|
|
||||||
|
|
||||||
var replyIcon = document.createElement('span');
|
|
||||||
replyIcon.classList = "twemoji";
|
|
||||||
replyIcon.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M10 9V5l-7 7 7 7v-4.1c5 0 8.5 1.6 11 5.1-1-5-4-10-11-11Z"></path></svg>';
|
|
||||||
|
|
||||||
var replies = document.createElement('span');
|
|
||||||
replies.className = "discourse-replies";
|
|
||||||
|
|
||||||
var reply_count = list[i]['posts_count'] - 1;
|
|
||||||
if (reply_count == 1) {
|
|
||||||
replies.innerText = "1 Reply"
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
replies.innerText = " " + reply_count
|
|
||||||
}
|
|
||||||
postinfo.appendChild(replyIcon);
|
|
||||||
postinfo.appendChild(replies);
|
|
||||||
|
|
||||||
topic.appendChild(h3);
|
|
||||||
topic.appendChild(document.createElement('hr'));
|
|
||||||
topic.appendChild(authorinfo);
|
|
||||||
topic.appendChild(postinfo);
|
|
||||||
topics.appendChild(topic);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
@ -52,11 +52,10 @@
|
|||||||
{% if config.theme.language == "en" %}
|
{% if config.theme.language == "en" %}
|
||||||
<h2>Top discussions this week</h2>
|
<h2>Top discussions this week</h2>
|
||||||
<div class="grid cards">
|
<div class="grid cards">
|
||||||
<ul
|
<ul>
|
||||||
class="topics-list"
|
<!-- start top posts -->
|
||||||
data-forum="https://discuss.privacyguides.net"
|
|
||||||
data-feed="https://discuss.privacyguides.net/top.json?period=weekly"
|
<!-- end top posts -->
|
||||||
data-count="3">
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -103,11 +102,10 @@
|
|||||||
<div class="mdx-discourse-topics">
|
<div class="mdx-discourse-topics">
|
||||||
<h3>Latest discussions</h3>
|
<h3>Latest discussions</h3>
|
||||||
<div class="grid cards">
|
<div class="grid cards">
|
||||||
<ul
|
<ul>
|
||||||
class="topics-list"
|
<!-- start latest posts -->
|
||||||
data-forum="https://discuss.privacyguides.net"
|
|
||||||
data-feed="https://discuss.privacyguides.net/latest.json"
|
<!-- end latest posts -->
|
||||||
data-count="12">
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<noscript>
|
<noscript>
|
||||||
@ -120,7 +118,3 @@
|
|||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block scripts %}
|
|
||||||
<script src="{{ 'assets/javascripts/discourse-topics.js' | url }}"></script>
|
|
||||||
{{ super() }}
|
|
||||||
{% endblock %}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user