learn.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import _ from 'lodash'
  2. import App from '../base'
  3. import '../directives/mathjax'
  4. import '../services/algolia-search'
  5. App.controller('SearchWikiController', [
  6. '$scope',
  7. 'algoliaSearch',
  8. function ($scope, algoliaSearch) {
  9. $scope.hits = []
  10. $scope.hits_total = 0
  11. $scope.config_hits_per_page = 20
  12. $scope.processingSearch = false
  13. $scope.clearSearchText = function () {
  14. $scope.searchQueryText = ''
  15. updateHits([])
  16. }
  17. $scope.safeApply = function (fn) {
  18. const phase = $scope.$root.$$phase
  19. if (phase === '$apply' || phase === '$digest') {
  20. $scope.$eval(fn)
  21. } else {
  22. $scope.$apply(fn)
  23. }
  24. }
  25. const buildHitViewModel = function (hit) {
  26. const pagePath = hit.kb ? 'how-to/' : 'latex/'
  27. const pageSlug = encodeURIComponent(hit.pageName.replace(/\s/g, '_'))
  28. let sectionUnderscored = ''
  29. if (hit.sectionName && hit.sectionName !== '') {
  30. sectionUnderscored = '#' + hit.sectionName.replace(/\s/g, '_')
  31. }
  32. const section = hit._highlightResult.sectionName
  33. let pageName = hit._highlightResult.pageName.value
  34. if (section && section.value && section !== '') {
  35. pageName += ' - ' + section.value
  36. }
  37. let content = hit._highlightResult.content.value
  38. // Replace many new lines
  39. content = content.replace(/\n\n+/g, '\n\n')
  40. const lines = content.split('\n')
  41. // Only show the lines that have a highlighted match
  42. const matchingLines = []
  43. for (const line of lines) {
  44. if (!/^\[edit\]/.test(line)) {
  45. content += line + '\n'
  46. if (/<em>/.test(line)) {
  47. matchingLines.push(line)
  48. }
  49. }
  50. }
  51. content = matchingLines.join('\n...\n')
  52. const result = {
  53. name: pageName,
  54. url: `/learn/${pagePath}${pageSlug}${sectionUnderscored}`,
  55. content,
  56. }
  57. return result
  58. }
  59. const updateHits = (hits, hitsTotal = 0) => {
  60. $scope.safeApply(() => {
  61. $scope.hits = hits
  62. $scope.hits_total = hitsTotal
  63. })
  64. }
  65. $scope.search = function () {
  66. $scope.processingSearch = true
  67. const query = $scope.searchQueryText
  68. if (!query || query.length === 0) {
  69. updateHits([])
  70. return
  71. }
  72. algoliaSearch.searchWiki(
  73. query,
  74. {
  75. hitsPerPage: $scope.config_hits_per_page,
  76. },
  77. function (err, response) {
  78. if (err) {
  79. $scope.searchError = err
  80. }
  81. $scope.processingSearch = false
  82. if (response.hits.length === 0) {
  83. updateHits([])
  84. } else {
  85. const hits = _.map(response.hits, buildHitViewModel)
  86. updateHits(hits, response.nbHits)
  87. }
  88. }
  89. )
  90. }
  91. },
  92. ])
  93. export default App.controller('LearnController', function () {})