-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.liquid
74 lines (67 loc) · 1.51 KB
/
index.liquid
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: main
---
<h1>Characters</h1>
<ul>
{% for character in collections.characters %}
<li><a href="{{ character.url }}">{{ character.data.title }}</a></li>
{% endfor %}
</ul>
<div id="app">
<input type="search" v-model="term"> <button @click="search">Search</button>
<div v-if="results">
<h3>Search Results</h3>
<ul>
<li v-for="result in results">
<a :href="result.url"> {% raw %}{{ result.title }}{% endraw %}</a>
</li>
</ul>
<p v-if="noResults">
Sorry, no results were found.
</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
docs:null,
idx:null,
term:'',
results:null
},
async created() {
let result = await fetch('/index.json');
docs = await result.json();
// assign an ID so it's easier to look up later, it will be the same as index
this.idx = lunr(function () {
this.ref('id');
this.field('title');
this.field('content');
docs.forEach(function (doc, idx) {
doc.id = idx;
this.add(doc);
}, this);
});
this.docs = docs;
},
computed: {
noResults() {
return this.results.length === 0;
}
},
methods:{
search() {
console.log('search', this.term);
let results = this.idx.search(this.term);
// we need to add title, url from ref
results.forEach(r => {
r.title = this.docs[r.ref].title;
r.url = this.docs[r.ref].url;
});
this.results = results;
}
}
});
</script>