]> Untitled Git - sneer-archive-site.git/blob - static/searchResults.js
On branch main
[sneer-archive-site.git] / static / searchResults.js
1 function el(element, config) {
2   const e = document.createElement(element);
3
4   if(config) {
5     if(config.children) {
6       for(const child of config.children.filter(a => a)) {
7         e.appendChild(child);
8       }
9
10       delete config.children;
11     }
12
13     for(const key in config) {
14       e[key] = config[key];
15     }
16   }
17
18   return e;
19 }
20
21 async function setup_lunr() {
22   const index_json = await (await fetch('/archives/lunr.json')).json();
23   const num_docs = index_json.length;
24   const search_index = elasticlunr(function() {
25     this.addField('title');
26     this.addField('author');
27     this.setRef('id');
28   });
29
30   for(const doc of index_json) {
31     search_index.addDoc(doc);
32   }
33
34   return [search_index, num_docs];
35 }
36
37 setup_lunr().then(([index, num_docs]) => {
38   const params = new URLSearchParams(window.location.search);
39   const query = params.get('query');
40   const submissionList = document.getElementById('searchResults');
41   const results = index.search(query, {});
42   submissionList.innerHTML = '';
43   submissionList.className = 'submissionList';
44   submissionList.appendChild(el('div', {
45     className: 'submissionItem',
46     children: [
47       el('span', {
48         textContent: `${results.length} results for "${query}" (out of ${num_docs} archived posts)`
49       })
50     ]
51   }));
52
53   for(const r of results) {
54     submissionList.appendChild(el('div', {
55       className: 'submissionItem',
56       children: [
57         el('div', {
58           className: 'score',
59           textContent: r.doc.score
60         }),
61         el('div', {
62           className: 'submissionContent',
63           children: [
64             el('div', {
65               className: 'title',
66               children: [
67                 el('a', {
68                   href: `/archives/thread/${r.doc.id}`,
69                   children: [
70                     r.doc.link_flair_text &&
71                       el('span', {
72                         className: 'flair',
73                         textContent: `${r.doc.link_flair_text} `
74                       }),
75                     el('span', {
76                       textContent: r.doc.title
77                     })
78                   ]
79                 }),
80                 r.doc.url && el('span', {
81                   className: 'url',
82                   children: [
83                     el('a', {
84                       href: r.doc.url,
85                       innerHTML: ` <span class="truncate">${r.doc.url}</span>`
86                     })
87                   ]
88                 })
89               ]
90             }),
91             el('div', {
92               className: 'subtitle',
93               children: [
94                 el('span', {
95                   textContent: `posted at ${r.doc.created_date} by `
96                 }),
97                 el('span', {
98                   className: 'author',
99                   textContent: `u/${r.doc.author}`
100                 })
101               ]
102             }),
103             el('div', {
104               className: 'subtitle',
105               children: [
106                 el('a', {
107                   href: `/archives/thread/${r.doc.id}`,
108                   textContent: `${r.doc.num_comments} comments`
109                 })
110               ]
111             })
112           ]
113         })
114       ]
115     }));
116   }
117 });