-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
search.html
74 lines (64 loc) · 2.32 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
layout: default
title: Search
---
<h2>Search: <span id="search-page-query"></span></h2>
<div id="search-page-results"></div>
<!-- html template -->
<template id="search-page-result-template">
<div class="search-page-result">
<h4>
<span class="search-page-result-type"></span>
<span class="search-page-result-title"></span>
</h4>
<p class="search-page-result-body"></p>
</div>
</template>
<script>
document.addEventListener("DOMContentLoaded", function() {
var searchQueryMatch = window.location.href.match(/\?query=(.*)/)
var searchQuery, searchResults
if (!searchQueryMatch) {
renderResults("(no query)", [])
} else {
searchQuery = decodeURIComponent(searchQueryMatch[1])
index.search({query: searchQuery, hitsPerPage: 1000}, function(err, content) {
renderResults(searchQuery, content.hits)
})
}
function renderResults(query, results) {
var querySpan = document.getElementById('search-page-query')
querySpan.innerText = '"' + decodeURIComponent(query) + '"'
var resultsContainer = document.getElementById("search-page-results")
var resultsTemplate = document.getElementById("search-page-result-template")
function writeToTemplate(header, body, resultType) {
resultsTemplate
.content
.querySelector(".search-page-result-title")
.innerHTML = header
resultsTemplate
.content
.querySelector(".search-page-result-body")
.innerHTML = body
var resultEl = document.importNode(resultsTemplate.content, true);
resultsContainer.appendChild(resultEl)
}
if (results.length == 0) {
writeToTemplate("No Results", "")
} else {
results.forEach(function(result) {
var resultPath
if (result.anchor) {
resultPath = result.url + "#" + result.anchor
} else {
resultPath = result.url
}
var resultHeader = [result.section || "API", result.title].concat(result.headings).join(" > ")
var header = "<a href='" + resultPath + "'>" + resultHeader + '</a>'
var resultBody = result.rubydoc_url ? "API Docs for " + result.title : result._highlightResult.html.value
writeToTemplate(header, resultBody)
})
}
}
})
</script>