Welcome to mirror list, hosted at ThFree Co, Russian Federation.

search.js « js « assets - gitlab.com/rmaguiar/hugo-theme-color-your-world.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81ff8e80c485cabcdca49efb7c989444d1fb634e (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// =================================================
// Basic search functionality via Fuse.js
// Based on: https://gist.github.com/eddiewebb/735feb48f50f0ddd65ae5606a1cb41ae#gistcomment-2987774
// =================================================

'use strict';

const fuseOptions = {
  shouldSort: true,
  threshold: 0,
  ignoreLocation: true,
  maxPatternLength: {{ .Site.Params.Search.maxLength | default .Site.Data.default.search.maxLength }},
  minMatchCharLength: {{ .Site.Params.Search.minLength | default .Site.Data.default.search.minLength }},
  keys: [
    { name: 'title',        weight: .4 },
    { name: 'tags',         weight: .3 },
    { name: 'description',  weight: .2 },
    { name: 'content',      weight: .1 }
  ]
}

// Sanitize
function getUrlParameter(name) {
  name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
  const regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
  const results = regex.exec(location.search);
  return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

// Capture input
const searchQuery = getUrlParameter('q');

// Search info section
const searchInfo = document.querySelector('#search-info');

// Show message
function report(message, type) {
  const el = document.createElement('p');
  
  el.textContent = message;
  
  if (type) {
    el.classList.add(type);
  }
  
  searchInfo.appendChild(el);
}

if (searchQuery) {

  // Transfer text to search field
  document.querySelector('.search-box input')
    .value = searchQuery;
  
  executeSearch(searchQuery);
} else {
  report('{{ T "searchAwaitingSearch" }}');
}


function executeSearch(searchQuery) {
  fetch('index.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    
    // Limit results and throw an error if too many pages are found
    const limit = {{ .Site.Params.Search.maxResults | default 30 }};

    import('/libs/fuse.js@6.5.3/dist/fuse.basic.esm.min.js')
      .then((fuseBasic) => {
        const fuse = new fuseBasic.default(data, fuseOptions);
        return fuse.search(searchQuery);
      })
      .then((output) => {
        report('{{ T "searchResultsFor" }}: ' + searchQuery);

        const matches = output.length;
        
        if (matches > 0) {
          if (matches == 1) {
            report('{{ T "searchOnePageFound" }}.');
          } else if (1 < matches && matches < limit + 1) {
            report(matches + ' {{ T "searchPagesFound" }}.');
          } else {
            report('{{ T "searchTooMany" }}', 'error');
          }
        } else {
          report('{{ T "searchNoPageFound" }}', 'error');
        }
        
        if (0 < matches && matches < limit + 1) {
          populateResults(output);
        }
      });
  });
}


// Populate results
function populateResults(output) {
  output.forEach((value) => {
    
    const el = value.item;

    const postTitle = el.title;
    const postDate = el.date;
    
    const htmlPostTitle = document.createElement('p');
    htmlPostTitle.textContent = postTitle;
              
    // Pull HTML template
    const resultsTemplate = document.querySelector('#search-results-template')
      .content.cloneNode(true);
      
    const postLink = resultsTemplate.querySelector('.btn');
    
    // Replace values
    postLink.setAttribute('href', el.permalink);
    postLink.setAttribute('title', postTitle);

    if (postDate) {
      const htmlPostDate = document.createElement('time');
      htmlPostDate.setAttribute('datetime', postDate);
      htmlPostDate.textContent = postDate;

      const htmlPostWithDate = document.createDocumentFragment();
      htmlPostWithDate.appendChild(htmlPostTitle);
      htmlPostWithDate.appendChild(htmlPostDate);

      postLink.setAttribute('title', postTitle + ' (' + postDate + ')');
      postLink.appendChild(htmlPostWithDate);
    } else {
      postLink.setAttribute('title', postTitle);
      postLink.appendChild(htmlPostTitle);
    }

    document.querySelector('#search-results')
      .appendChild(resultsTemplate);
  });
}