a0667899e8ff12b2b9e3f5e2d96d48fd49cd2e16
[ndcode_site.git] / search / index.html.jst
1 let assert = require('assert')
2 let querystring = require('querystring')
3
4 return async env => {
5   let breadcrumbs = await _require('/_lib/breadcrumbs.jst')
6   let navbar = await _require('/_lib/navbar.jst')
7   let zet_site = await env.site.get_zettair('/_zet/site')
8
9   let query = env.parsed_url.query.query
10   let first = parseInt(env.parsed_url.query.first || '0')
11   let search = zet_site.search(query, first, 10)
12   console.log(
13     `${env.parsed_url.host} search "${query}" first ${first} results ${search.results.length} total results ${search.total_results}`
14   )
15
16   await navbar(
17     env,
18     async _out => {},
19     async _out => {
20       async function breadcrumbs_str(pathname) {
21         assert(pathname.slice(0, 1) === '/')
22
23         // find names of path components
24         console.log('pathname', pathname)
25         let components = ['Home']
26         for (let i = 1, j; (j = pathname.indexOf('/', i)) !== -1; i = j + 1) {
27           let menu
28           try {
29             menu = await env.site.get_menu(`${pathname.slice(0, i)}_menu.json`)
30           }
31           catch (error) {
32             return pathname // fallback
33           }
34           let dir = pathname.slice(i, j)
35           if (!Object.prototype.hasOwnProperty.call(menu.index, dir))
36             return pathname // fallback
37           components.push(menu.entries[menu.index[dir]].name)
38         }
39
40         return components.join(' > ')
41       }
42
43       await breadcrumbs(env, _out)
44
45       h4 {
46         'Query: '
47         strong {`${query}`}
48       }
49
50       if (search.results.length) {
51         p {`Showing results ${first + 1}–${first + search.results.length} of ${search.total_results}`}
52
53         ol(start=first + 1) {
54           for (let i = 0; i < search.results.length; ++i) {
55             let page = search.results[i].auxiliary
56             li {
57               a(href=page) {`${await breadcrumbs_str(page)}`}
58               br {}
59               p {_out.push(search.results[i].summary)} // note: contains HTML
60             }
61           }
62         }
63
64         ul.pagination {
65           for (let i = 0; i * 10 < search.total_results; ++i) {
66             let page = '/search/index.html?' + querystring.stringify(
67               {query: query, first: i * 10}
68             )
69             let text = (i + 1).toString()
70             if (i * 10 === first)
71               li.page-item.active {
72                 a.page-link(href=page) {
73                   `${text}`
74                   span.sr-only {'(current)'}
75                 }
76               }
77             else
78               li.page-item {
79                 a.page-link(href=page) {`${text}`}
80               }
81           }
82         }
83       }
84       else
85         p {'No results'}
86     },
87     async _out => {}
88   )
89 }