catcrafts.net/implementations/Catcrafts-Blog.cpp

83 lines
2.7 KiB
C++
Raw Normal View History

2025-11-12 20:58:07 +01:00
/*
catcrafts.net
Copyright (C) 2025 Catcrafts
The source code of this website is made available for viewing purposes only.
No permission is granted to copy, modify, distribute, or create derivative works.
*/
export module Catcrafts:Blog_impl;
import :Blog;
2025-11-12 21:56:18 +01:00
import :Root;
import :Views;
2025-11-12 20:58:07 +01:00
import std;
2025-11-12 21:56:18 +01:00
using namespace Crafter::CppDOMBindings;
export namespace Catcrafts {
std::vector<HtmlElementView>* blogButtons = new std::vector<HtmlElementView>();
void RenderBlog() {
delete blogButtons;
blogButtons = new std::vector<HtmlElementView>();
2025-11-12 20:58:07 +01:00
std::string html = "";
for(const BlogPost& post : *posts) {
2025-11-12 21:56:18 +01:00
// For preview, we'll limit the content to first 200 characters
std::string previewContent = post.content;
if(previewContent.length() > 200) {
// Find the last space before 200 characters to avoid cutting words
std::size_t lastSpace = previewContent.find_last_of(' ', 200);
if(lastSpace != std::string::npos) {
previewContent = previewContent.substr(0, lastSpace) + "...";
} else {
previewContent = previewContent.substr(0, 200) + "...";
}
}
2025-11-12 21:24:23 +01:00
html += std::format(R"(
2025-11-12 22:11:56 +01:00
<div class="post fade-in" id="blog-post-{}">
2025-11-12 21:24:23 +01:00
<div class="post-header">
2025-11-12 21:56:18 +01:00
<h2 class="post-title"><a>{}</a></h2>
2025-11-12 21:24:23 +01:00
<span class="post-date">{}</span>
</div>
<div class="post-content">
{}
</div>
2025-11-12 21:56:18 +01:00
<div class="post-footer">
2025-11-12 22:11:56 +01:00
<a class="btn">Read Full Post</a>
2025-11-12 21:56:18 +01:00
</div>
2025-11-12 22:11:56 +01:00
</div>)", post.slug, post.name, post.date, previewContent);
2025-11-12 20:58:07 +01:00
}
2025-11-12 21:56:18 +01:00
main->SetInnerHTML(html);
for(const BlogPost& post : *posts) {
2025-11-12 22:11:56 +01:00
// Add click listener to the entire post card
HtmlElementView& cardView = blogButtons->emplace_back(std::format("blog-post-{}", post.slug));
cardView.AddClickListener([slug = post.slug](Crafter::MouseEvent e) {
2025-11-12 21:56:18 +01:00
PushState("{}", "", std::format("/blog/{}", slug));
RenderRoot(std::format("/blog/{}", slug));
});
2025-11-12 22:11:56 +01:00
2025-11-12 21:56:18 +01:00
}
}
void RenderBlogPost(const std::string_view slug) {
2025-11-12 22:40:33 +01:00
std::cout << "render "<< std::endl;
2025-11-12 21:56:18 +01:00
for(const BlogPost& post : *posts) {
if(post.slug == slug) {
main->SetInnerHTML(std::format(R"(
2025-11-12 22:40:33 +01:00
<div class="blog-post-page">
2025-11-12 21:56:18 +01:00
<div class="post-header">
<h1 class="post-title">{}</h1>
<span class="post-date">{}</span>
</div>
<div class="post-content">
{}
</div>
</div>)", post.name, post.date, post.content));
return;
}
}
main->SetInnerHTML("<h1>Post Not Found</h1><p>The requested blog post could not be found.</p>");
2025-11-12 20:58:07 +01:00
}
2025-11-12 21:24:23 +01:00
}