PageSourceSearch

https://opencensus.io/js/search.js

js opencensus.io collected 2026-10-01 10:28:26 UTC 3,084 bytes, 91 lines download raw bytes

1var lunrIndex, pagesIndex;
2
3function endsWith(str, suffix) {
4    return str.indexOf(suffix, str.length - suffix.length) !== -1;
5}
6
7// Initialize lunrjs using our generated index file
8function initLunr() {
9    if (!endsWith(baseurl,"/")){
10        baseurl = baseurl+'/'
11    };
12
13    // First retrieve the index file
14    $.getJSON(baseurl +"index.json")
15        .done(function(index) {
16            pagesIndex =   index;
17            // Set up lunrjs by declaring the fields we use
18            // Also provide their boost level for the ranking
19            lunrIndex = new lunr.Index
20            lunrIndex.ref("uri");
21            lunrIndex.field('title', {
22                boost: 15
23            });
24            lunrIndex.field('tags', {
25                boost: 10
26            });
27            lunrIndex.field("content", {
28                boost: 5
29            });
30
31            // Feed lunr with each file and let lunr actually index them
32            pagesIndex.forEach(function(page) {
33                lunrIndex.add(page);
34            });
35            lunrIndex.pipeline.remove(lunrIndex.stemmer)
36        })
37        .fail(function(jqxhr, textStatus, error) {
38            var err = textStatus + ", " + error;
39            console.error("Error getting Hugo index file:", err);
40        });
41}
42
43/**
44 * Trigger a search in lunr and transform the result
45 *
46 * @param  {String} query
47 * @return {Array}  results
48 */
49function search(query) {
50    // Find the item in our index corresponding to the lunr one to have more info
51    return lunrIndex.search(query).map(function(result) {
52            return pagesIndex.filter(function(page) {
53                return page.uri === result.ref;
54            })[0];
55        });
56}
57
58// Let's get started
59initLunr();
60$( document ).ready(function() {
61    var searchList = new autoComplete({
62        /* selector for the search box element */
63        selector: $("#search-by").get(0),
64        /* source is the callback to perform the search */
65        source: function(term, response) {
66            response(search(term));
67        },
68        /* renderItem displays individual search results */
69        renderItem: function(item, term) {
70            var numContextWords = 2;
71            var text = item.content.match(
72                "(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}" +
73                    term+"(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}");
74            item.context = text;
75            return '<div class="autocomplete-suggestion" ' +
76                'data-term="' + term + '" ' +
77                'data-title="' + item.title + '" ' +
78                'data-uri="'+ item.uri + '" ' +
79                'data-context="' + item.context + '">' +
80                '» ' + item.title +
81                '<div class="context">' +
82                (item.context || '') +'</div>' +
83                '</div>';
84        },
85        /* onSelect callback fires when a search suggestion is chosen */
86        onSelect: function(e, term, item) {
87            console.log(item.getAttribute('data-val'));
88            location.href = item.getAttribute('data-uri');
89        }
90    });
91});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.