Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d98a004

Browse files
committed
Add lunr search
1 parent 850ace0 commit d98a004

File tree

10 files changed

+129
-76
lines changed

10 files changed

+129
-76
lines changed

_config.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ title: Archived 2.3 Magento Developer Guide
66
logo: Commerce DevDocs
77
description: Magento Developer Guide
88

9-
# the base protocol and domain
10-
url: http://magento.github.io/devdocs/
11-
129
# Site theme: https://github.com/commerce-docs/devdocs-theme
1310
# About themes in Jekyll: https://jekyllrb.com/docs/themes/
1411
theme: devdocs

src/_includes/layout/search-form.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
{%- endif -%}
66

77
<form class="spectrum-Search spectrum-Textfield search-form"
8-
action="{{ site.baseurl }}/guides/v{{ search_version }}/search.html">
9-
<input type="search" placeholder="Looking for something specific?" name="query" value=""
8+
action="{{ site.baseurl }}/guides/v{{ search_version }}/search.html" method="get">
9+
<input type="search" autocomplete="off" placeholder="Looking for something specific?" name="query" id="search-box" value=""
1010
class="spectrum-Textfield-input spectrum-Search-input search-field form-control" autocomplete="off">
1111
<svg class="spectrum-Icon spectrum-Icon--sizeM spectrum-Textfield-icon spectrum-Search-icon" focusable="false" aria-hidden="true">
1212
<rect fill="#ff13dc" opacity="0" width="18" height="18" />
@@ -22,3 +22,7 @@
2222
</div>
2323
</button>
2424
</form>
25+
<ul id="search-results" class="search-results"></ul>
26+
<script src="{{ site.baseurl }}/js/search-content.js"></script>
27+
<script src="{{ site.baseurl }}/js/lunr.js"></script>
28+
<script src="{{ site.baseurl }}/js/search.js"></script>

src/_includes/layout/search-lunr.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!-- Credits to https://thomascfoulds.com/2020/07/27/jekyll-static-site-search-lunr.html -->
2+
3+
<div class="search-results-header">
4+
<div class="search-results-header-inner">
5+
<div id="search-input">
6+
<div class="ais-SearchBox">
7+
<form class="ais-SearchBox-form spectrum-Search" action="search" method="get">
8+
<input type="search" autocomplete="off" autofocus="true" autocorrect="off" autocapitalize="off" maxlength="512" id="search-box" name="query" placeholder="Search the site" class="ais-SearchBox-input spectrum-Textfield spectrum-Textfield-input spectrum-Search-input" />
9+
<button class="ais-SearchBox-submit" type="submit" title="Submit the search query." hidden=""><svg class="ais-SearchBox-submitIcon" xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 40 40"> <path d="M26.804 29.01c-2.832 2.34-6.465 3.746-10.426 3.746C7.333 32.756 0 25.424 0 16.378 0 7.333 7.333 0 16.378 0c9.046 0 16.378 7.333 16.378 16.378 0 3.96-1.406 7.594-3.746 10.426l10.534 10.534c.607.607.61 1.59-.004 2.202-.61.61-1.597.61-2.202.004L26.804 29.01zm-10.426.627c7.323 0 13.26-5.936 13.26-13.26 0-7.32-5.937-13.257-13.26-13.257C9.056 3.12 3.12 9.056 3.12 16.378c0 7.323 5.936 13.26 13.258 13.26z"></path> </svg></button>
10+
<!-- <div class="input-group-btn">
11+
<button type="submit" class="spectrum-Button spectrum-Button--outline spectrum-Button--primary spectrum-Button--sizeS">
12+
<span class="spectrum-Button-label">Search</span>
13+
</button>
14+
</div> -->
15+
</form>
16+
</div>
17+
</div>
18+
</div>
19+
</div>
20+
<div class="search-results-container">
21+
<div id="hits">
22+
<div class="search-results" role="tabpanel" aria-labelledby="devdocs-tab" id="devdocs">
23+
<div class="search-results-main">
24+
<div class="ais-Hits">
25+
<div>
26+
<ol id="search-results" class="ais-Hits-list"></ul>
27+
</div>
28+
</div>
29+
</div>
30+
</div>
31+
</div>
32+
</div>
33+
34+
<script src="{{ site.baseurl }}/js/search-content.js"></script>
35+
<script src="{{ site.baseurl }}/js/lunr.js"></script>
36+
<script src="{{ site.baseurl }}/js/search.js"></script>
37+
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/search.css?ver=20" />

