2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-04-06 07:45:56 -04:00
|
|
|
define([
|
|
|
|
'../base',
|
|
|
|
'../directives/mathjax',
|
|
|
|
'../services/algolia-search'
|
|
|
|
], function(App) {
|
2018-11-05 05:06:39 -05:00
|
|
|
App.controller('SearchWikiController', function(
|
|
|
|
$scope,
|
|
|
|
algoliaSearch,
|
|
|
|
_,
|
|
|
|
$modal
|
|
|
|
) {
|
|
|
|
$scope.hits = []
|
2018-11-15 12:22:08 -05:00
|
|
|
$scope.hits_total = 0
|
|
|
|
$scope.config_hits_per_page = 20
|
2019-07-04 08:40:41 -04:00
|
|
|
$scope.processingSearch = false
|
2018-11-05 05:06:39 -05:00
|
|
|
|
|
|
|
$scope.clearSearchText = function() {
|
|
|
|
$scope.searchQueryText = ''
|
|
|
|
return updateHits([])
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.safeApply = function(fn) {
|
|
|
|
const phase = $scope.$root.$$phase
|
|
|
|
if (phase === '$apply' || phase === '$digest') {
|
|
|
|
return $scope.$eval(fn)
|
|
|
|
} else {
|
|
|
|
return $scope.$apply(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const buildHitViewModel = function(hit) {
|
2019-01-16 05:59:51 -05:00
|
|
|
const pagePath = hit.kb ? 'how-to/' : 'latex/'
|
2019-02-14 13:07:44 -05:00
|
|
|
const pageSlug = encodeURIComponent(hit.pageName.replace(/\s/g, '_'))
|
2019-01-16 05:59:51 -05:00
|
|
|
let section_underscored = ''
|
|
|
|
if (hit.sectionName && hit.sectionName !== '') {
|
|
|
|
section_underscored = '#' + hit.sectionName.replace(/\s/g, '_')
|
|
|
|
}
|
|
|
|
const section = hit._highlightResult.sectionName
|
|
|
|
let pageName = hit._highlightResult.pageName.value
|
|
|
|
if (section && section.value && section !== '') {
|
|
|
|
pageName += ' - ' + section.value
|
|
|
|
}
|
|
|
|
|
2018-11-05 05:06:39 -05:00
|
|
|
let content = hit._highlightResult.content.value
|
|
|
|
// Replace many new lines
|
|
|
|
content = content.replace(/\n\n+/g, '\n\n')
|
|
|
|
const lines = content.split('\n')
|
|
|
|
// Only show the lines that have a highlighted match
|
|
|
|
const matching_lines = []
|
|
|
|
for (let line of Array.from(lines)) {
|
|
|
|
if (!/^\[edit\]/.test(line)) {
|
|
|
|
content += line + '\n'
|
|
|
|
if (/<em>/.test(line)) {
|
|
|
|
matching_lines.push(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content = matching_lines.join('\n...\n')
|
|
|
|
const result = {
|
2019-01-16 05:59:51 -05:00
|
|
|
name: pageName,
|
|
|
|
url: `/learn/${pagePath}${pageSlug}${section_underscored}`,
|
2018-11-05 05:06:39 -05:00
|
|
|
content
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:22:08 -05:00
|
|
|
var updateHits = (hits, hits_total = 0) => {
|
|
|
|
$scope.safeApply(() => {
|
|
|
|
$scope.hits = hits
|
|
|
|
$scope.hits_total = hits_total
|
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2018-11-15 12:22:08 -05:00
|
|
|
$scope.search = function() {
|
2019-07-04 08:40:41 -04:00
|
|
|
$scope.processingSearch = true
|
2018-11-05 05:06:39 -05:00
|
|
|
const query = $scope.searchQueryText
|
|
|
|
if (query == null || query.length === 0) {
|
|
|
|
updateHits([])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:22:08 -05:00
|
|
|
return algoliaSearch.searchWiki(
|
|
|
|
query,
|
2020-01-10 09:26:27 -05:00
|
|
|
{
|
|
|
|
hitsPerPage: $scope.config_hits_per_page
|
|
|
|
},
|
2018-11-15 12:22:08 -05:00
|
|
|
function(err, response) {
|
2019-07-04 08:40:41 -04:00
|
|
|
$scope.processingSearch = false
|
2018-11-15 12:22:08 -05:00
|
|
|
if (response.hits.length === 0) {
|
|
|
|
return updateHits([])
|
|
|
|
} else {
|
|
|
|
const hits = _.map(response.hits, buildHitViewModel)
|
|
|
|
return updateHits(hits, response.nbHits)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2018-11-15 12:22:08 -05:00
|
|
|
)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return App.controller('LearnController', function() {})
|
|
|
|
})
|