Skip to content

Commit

Permalink
Merge pull request #48 from christoph3r3w/40-time-and-date-parser-for…
Browse files Browse the repository at this point in the history
…-all-routes-jesse

JK - (re)Made time & date parser
  • Loading branch information
christoph3r3w authored May 25, 2024
2 parents 404f1fe + 37cefc5 commit 1fbd807
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
62 changes: 40 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,42 @@ const categoriesUrl = `${apiUrl}categories?per_page=100`

// functions//

// date parser
const datePars = function(postData,categoryData){ // ask how to implement this function and fix the post data
for(var i = 0; i < postData.length;i++){
const parsedDate = new Date(postData[i].date),
day = parsedDate.getDate(),
options = { month: "short" }, // De maand moet kort geschreven zijn
month = Intl.DateTimeFormat("nl-NL", options).format(parsedDate.getMonth() + 1),
newDate = day + ''+ month;
postData[i].date = newDate;
categoryData[i].date = newDate;
postData[i].author = userData.find(user => user.id === postData[i].author);
console.log('date parser works');
return(postData)
}}


const formatPostMeta = (postData) => {
const options = { day: 'numeric', month: 'long' };
const formattedDate = new Date(post.date_gmt).toLocaleDateString('nl-NL', options);
const estimatedReadingTime = post.yoast_head_json.twitter_misc["Geschatte leestijd"].replace("minuten", "min");
return `${formattedDate} - ${post.yoast_head_json.author} - ${estimatedReadingTime}`;
};
// metaParse
function metaParse(postsData) {
postsData.forEach(postData => {
const postDate = new Date(postData.date_gmt);

const dateOptions = { day: 'numeric', month: 'long' };
const timeOptions = { hour: '2-digit', minute: '2-digit' };

const formattedDate = postDate.toLocaleDateString('nl-NL', dateOptions);
const formattedTime = postDate.toLocaleTimeString('nl-NL', timeOptions);

const author = postData.yoast_head_json?.twitter_misc?.["Geschreven door"] ?? "N/A";

const readingTime = postData.yoast_head_json?.twitter_misc?.["Geschatte leestijd"] ?? "N/A";
const estimatedReadingTime = readingTime.replace("minuten", "min");

postData.meta = `${formattedDate} ${formattedTime} - ${author} - ${estimatedReadingTime}`;
});

return postsData;
}

// dateParse
function dateParse(postData) {
const postDate = new Date(postData.date_gmt);

const dateOptions = { day: 'numeric', month: 'long' };
const timeOptions = { hour: '2-digit', minute: '2-digit' };

const formattedDate = postDate.toLocaleDateString('nl-NL', dateOptions);
const formattedTime = postDate.toLocaleTimeString('nl-NL', timeOptions);

postData.date_gmt = `${formattedDate} ${formattedTime}`;

return postData;
}

// page views

Expand Down Expand Up @@ -151,6 +165,8 @@ app.get("/",function(req,res){
// categoryData = datePars(categoryData);
// console.log("postData");

postData = metaParse(postData);

res.render("index.ejs", {
posts : postData,
user : userData,
Expand Down Expand Up @@ -185,6 +201,8 @@ app.get("/detail/:id",function(req,res){
// postData = datePars(postData);
// formatPostMeta(postData);
// console.log(formatPostMeta);

postData = dateParse(postData)

// page views detection // can be used to show if a page has already been visited
// views(postData);
Expand Down
8 changes: 1 addition & 7 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@
<a href="/detail/<%-post.id%>">
<img class="post-image" src="<%= post.jetpack_featured_media_url %>" alt="">
<h2 class="post-title"><%- post.title.rendered %></h2>
<% <!-- THIS CAN BE IMPROVED BY DOING THIS ALL INSIDE THE SERVER.JS ALREADY -->
const options = { day: 'numeric', month: 'long' };
const formattedDate = new Date(post.date_gmt).toLocaleDateString('nl-NL', options);
const readingTime = post.yoast_head_json?.twitter_misc?.["Geschatte leestijd"] ?? "N/A"; <!--Had to add this because reading time doesnt exist in some posts -->
const estimatedReadingTime = readingTime.replace("minuten", "min");
%>
<span class="post-meta"><%= formattedDate %> - <%= post.yoast_head_json.author %> - <%= estimatedReadingTime %></span>
<span class="post-meta"><%= post.meta %></span>
</a>
</li>
<% }) %>
Expand Down

0 comments on commit 1fbd807

Please sign in to comment.