Skip to content

Commit 69256b4

Browse files
committed
feat: search function
1 parent 52832f8 commit 69256b4

File tree

4 files changed

+67
-43
lines changed

4 files changed

+67
-43
lines changed

pelicanconf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@
4444
},
4545
"output_format": "html5",
4646
}
47+
48+
TEMPLATE_PAGES = {
49+
"search.json": "search.json",
50+
}

theme/templates/base.html

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,48 +40,57 @@
4040
});
4141
});
4242

43-
document.addEventListener("DOMContentLoaded", () => {
44-
const searchInput = document.getElementById("search-bar");
45-
const resultsContainer = document.createElement("div");
46-
resultsContainer.className = "search-results";
47-
searchInput.parentNode.appendChild(resultsContainer);
48-
49-
let articles = [];
50-
51-
// Load JSON
52-
fetch("{{ SITEURL }}/search.json")
53-
.then(response => response.json())
54-
.then(data => { articles = data; });
55-
56-
// Search logic
57-
searchInput.addEventListener("input", () => {
58-
const query = searchInput.value.toLowerCase().trim();
59-
resultsContainer.innerHTML = "";
60-
61-
if (!query) return;
62-
63-
const words = query.split(/\s+/);
64-
65-
// ✅ At least one word must match title, summary, or tags
66-
const matches = articles.filter(article =>
67-
words.some(word =>
68-
(article.title && article.title.toLowerCase().includes(word)) ||
69-
(article.summary && article.summary.toLowerCase().includes(word)) ||
70-
(article.tags && article.tags.join(" ").toLowerCase().includes(word))
71-
)
72-
);
73-
74-
if (matches.length === 0) {
75-
resultsContainer.innerHTML = "<div>No results found</div>";
76-
} else {
77-
matches.forEach(article => {
78-
const item = document.createElement("div");
79-
item.className = "search-result";
80-
item.innerHTML = `<a href="${article.url}">${article.title}</a>`;
81-
resultsContainer.appendChild(item);
82-
});
83-
}
84-
});
43+
document.addEventListener("DOMContentLoaded", () => {
44+
const searchInput = document.getElementById("search-bar");
45+
const resultsContainer = document.createElement("div");
46+
resultsContainer.className = "search-results";
47+
searchInput.parentNode.appendChild(resultsContainer);
48+
49+
let articles = [];
50+
51+
fetch("{{ SITEURL }}/search.json")
52+
.then(response => response.json())
53+
.then(data => { articles = data; });
54+
55+
searchInput.addEventListener("input", () => {
56+
57+
const query = searchInput.value.toLowerCase().trim();
58+
59+
if (!query) return;
60+
61+
const words = query.split(/\s+/);
62+
63+
// Almenos una coincidencia con title, summary, tags
64+
const matches = articles.filter(article =>
65+
words.some(word =>
66+
(article.title && article.title.toLowerCase().includes(word)) ||
67+
(article.summary && article.summary.toLowerCase().includes(word)) ||
68+
(article.tags && article.tags.join(" ").toLowerCase().includes(word))
69+
));
70+
71+
if (matches.length !== 0) {
72+
73+
const previewContainer = document.querySelector(".preview-container");
74+
previewContainer.innerHTML = "";
75+
76+
matches.forEach(article => {
77+
const li = document.createElement("li");
78+
li.className = "preview-post";
79+
li.innerHTML = `
80+
<a class="preview-link" href="${article.url}">
81+
<img class="preview-img" src="${article.image || '{{ SITEURL }}/img/otros/default.webp'}" alt="">
82+
<div class="preview-title">${article.title}</div>
83+
<div class="preview-summary">${article.summary || ""}</div>
84+
<div class="preview-author">${article.date || ""} ${article.author ? "Por " + article.author : ""}</div>
85+
<div class="preview-tags-container">
86+
${(article.tags || []).map(tag => `<div class="preview-tag">${tag}</div>`).join("")}
87+
</div>
88+
</a>
89+
`;
90+
previewContainer.appendChild(li);
91+
});
92+
}
93+
});
8594
});
8695
</script>
8796

theme/templates/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ <h1 class="welcome-msg-title">¡Hola Pythonista!</h1>
2121
<li class="preview-post">
2222
<a class="preview-link" href="{{ SITEURL }}/{{ article.url }}">
2323
<img class="preview-img" src="{{ SITEURL }}/{{ article.image }}" alt="">
24-
<br/>
2524
<div class="preview-title">{{ article.title }}</div>
2625
<div class="preview-summary">{{ article.summary }}</div>
2726
<div class="preview-author">{{ article.date.strftime('%Y-%m-%d') }} Por {{ article.author }}</div>

theme/templates/search.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{% for article in articles %}
3+
{
4+
"title": {{ article.title|tojson }},
5+
"date": "{{ article.date }}",
6+
"url": "{{ SITEURL }}/{{ article.url }}",
7+
"summary": {{ article.summary|striptags|tojson }},
8+
"image": "{{ SITEURL }}/{{ article.image }}",
9+
"tags": [{% for tag in article.tags %}{{ tag.name|tojson }}{% if not loop.last %}, {% endif %}{% endfor %}]
10+
}{% if not loop.last %},{% endif %}
11+
{% endfor %}
12+
]

0 commit comments

Comments
 (0)