-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.njk
88 lines (86 loc) · 2.06 KB
/
index.njk
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
layout: layout.njk
---
<div id="app">
<input type="search" placeholder="Suchen …" v-model="term" v-on:input="search">
<div class="articlelist" v-if="results">
<ul>
<li v-for="result in results">
<a :href="result.url"> {% raw %} <span class="number">#{{result.articleId}}</span> {{ result.title }}{% endraw %}</a>
</li>
</ul>
</div>
<div class="articlelist" v-else>
<ul id="index">
{% for article in collections.articles %}
<li class="indexList"><a href="{{ article.url }}"><span class="number">#{{ article.data.id }}</span> {{ article.data.title }}</a></li>
{% endfor %}
</ul>
</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();
this.idx = lunr(function () {
this.ref('id');
this.field('title');
this.field('content');
this.field('articleId');
docs.forEach(function (doc, idx) {
doc.id = idx;
this.add(doc);
}, this);
});
this.docs = docs;
},
computed: {
noResults() {
// Show all the articles
return this.results.length === 0;
}
},
methods:{
isInt(value) {
var x;
if (isNaN(value)) {
return false;
}
x = parseFloat(value);
return (x | 0) === x;
},
getResults(term) {
if (this.isInt(term) == true) {
console.log('int');
return this.idx.search(term, {field: 'id'});
} else {
console.log('string');
return this.idx.query(function(q) {
q.term(term, {editDistance: 5, presence: 2})
});
}
},
search() {
results = this.getResults(this.term);
console.log('search', results);
// we need to add title, url, articleID from ref
results.forEach(r => {
r.title = this.docs[r.ref].title;
r.url = this.docs[r.ref].url;
r.articleId = this.docs[r.ref].articleId;
});
this.results = results;
}
}
});
</script>