2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../base'
|
|
|
|
import '../directives/mathjax'
|
|
|
|
import '../services/algolia-search'
|
2021-04-14 09:17:21 -04:00
|
|
|
App.controller(
|
|
|
|
'SearchWikiController',
|
|
|
|
function ($scope, algoliaSearch, $modal) {
|
|
|
|
$scope.hits = []
|
|
|
|
$scope.hits_total = 0
|
|
|
|
$scope.config_hits_per_page = 20
|
|
|
|
$scope.processingSearch = false
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.clearSearchText = function () {
|
|
|
|
$scope.searchQueryText = ''
|
2021-05-27 09:50:49 -04:00
|
|
|
updateHits([])
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.safeApply = function (fn) {
|
|
|
|
const phase = $scope.$root.$$phase
|
|
|
|
if (phase === '$apply' || phase === '$digest') {
|
2021-05-27 09:50:49 -04:00
|
|
|
$scope.$eval(fn)
|
2021-04-14 09:17:21 -04:00
|
|
|
} else {
|
2021-05-27 09:50:49 -04:00
|
|
|
$scope.$apply(fn)
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2019-01-16 05:59:51 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const buildHitViewModel = function (hit) {
|
|
|
|
const pagePath = hit.kb ? 'how-to/' : 'latex/'
|
|
|
|
const pageSlug = encodeURIComponent(hit.pageName.replace(/\s/g, '_'))
|
2021-05-27 09:50:49 -04:00
|
|
|
let sectionUnderscored = ''
|
2021-04-14 09:17:21 -04:00
|
|
|
if (hit.sectionName && hit.sectionName !== '') {
|
2021-05-27 09:50:49 -04:00
|
|
|
sectionUnderscored = '#' + hit.sectionName.replace(/\s/g, '_')
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
|
|
|
const section = hit._highlightResult.sectionName
|
|
|
|
let pageName = hit._highlightResult.pageName.value
|
|
|
|
if (section && section.value && section !== '') {
|
|
|
|
pageName += ' - ' + section.value
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-05-27 09:50:49 -04:00
|
|
|
const matchingLines = []
|
|
|
|
for (const line of lines) {
|
2021-04-14 09:17:21 -04:00
|
|
|
if (!/^\[edit\]/.test(line)) {
|
|
|
|
content += line + '\n'
|
|
|
|
if (/<em>/.test(line)) {
|
2021-05-27 09:50:49 -04:00
|
|
|
matchingLines.push(line)
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 09:50:49 -04:00
|
|
|
content = matchingLines.join('\n...\n')
|
2021-04-14 09:17:21 -04:00
|
|
|
const result = {
|
|
|
|
name: pageName,
|
2021-05-27 09:50:49 -04:00
|
|
|
url: `/learn/${pagePath}${pageSlug}${sectionUnderscored}`,
|
2021-04-27 03:52:58 -04:00
|
|
|
content,
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
|
|
|
return result
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-27 09:50:49 -04:00
|
|
|
var updateHits = (hits, hitsTotal = 0) => {
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.safeApply(() => {
|
|
|
|
$scope.hits = hits
|
2021-05-27 09:50:49 -04:00
|
|
|
$scope.hits_total = hitsTotal
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
2018-11-15 12:22:08 -05:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.search = function () {
|
|
|
|
$scope.processingSearch = true
|
|
|
|
const query = $scope.searchQueryText
|
2021-05-27 09:50:49 -04:00
|
|
|
if (!query || query.length === 0) {
|
2021-04-14 09:17:21 -04:00
|
|
|
updateHits([])
|
|
|
|
return
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
|
2021-05-27 09:50:49 -04:00
|
|
|
algoliaSearch.searchWiki(
|
2021-04-14 09:17:21 -04:00
|
|
|
query,
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
hitsPerPage: $scope.config_hits_per_page,
|
2021-04-14 09:17:21 -04:00
|
|
|
},
|
|
|
|
function (err, response) {
|
|
|
|
$scope.processingSearch = false
|
|
|
|
if (response.hits.length === 0) {
|
2021-05-27 09:50:49 -04:00
|
|
|
updateHits([])
|
2021-04-14 09:17:21 -04:00
|
|
|
} else {
|
|
|
|
const hits = _.map(response.hits, buildHitViewModel)
|
2021-05-27 09:50:49 -04:00
|
|
|
updateHits(hits, response.nbHits)
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
export default App.controller('LearnController', function () {})
|