-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.html
169 lines (161 loc) · 5.61 KB
/
post.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post</title>
<style>
/* Estilos para el header y la barra lateral */
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #181a1c;
padding: 10px;
}
header h1 {
font-size: 1.5em;
font-weight: bold;
color: white;
}
aside {
position: fixed;
top: 0;
left: 0;
width: 200px;
height: 100%;
background-color: #181a1c;
padding-top: 60px;
overflow-y: auto;
margin-right: 20px;
}
.index {
font-size: 16px;
margin-left: 20px;
}
.index .folder {
cursor: pointer;
}
.index ul {
display: none;
margin-left: 1px;
}
/* Estilos para reducir el margen izquierdo de cada elemento dentro del índice de cada mes */
.index ul li {
margin-left: 1px;
}
.index .folder.opened ul {
display: block;
}
#post-content {
margin-left: 220px;
padding-top: 60px;
}
/* Estilos para el fondo, color de letra y fuente */
body {
background-color: #181a1c;
color: white;
font-family: Monospace, sans-serif;
}
/* Estilos para subrayar texto */
em {
text-decoration: underline;
}
</style>
</head>
<body>
<header>
<h1>volteret4</h1>
</header>
<aside>
<h2><a href="index.html">volteret4</a></h2>
<div class="index" id="post-index"></div>
</aside>
<div id="post-content"></div>
<script>
// Función para agrupar los posts por mes y año
function createPostIndex(posts) {
const index = document.getElementById('post-index');
const groupedPosts = groupByMonth(posts);
Object.entries(groupedPosts).forEach(([monthYear, monthPosts]) => {
const folder = document.createElement('div');
folder.classList.add('folder');
// Obtener el nombre del mes completo y las dos últimas cifras del año a partir de la cadena "mes-año"
const dateParts = monthYear.split('-');
const monthName = new Date(dateParts[1], dateParts[0] - 1).toLocaleString('es', { month: 'long' });
const year = dateParts[1].slice(-2);
folder.textContent = `${monthName} ${year}`;
index.appendChild(folder);
const nestedUl = document.createElement('ul');
monthPosts.forEach((postKey, index) => {
const post = posts.blog[postKey];
const nestedLi = document.createElement('li');
const nestedA = document.createElement('a');
nestedA.href = `post.html?postTitle=${postKey}`;
nestedA.textContent = postKey;
nestedLi.appendChild(nestedA);
nestedUl.appendChild(nestedLi);
});
folder.appendChild(nestedUl);
// Agregar un controlador de eventos click para hacer que los elementos de la barra lateral sean interactivos
folder.addEventListener('click', () => {
folder.classList.toggle('opened');
});
});
}
function groupByMonth(posts) {
const blogPosts = posts.blog;
const groupedPosts = {};
for (const postKey in blogPosts) {
const post = blogPosts[postKey];
const dateParts = post.fecha_publicacion.split('-');
const monthYear = `${dateParts[1]}-${dateParts[0]}`;
if (!groupedPosts[monthYear]) {
groupedPosts[monthYear] = [];
}
groupedPosts[monthYear].push(postKey);
}
return groupedPosts;
}
// Función para cargar el contenido del post desde el archivo JSON
function loadPostContent(postName) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const posts = JSON.parse(xhr.responseText);
createPostIndex(posts);
const post = posts.blog[postName];
const title = postName;
const image = post.img;
const content = post.descripcion;
const date = new Date(post["fecha_publicacion"]).toLocaleDateString('es', { day: 'numeric', month: 'long', year: 'numeric'});
const postContent = document.getElementById('post-content');
const postTitle = document.createElement('h2');
postTitle.textContent = title;
postContent.appendChild(postTitle);
const postImage = document.createElement('img');
postImage.src = image;
postImage.style.width = '600px';
postImage.style.height = '600px';
postContent.appendChild(postImage);
const postDate = document.createElement('p');
postDate.textContent = date;
postContent.appendChild(postDate);
const postContentPara = document.createElement('p');
postContentPara.innerHTML = content;
postContent.appendChild(postContentPara);
}
};
xhr.open('GET', 'posts.json');
xhr.send();
}
// Obtener el índice del post de la URL
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const postTitle = urlParams.get('postTitle');
// Cargar el contenido del post correspondiente
loadPostContent(postTitle);
</script>
</body>
</html>