Repository navigation
Expand file tree
/
Copy pathpost.html
More file actions
137 lines (115 loc) · 4.24 KB
/
Copy pathpost.html
File metadata and controls
137 lines (115 loc) · 4.24 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
<!DOCTYPE html>
<html>
<head>
<title>Post</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="blog.html" class="volver-btn">Volver al Blog</a>
<h1 id="post-title"></h1>
<div class="post-meta" id="post-meta"></div>
<div class="post-content" id="post-content"></div>
</div>
</main>
<footer-component></footer-component>
<script>
const API_KEY = 'AIzaSyBFBbH1SQkSZf1LJzammWAe2karh5mG9rQ';
const BLOG_ID = '2756493429384988662';
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="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 url = `https://www.googleapis.com/blogger/v3/blogs/${BLOG_ID}/posts/${postId}?key=${API_KEY}`;
const response = await fetch(url);
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;
// Actualizar el contenido
document.getElementById('post-title').textContent = post.title;
document.getElementById('post-meta').textContent = `Publicado el ${formatearFecha(post.published)}`;
// Procesar el contenido
let contenido = post.content;
// Mejorar la visualización de imágenes
contenido = contenido.replace(/<img/g, '<img class="post-image"');
document.getElementById('post-content').innerHTML = contenido;
// Aplicar estilos adicionales a las imágenes
const imagenes = document.querySelectorAll('.post-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';
});
// Mejorar la visualización de bloques de código
const bloquesCodigo = document.querySelectorAll('pre, 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);
if (slug !== slugEsperado) {
const nuevaURL = `post.html?slug=${slugEsperado}&id=${postId}`;
window.history.replaceState({}, post.title, nuevaURL);
}
} catch (error) {
console.error('Error al obtener el post:', error);
mostrarError('Error al cargar el post');
}
}
// Cargar el post
obtenerPost();
</script>
</body>
</html>