Skip to content

Commit 5bff972

Browse files
committed
#29 검색 기능 추가
@kciter 님께서 주신 자료를 가지고 붙여넣기만 했습니다 현재는 포스트 양이 적으니까 별 무리 없을 것 같아 수정은 안했습니다
1 parent 38e955d commit 5bff972

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

_layouts/search.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: default
3+
---
4+
5+
<div class="search">
6+
{{ content }}
7+
</div>
8+
9+
<script src="../js/vendors/lunr.js"></script>
10+
<script src="../js/search.js"></script>

_sass/_forms.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,18 @@ textarea {
104104
outline: none;
105105
box-shadow: 0 0 2px rgba($blue, .5);
106106
}
107+
.search-form {
108+
margin-bottom: 10px;
109+
}
110+
#search-box {
111+
box-sizing: border-box;
112+
width: 100%;
113+
border: none;
114+
border-bottom: 1px solid #000;
115+
outline: none;
116+
transition: border-color 0.3s ease-in-out, border-width 0.1s ease-in-out;
117+
}
118+
119+
#search-box:focus {
120+
border-bottom: 3px solid #41B883;
121+
}

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
layout: default
33
---
44
{% assign posts_count = (paginator.posts | size) %}
5+
<form class="search-form" action="/search" method="get">
6+
<label for="search_box"></label>
7+
<input type="text" id="search-box" name="query" placeholder="검색">
8+
</form>
59

610
<div class="home">
711
{% if posts_count > 0 %}

js/search.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(function() {
2+
function displaySearchResults(results, store) {
3+
var searchResults = document.getElementById('search-results');
4+
5+
if (results.length) { // Are there any results?
6+
var appendString = '';
7+
8+
for (var i = 0; i < results.length; i++) { // Iterate over the results
9+
var item = store[results[i].ref];
10+
appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>';
11+
appendString += '<p>' + item.content.substring(0, 150) + '...</p></li>';
12+
}
13+
14+
searchResults.innerHTML = appendString;
15+
} else {
16+
searchResults.innerHTML = '<li>검색어에 대한 내용이 없습니다. 다른 검색어로 찾아보시는 것은 어떨까요?</li>';
17+
}
18+
}
19+
20+
function getQueryVariable(variable) {
21+
var query = window.location.search.substring(1);
22+
var vars = query.split('&');
23+
24+
for (var i = 0; i < vars.length; i++) {
25+
var pair = vars[i].split('=');
26+
27+
if (pair[0] === variable) {
28+
return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
29+
}
30+
}
31+
}
32+
33+
var searchTerm = getQueryVariable('query');
34+
35+
if (searchTerm) {
36+
document.getElementById('search-box').setAttribute("value", searchTerm);
37+
38+
// Initalize lunr with the fields it will be searching on. I've given title
39+
// a boost of 10 to indicate matches on this field are more important.
40+
var idx = lunr(function () {
41+
this.field('id');
42+
this.field('title', { boost: 10 });
43+
this.field('author');
44+
this.field('category');
45+
this.field('content');
46+
});
47+
48+
for (var key in window.store) { // Add the data to lunr
49+
idx.add({
50+
'id': key,
51+
'title': window.store[key].title,
52+
'author': window.store[key].author,
53+
'category': window.store[key].category,
54+
'content': window.store[key].content
55+
});
56+
57+
var results = idx.search(searchTerm); // Get lunr to perform a search
58+
displaySearchResults(results, window.store); // We'll write this in the next section
59+
}
60+
}
61+
})();

js/vendors/lunr.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

search.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
layout: search
3+
---
4+
<form action="/search" method="get">
5+
<label for="search-box"></label>
6+
<input type="text" id="search-box" name="query">
7+
</form>
8+
9+
<ul id="search-results"></ul>
10+
11+
<script>
12+
window.store = {
13+
{% for post in site.posts %}
14+
"{{ post.url | slugify }}": {
15+
"title": "{{ post.title | xml_escape }}",
16+
"author": "{{ post.author | xml_escape }}",
17+
"category": "{{ post.category | xml_escape }}",
18+
"content": {{ post.content | strip_html | strip_newlines | jsonify }},
19+
"url": "{{ post.url | xml_escape }}"
20+
}
21+
{% unless forloop.last %},{% endunless %}
22+
{% endfor %}
23+
};
24+
</script>

0 commit comments

Comments
 (0)