-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (49 loc) · 1.8 KB
/
script.js
File metadata and controls
53 lines (49 loc) · 1.8 KB
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
// this is for retrieving the blog data and nothing else!
function getBlogList() {
fetch('posts.json')
.then(res => res.json())
.then(posts => {
const container = document.getElementById('posts');
posts.forEach(post => {
const div = document.createElement('div');
div.className = 'post';
div.innerHTML = `
<hr size="1">
<h3><a href="post.html?id=${post.id}">${post.title}</a></h3>
<small>${post.date}</small>
<p>${post.description}</p>
`;
container.appendChild(div);
});
});
}
function getBlogData() {
const params = new URLSearchParams(location.search);
const id = params.get("id");
fetch("posts.json")
.then(r => r.json())
.then(posts => {
const post = posts.find(p => p.id == id);
const container = document.getElementById("post");
if (!post) {
container.innerHTML = "<h1>Post not found</h1>";
document.title = "cbladeOfficial - Blog Post";
return;
}
document.title = '"' + post.title + '" -- cbladeOfficial\'s Blog';
fetch("posts/" + id + ".md")
.then(r => r.text())
.then(md => {
const html = DOMPurify.sanitize(marked.parse(md));
container.innerHTML = `
<h1>${post.title}</h1>
<small>by cbladeOfficial, ${post.date}</small>
<hr size="1">
${html}
`;
});
})
.catch(() => {
document.getElementById("post").innerHTML = "<h3>Failed to load post</h3>";
});
}