Repository navigation
Expand file tree
/
Copy pathwp-post.html
More file actions
164 lines (140 loc) · 5.38 KB
/
Copy pathwp-post.html
File metadata and controls
164 lines (140 loc) · 5.38 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
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
<!DOCTYPE html>
<html>
<head>
<title>Post WordPress</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500;700&family=Poppins:wght@400;500&display=swap" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="componentes/components.js"></script>
</head>
<body>
<header-component></header-component>
<main class="main-content">
<div class="post">
<a href="wp-blog.html" class="volver-btn">Volver al Blog</a>
<h1 id="post-title"></h1>
<div class="post-meta" id="post-meta"></div>
<div id="post-categories" class="etiquetas"></div>
<div class="post-content" id="post-content"></div>
</div>
</main>
<footer-component></footer-component>
<script>
const WP_URL = 'https://dev-webgae.pantheonsite.io/wp-json/wp/v2';
function obtenerParametroURL(nombre) {
const params = new URLSearchParams(window.location.search);
return params.get(nombre);
}
function formatearFecha(fecha) {
return new Date(fecha).toLocaleDateString('es-ES', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
}
function crearSlug(texto) {
return texto
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '')
.substring(0, 100);
}
function mostrarError(mensaje) {
document.querySelector('main').innerHTML = `
<div class="error-container">
<h1>${mensaje}</h1>
<p>No se pudo cargar el contenido del post.</p>
<a href="wp-blog.html" class="volver-btn">Volver al Blog</a>
</div>
`;
}
async function obtenerPost() {
const postId = obtenerParametroURL('id');
const slug = obtenerParametroURL('slug');
if (!postId) {
mostrarError('Post no encontrado');
return;
}
try {
const response = await fetch(`${WP_URL}/posts/${postId}?_embed`);
if (!response.ok) {
throw new Error('Post no encontrado');
}
const post = await response.json();
// Actualizar el título de la página
document.title = post.title.rendered;
// Actualizar el contenido
document.getElementById('post-title').innerHTML = post.title.rendered;
document.getElementById('post-meta').textContent = `Publicado el ${formatearFecha(post.date)}`;
// Mostrar categorías si existen
if (post._embedded && post._embedded['wp:term']) {
const categorias = post._embedded['wp:term'][0] || [];
const categoriasHTML = categorias.map(cat =>
`<span class="etiqueta">${cat.name}</span>`
).join('');
document.getElementById('post-categories').innerHTML = categoriasHTML;
}
// Procesar el contenido
let contenido = post.content.rendered;
// Si hay imagen destacada, mostrarla al principio del contenido
if (post._embedded && post._embedded['wp:featuredmedia']) {
const imagenDestacada = post._embedded['wp:featuredmedia'][0];
const imagenHTML = `
<img
src="${imagenDestacada.source_url}"
alt="${imagenDestacada.alt_text || post.title.rendered}"
class="featured-image"
>
`;
contenido = imagenHTML + contenido;
}
document.getElementById('post-content').innerHTML = contenido;
// Aplicar estilos a las imágenes
const imagenes = document.querySelectorAll('.post-content img, .featured-image');
imagenes.forEach(img => {
img.style.maxWidth = '100%';
img.style.height = 'auto';
img.style.display = 'block';
img.style.margin = '20px auto';
img.style.borderRadius = '8px';
});
// Aplicar estilos a los iframes (videos embebidos)
const iframes = document.querySelectorAll('.post-content iframe');
iframes.forEach(iframe => {
iframe.style.maxWidth = '100%';
iframe.style.margin = '20px auto';
iframe.style.display = 'block';
iframe.style.borderRadius = '8px';
});
// Mejorar la visualización de bloques de código
const bloquesCodigo = document.querySelectorAll('.post-content pre, .post-content code');
bloquesCodigo.forEach(bloque => {
bloque.style.backgroundColor = '#f5f5f5';
bloque.style.padding = '15px';
bloque.style.borderRadius = '5px';
bloque.style.overflow = 'auto';
bloque.style.fontSize = '14px';
bloque.style.lineHeight = '1.5';
bloque.style.fontFamily = 'Consolas, Monaco, monospace';
});
// Actualizar URL si es necesario
const slugEsperado = crearSlug(post.title.rendered);
if (slug !== slugEsperado) {
const nuevaURL = `wp-post.html?slug=${slugEsperado}&id=${postId}`;
window.history.replaceState({}, post.title.rendered, nuevaURL);
}
} catch (error) {
console.error('Error al obtener el post:', error);
mostrarError('Error al cargar el post');
}
}
// Cargar el post
obtenerPost();
</script>
</body>
</html>