(query)
| 148 | * @returns {Array} Array of results |
| 149 | */ |
| 150 | export function search(query) { |
| 151 | const matchingResults = []; |
| 152 | let data = []; |
| 153 | Object.keys(INDEXS).forEach(key => { |
| 154 | data = data.concat(Object.keys(INDEXS[key]).map(page => INDEXS[key][page])); |
| 155 | }); |
| 156 | |
| 157 | query = query.trim(); |
| 158 | let keywords = query.split(/[\s\-,\\/]+/); |
| 159 | if (keywords.length !== 1) { |
| 160 | keywords = [].concat(query, keywords); |
| 161 | } |
| 162 | |
| 163 | for (let i = 0; i < data.length; i++) { |
| 164 | const post = data[i]; |
| 165 | let matchesScore = 0; |
| 166 | let resultStr = ''; |
| 167 | let handlePostTitle = ''; |
| 168 | let handlePostContent = ''; |
| 169 | const postTitle = post.title && post.title.trim(); |
| 170 | const postContent = post.body && post.body.trim(); |
| 171 | const postUrl = post.slug || ''; |
| 172 | |
| 173 | if (postTitle) { |
| 174 | keywords.forEach(keyword => { |
| 175 | // From https://github.com/sindresorhus/escape-string-regexp |
| 176 | const regEx = new RegExp( |
| 177 | escapeHtml(ignoreDiacriticalMarks(keyword)).replace( |
| 178 | /[|\\{}()[\]^$+*?.]/g, |
| 179 | '\\$&' |
| 180 | ), |
| 181 | 'gi' |
| 182 | ); |
| 183 | let indexTitle = -1; |
| 184 | let indexContent = -1; |
| 185 | handlePostTitle = postTitle |
| 186 | ? escapeHtml(ignoreDiacriticalMarks(postTitle)) |
| 187 | : postTitle; |
| 188 | handlePostContent = postContent |
| 189 | ? escapeHtml(ignoreDiacriticalMarks(postContent)) |
| 190 | : postContent; |
| 191 | |
| 192 | indexTitle = postTitle ? handlePostTitle.search(regEx) : -1; |
| 193 | indexContent = postContent ? handlePostContent.search(regEx) : -1; |
| 194 | |
| 195 | if (indexTitle >= 0 || indexContent >= 0) { |
| 196 | matchesScore += indexTitle >= 0 ? 3 : indexContent >= 0 ? 2 : 0; |
| 197 | if (indexContent < 0) { |
| 198 | indexContent = 0; |
| 199 | } |
| 200 | |
| 201 | let start = 0; |
| 202 | let end = 0; |
| 203 | |
| 204 | start = indexContent < 11 ? 0 : indexContent - 10; |
| 205 | end = start === 0 ? 70 : indexContent + keyword.length + 60; |
| 206 | |
| 207 | if (postContent && end > postContent.length) { |
no test coverage detected
searching dependent graphs…