-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.js
66 lines (60 loc) · 2.11 KB
/
blog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// blog.js
document.addEventListener("DOMContentLoaded", () => {
fetch(
"https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@dxtaner"
)
.then((response) => response.json())
.then((data) => {
if (data.status === "ok") {
const items = data.items;
const itemsPerPage = 5;
let currentPage = 1;
const totalItems = items.length;
const totalPages = Math.ceil(totalItems / itemsPerPage);
const blogContainer = document.getElementById("blog-container");
const renderBlogPosts = (page) => {
const start = (page - 1) * itemsPerPage;
const end = start + itemsPerPage;
const blogHTML = items
.slice(start, end)
.map(
(item) => `
<div class="blog-post">
<h3>${item.title}</h3>
<p>${item.description.slice(0, 350)}...</p>
<p>Published Date: ${new Date(item.pubDate).toDateString()}</p>
<a href="${item.link}" target="_blank">Read more</a>
</div>
`
)
.join("");
blogContainer.innerHTML = blogHTML;
};
const pageContainer = document.getElementById("page-container");
const renderPagination = () => {
let pageNumbers = "";
for (let i = 1; i <= totalPages; i++) {
pageNumbers += `
<a class="page-number" href="#" data-page="${i}">${i}</a>
`;
}
pageContainer.innerHTML = pageNumbers;
const pageLinks = document.querySelectorAll(".page-number");
pageLinks.forEach((link) => {
link.addEventListener("click", (e) => {
e.preventDefault();
currentPage = parseInt(e.target.dataset.page);
renderBlogPosts(currentPage);
});
});
};
renderBlogPosts(currentPage);
renderPagination();
} else {
console.error("Hata: RSS verileri alınamadı.");
}
})
.catch((error) => {
console.error("Veri alınamadı:", error);
});
});