Skip to content

Commit 615fa1e

Browse files
committed
Closes zutrinken#227, closes zutrinken#222; Update search to native implementation
1 parent b47a795 commit 615fa1e

File tree

9 files changed

+24
-487
lines changed

9 files changed

+24
-487
lines changed

Gruntfile.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module.exports = function(grunt) {
1414
'jsTargetDir': 'assets/js',
1515
'jsDependencies': [
1616
'<%= config.jsSrcDir %>/libs/jquery.min.js',
17-
'<%= config.jsSrcDir %>/libs/elasticlunr.min.js',
1817
'<%= config.jsSrcDir %>/libs/jquery.fitvids.js',
1918
'<%= config.jsSrcDir %>/libs/highlight.pack.js'
2019
]

assets/css/style.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/script.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

default.hbs

+1-25
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<div class="nav-wrapper-control">
7777
<div class="inner">
7878
<a class="nav-menu" role="button"><i class="icon icon-menu" aria-hidden="true"></i>{{t "Menu"}}</a>
79-
<a class="nav-search" style="display: none;" title="{{t "Search"}}" role="button"><i class="icon icon-search" aria-hidden="true"></i></a>
79+
<a class="nav-search" title="{{t "Search"}}" role="button" data-ghost-search><i class="icon icon-search" aria-hidden="true"></i></a>
8080
</div>
8181
</div>
8282
</div>
@@ -86,30 +86,6 @@
8686

8787
{{{body}}}
8888

89-
90-
<div class="search-wrapper">
91-
<div class="search">
92-
<form class="search-form">
93-
<input class="search-field" type="text" placeholder="{{t "Search"}}">
94-
<button class="search-button" type="submit">
95-
<i class="icon icon-search" aria-hidden="true"></i>
96-
</button>
97-
</form>
98-
<div class="popular-wrapper">
99-
<h4 class="popular-title">{{t "Topics"}}</h4>
100-
<span class="popular-tags post-tags">
101-
{{#get 'tags' limit='all' include='count.posts' order='count.posts desc'}}
102-
{{#foreach tags}}
103-
<a href='{{ url }}'>{{ name }}: {{ count.posts }}</a>
104-
{{/foreach}}
105-
{{/get}}
106-
</span>
107-
</div>
108-
<div class="search-result"></div>
109-
</div>
110-
<button class="search-wrapper-close" aria-label="{{t "Close"}}"></button>
111-
</div>
112-
11389
<div class="nav-footer">
11490
<nav class="nav-wrapper" aria-label="{{t "Footer"}}">
11591
<span class="nav-copy">{{@site.title}} &copy; {{date format='YYYY'}} <a class="nav-rss" title="RSS" href="{{@site.url}}/rss/" target="_blank"><i class="icon icon-rss" aria-hidden="true"></i></a></span>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"demo": "https://attila.peteramende.de",
55
"version": "3.4.1",
66
"engines": {
7-
"ghost": ">=4.0.0"
7+
"ghost": ">=5.0.0"
88
},
99
"license": "MIT",
1010
"screenshots": {

partials/navigation-meta.hbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
</a>
1616
</li>
1717
{{/if}}
18-
<li class="nav-search" style="display: none;">
19-
<a title="{{t "Search"}}">
18+
<li class="nav-search">
19+
<a title="{{t "Search"}}" data-ghost-search>
2020
<i class="icon icon-search" aria-hidden="true"></i>
2121
<span>{{t "Search"}}</span>
2222
</a>

src/js/libs/elasticlunr.min.js

-10
This file was deleted.

src/js/script.js

-173
Original file line numberDiff line numberDiff line change
@@ -38,179 +38,6 @@ jQuery(function($) {
3838
}
3939
});
4040

41-
/* ==========================================================================
42-
Search
43-
========================================================================== */
44-
45-
function search() {
46-
'use strict';
47-
if (
48-
typeof gh_search_key == 'undefined' ||
49-
gh_search_key == ''
50-
)
51-
return;
52-
53-
html.addClass('has-search');
54-
55-
var searchInput = $('.search-field');
56-
var searchButton = $('.search-button');
57-
var searchResult = $('.search-result');
58-
var popular = $('.popular-wrapper');
59-
var includeContent = typeof gh_search_content == 'undefined' || gh_search_content == true ? true : false;
60-
61-
var url =
62-
siteUrl +
63-
'/ghost/api/v3/content/posts/?key=' +
64-
gh_search_key +
65-
'&limit=all&fields=id,title,excerpt,url,updated_at,visibility&order=updated_at%20desc&formats=plaintext';
66-
var indexDump = JSON.parse(localStorage.getItem('ease_search_index'));
67-
var index;
68-
69-
elasticlunr.clearStopWords();
70-
71-
localStorage.removeItem('ease_index');
72-
localStorage.removeItem('ease_last');
73-
74-
function update(data) {
75-
data.posts.forEach(function (post) {
76-
index.addDoc(post);
77-
});
78-
79-
try {
80-
localStorage.setItem('ease_search_index', JSON.stringify(index));
81-
localStorage.setItem('ease_search_last', data.posts[0].updated_at);
82-
} catch (e) {
83-
console.error('Your browser local storage is full. Update your search settings following the instruction at https://github.com/TryGhost/Dawn#disable-content-search');
84-
}
85-
}
86-
87-
if (
88-
!indexDump
89-
) {
90-
$.get(url, function (data) {
91-
if (data.posts.length > 0) {
92-
index = elasticlunr(function () {
93-
this.addField('title');
94-
this.addField('plaintext');
95-
this.setRef('id');
96-
});
97-
98-
update(data);
99-
}
100-
});
101-
} else {
102-
index = elasticlunr.Index.load(indexDump);
103-
104-
$.get(
105-
url +
106-
"&filter=updated_at:>'" +
107-
localStorage
108-
.getItem('ease_search_last')
109-
.replace(/\..*/, '')
110-
.replace(/T/, ' ') +
111-
"'",
112-
function (data) {
113-
if (data.posts.length > 0) {
114-
update(data);
115-
}
116-
}
117-
);
118-
}
119-
120-
searchInput.on('keyup', function (e) {
121-
var result = index.search(e.target.value, { expand: true });
122-
var output = '';
123-
124-
result.forEach(function (post) {
125-
output +=
126-
'<div class="search-result-row">' +
127-
'<a class="search-result-row-link" href="' +
128-
post.doc.url +
129-
'">' +
130-
'<div class="search-result-row-title">' +
131-
post.doc.title +
132-
'</div><div class="search-result-row-excerpt">' +
133-
post.doc.excerpt +
134-
'</div></a>' +
135-
'</div>';
136-
console.log(post);
137-
});
138-
139-
searchResult.html(output);
140-
141-
if (e.target.value.length > 0) {
142-
searchButton.addClass('search-button-clear');
143-
} else {
144-
searchButton.removeClass('search-button-clear');
145-
}
146-
147-
if (result.length > 0) {
148-
popular.hide();
149-
} else {
150-
popular.show();
151-
}
152-
});
153-
154-
$('.search-form').on('submit', function (e) {
155-
e.preventDefault();
156-
});
157-
158-
searchButton.on('click', function () {
159-
if ($(this).hasClass('search-button-clear')) {
160-
searchInput.val('').focus().keyup();
161-
}
162-
});
163-
}
164-
search();
165-
166-
/* ==========================================================================
167-
Search Overlay
168-
========================================================================== */
169-
170-
function modal() {
171-
'use strict';
172-
var modalOverlay = $('.search-wrapper');
173-
var modal = $('.search');
174-
var modalInput = $('.search-field');
175-
176-
$('.nav-search').on('click', function (e) {
177-
e.preventDefault();
178-
modalOverlay.show().outerWidth();
179-
html.addClass('search-active');
180-
modalInput.focus();
181-
182-
if(html.hasClass('menu-active')) {
183-
html.removeClass('menu-active');
184-
}
185-
});
186-
187-
$('.search-wrapper-close, .search-wrapper').on('click', function () {
188-
html.removeClass('search-active');
189-
});
190-
191-
modal.on('click', function (e) {
192-
e.stopPropagation();
193-
});
194-
195-
$(document).keyup(function (e) {
196-
if (e.keyCode === 27 && html.hasClass('search-active')) {
197-
html.removeClass('search-active');
198-
modalInput.val('').keyup();
199-
}
200-
});
201-
202-
modalOverlay.on('transitionend', function (e) {
203-
if (!html.hasClass('search-active')) {
204-
modalOverlay.hide();
205-
}
206-
});
207-
208-
modal.on('transitionend', function (e) {
209-
e.stopPropagation();
210-
});
211-
}
212-
modal();
213-
21441
/* ==========================================================================
21542
Parallax cover
21643
========================================================================== */

0 commit comments

Comments
 (0)