src/_includes/layout/site-nav.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525

2626
<div class="nav-actions">
2727
{% include layout/version-switcher.html %}
28-
29-
{% if page.guide_version != "2.4" %}
30-
{% include layout/quick-search.html %}
31-
{% endif %}
32-
3328
</div>
3429

3530
</div>

src/_includes/search-lunr.html

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/_layouts/search_lunr.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% include layout/header.html %}
2+
3+
{% include layout/search-lunr.html %}
4+
5+
{% include layout/footer.html %}

src/guides/v2.3/search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
layout: search
2+
layout: search_lunr
33
title: Search Results
44
guide_version: "2.3"
55
---

src/js/search-content.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
layout: null
3+
---
4+
5+
// Credits to https://thomascfoulds.com/2020/07/27/jekyll-static-site-search-lunr.html
6+
window.store = {
7+
{% assign searchable_pages = site.pages | where_exp: "page", "page.group != nil and page.layout != redirect" %}
8+
{% for page in searchable_pages %}
9+
"{{ page.url | slugify }}": {
10+
"title": "{{ page.title | xml_escape }}",
11+
"content": {{ page.excerpt | strip_html | jsonify }},
12+
"url": "{{ site.url }}{{ site.baseurl }}{{ page.url | xml_escape }}"
13+
}
14+
{% unless forloop.last %},{% endunless %}
15+
{% endfor %}
16+
}

src/js/search.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Credits for the code to https://thomascfoulds.com/2020/07/27/jekyll-static-site-search-lunr.html
2+
(function() {
3+
function displaySearchResults(results, store) {
4+
var searchResults = document.getElementById('search-results');
5+
6+
if (results.length) { // Are there any results?
7+
var appendString = '';
8+
9+
for (var i = 0; i < results.length; i++) { // Iterate over the results
10+
var item = store[results[i].ref];
11+
appendString += '<li class="ais-Hits-item"><div class="hit">'
12+
appendString += '<a href="' + item.url + '"><h2 class="hit-name">' + item.title + '</h2></a>'
13+
appendString += '<div class="hit-url">' + item.url + '</div>'
14+
appendString += '<div class="hit-content">' + item.content.substring(0, 150) + '</div></div></li>'
15+
// appendString += '<li class="ais-Hits-item"><a href="' + item.url + '"><h3>' + item.title + '</h3></a>';
16+
// appendString += '<p>' + item.content.substring(0, 150) + '...</p></li>';
17+
}
18+
19+
searchResults.innerHTML = appendString;
20+
} else {
21+
searchResults.innerHTML = '<li>No results found</li>';
22+
}
23+
}
24+
25+
function getQueryVariable(variable) {
26+
var query = window.location.search.substring(1);
27+
var vars = query.split('&');
28+
29+
for (var i = 0; i < vars.length; i++) {
30+
var pair = vars[i].split('=');
31+
32+
if (pair[0] === variable) {
33+
return decodeURIComponent(pair[1].replace(/\+/g, '%20'));
34+
}
35+
}
36+
}
37+
38+
var searchTerm = getQueryVariable('query');
39+
40+
if (searchTerm) {
41+
document.getElementById('search-box').setAttribute("value", searchTerm);
42+
43+
// Initalize lunr with the fields it will be searching on. I've given title
44+
// a boost of 10 to indicate matches on this field are more important.
45+
var idx = lunr(function () {
46+
this.field('id');
47+
this.field('title', { boost: 10 });
48+
this.field('content');
49+
50+
// Add data to lunr
51+
for (var key in window.store) {
52+
this.add({
53+
'id': key,
54+
'title': window.store[key].title,
55+
'content': window.store[key].content
56+
});
57+
}
58+
});
59+
60+
var results = idx.search(searchTerm); // Get lunr to perform a search
61+
displaySearchResults(results, window.store); // We'll write this in the next section
62+
}
63+
})();
64+

src/search.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)