-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
56 lines (48 loc) · 1.38 KB
/
search.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
---
title: Search
---
<body>
<div id="search-results">
</div>
</body>
<script>
var searchIndex = [
{% for post in site.html_pages %}
{
"title" : "{{ post.title | escape }}",
"category" : "{{ post.category }}",
"tags" : "{{ post.tags | join: ', ' }}",
"url" : "{{ site.baseurl }}{{ post.url }}",
"date" : "{{ post.date }}"
}{% unless forloop.last %},{% endunless %}
{% endfor %}
];
function search(query) {
var results = [];
for (var i = 0; i < searchIndex.length; i++) {
var post = searchIndex[i];
if (post.title.toLowerCase().indexOf(query.toLowerCase()) >= 0 ||
post.category.toLowerCase().indexOf(query.toLowerCase()) >= 0 ||
post.tags.toLowerCase().indexOf(query.toLowerCase()) >= 0) {
results.push(post);
}
}
return results;
}
// Get the URL parameters
var urlParams = new URLSearchParams(window.location.search);
// Get the value of the "attribute" parameter
var query = urlParams.get('query');
if (query === null) {
query = "";
}
// Use the attribute value as needed
console.log(query);
var results = search(query);
var html = '';
for (var i = 0; i < results.length; i++) {
var post = results[i];
html += '<li><a href="' + post.url + '">' + post.title + '</a></li>';
}
document.getElementById('search-results').innerHTML = html;
</script>