-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
215 lines (182 loc) · 5.58 KB
/
Copy pathsearch.js
File metadata and controls
215 lines (182 loc) · 5.58 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Clase para manejar el buscador personalizado
class Search {
constructor(formId, inputId, resultsId) {
this.form = document.getElementById(formId);
this.input = document.getElementById(inputId);
this.resultsContainer = document.getElementById(resultsId);
this.pages = [
{
title: "Sobre mí",
titleEn: "About Me",
url: "index.html",
content: "",
},
{
title: "Formación y Experiencia",
titleEn: "Education & Experience",
url: "formacion.html",
content: "",
},
{
title: "Intereses",
titleEn: "Interests",
url: "intereses.html",
content: "",
},
{
title: "Proyectos",
titleEn: "Projects",
url: "proyectos.html",
content: "",
},
];
this.contentLoaded = false;
if (this.form && this.input) {
this.init();
}
}
async init() {
this.form.addEventListener("submit", (e) => this.handleSubmit(e));
this.input.addEventListener("input", (e) => this.handleInput(e));
// Cargar contenido de las páginas
await this.loadPagesContent();
}
async loadPagesContent() {
try {
for (let page of this.pages) {
const response = await fetch(page.url);
const html = await response.text();
// Crear un DOM temporal para extraer el contenido
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
// Extraer texto del main (contenido principal)
const main = doc.querySelector("main");
if (main) {
// Remover scripts y estilos
main.querySelectorAll("script, style").forEach((el) => el.remove());
page.content = main.textContent.toLowerCase().trim();
}
}
this.contentLoaded = true;
} catch (error) {
console.error("Error cargando contenido de páginas:", error);
}
}
handleInput(e) {
const query = e.target.value.trim();
if (query.length < 2) {
this.hideResults();
return;
}
this.search(query);
}
handleSubmit(e) {
e.preventDefault();
const query = this.input.value.trim();
if (query.length >= 2) {
this.search(query);
}
}
search(query) {
if (!this.contentLoaded) {
this.showResults([
{
title: "Cargando...",
titleEn: "Loading...",
url: "#",
snippet: "",
},
]);
return;
}
const queryLower = query.toLowerCase();
const results = [];
const currentLocale = window.i18n ? window.i18n.currentLocale : "es";
for (let page of this.pages) {
const title = currentLocale === "en" ? page.titleEn : page.title;
// Buscar en título y contenido
if (
title.toLowerCase().includes(queryLower) ||
page.content.includes(queryLower)
) {
// Extraer snippet (contexto alrededor de la coincidencia)
const index = page.content.indexOf(queryLower);
let snippet = "";
if (index !== -1) {
const start = Math.max(0, index - 60);
const end = Math.min(
page.content.length,
index + queryLower.length + 60
);
snippet = "..." + page.content.substring(start, end).trim() + "...";
// Resaltar término de búsqueda
const regex = new RegExp(`(${queryLower})`, "gi");
snippet = snippet.replace(regex, "<strong>$1</strong>");
} else {
// Si coincide en el título pero no en el contenido
snippet = page.content.substring(0, 120) + "...";
}
results.push({
title: title,
url: page.url,
snippet: snippet,
});
}
}
if (results.length === 0) {
const noResultsText =
currentLocale === "en"
? "No results found"
: "No se encontraron resultados";
results.push({
title: noResultsText,
url: "#",
snippet: "",
});
}
this.showResults(results);
}
showResults(results) {
if (!this.resultsContainer) return;
this.resultsContainer.innerHTML = "";
this.resultsContainer.style.display = "block";
results.forEach((result) => {
const resultItem = document.createElement("div");
resultItem.className = "search-result-item";
const link = document.createElement("a");
link.href = result.url;
link.className = "search-result-title";
link.textContent = result.title;
if (result.url === "#") {
link.style.cursor = "default";
link.onclick = (e) => e.preventDefault();
}
resultItem.appendChild(link);
if (result.snippet) {
const snippet = document.createElement("p");
snippet.className = "search-result-snippet";
snippet.innerHTML = result.snippet;
resultItem.appendChild(snippet);
}
this.resultsContainer.appendChild(resultItem);
});
}
hideResults() {
if (this.resultsContainer) {
this.resultsContainer.style.display = "none";
this.resultsContainer.innerHTML = "";
}
}
}
// Inicializar el buscador cuando el DOM esté listo
document.addEventListener("DOMContentLoaded", function () {
new Search("search-form", "search-input", "search-results");
// Cerrar resultados al hacer clic fuera
document.addEventListener("click", function (e) {
const searchForm = document.getElementById("search-form");
const searchResults = document.getElementById("search-results");
if (searchResults && !searchForm.contains(e.target)) {
searchResults.style.display = "none";
}
});
});