learn.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. camelcase,
  4. handle-callback-err,
  5. max-len,
  6. no-return-assign,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. import App from '../base'
  18. import '../directives/mathjax'
  19. import '../services/algolia-search'
  20. App.controller('SearchWikiController', function($scope, algoliaSearch, $modal) {
  21. $scope.hits = []
  22. $scope.hits_total = 0
  23. $scope.config_hits_per_page = 20
  24. $scope.processingSearch = false
  25. $scope.clearSearchText = function() {
  26. $scope.searchQueryText = ''
  27. return updateHits([])
  28. }
  29. $scope.safeApply = function(fn) {
  30. const phase = $scope.$root.$$phase
  31. if (phase === '$apply' || phase === '$digest') {
  32. return $scope.$eval(fn)
  33. } else {
  34. return $scope.$apply(fn)
  35. }
  36. }
  37. const buildHitViewModel = function(hit) {
  38. const pagePath = hit.kb ? 'how-to/' : 'latex/'
  39. const pageSlug = encodeURIComponent(hit.pageName.replace(/\s/g, '_'))
  40. let section_underscored = ''
  41. if (hit.sectionName && hit.sectionName !== '') {
  42. section_underscored = '#' + hit.sectionName.replace(/\s/g, '_')
  43. }
  44. const section = hit._highlightResult.sectionName
  45. let pageName = hit._highlightResult.pageName.value
  46. if (section && section.value && section !== '') {
  47. pageName += ' - ' + section.value
  48. }
  49. let content = hit._highlightResult.content.value
  50. // Replace many new lines
  51. content = content.replace(/\n\n+/g, '\n\n')
  52. const lines = content.split('\n')
  53. // Only show the lines that have a highlighted match
  54. const matching_lines = []
  55. for (let line of Array.from(lines)) {
  56. if (!/^\[edit\]/.test(line)) {
  57. content += line + '\n'
  58. if (/<em>/.test(line)) {
  59. matching_lines.push(line)
  60. }
  61. }
  62. }
  63. content = matching_lines.join('\n...\n')
  64. const result = {
  65. name: pageName,
  66. url: `/learn/${pagePath}${pageSlug}${section_underscored}`,
  67. content
  68. }
  69. return result
  70. }
  71. var updateHits = (hits, hits_total = 0) => {
  72. $scope.safeApply(() => {
  73. $scope.hits = hits
  74. $scope.hits_total = hits_total
  75. })
  76. }
  77. $scope.search = function() {
  78. $scope.processingSearch = true
  79. const query = $scope.searchQueryText
  80. if (query == null || query.length === 0) {
  81. updateHits([])
  82. return
  83. }
  84. return algoliaSearch.searchWiki(
  85. query,
  86. {
  87. hitsPerPage: $scope.config_hits_per_page
  88. },
  89. function(err, response) {
  90. $scope.processingSearch = false
  91. if (response.hits.length === 0) {
  92. return updateHits([])
  93. } else {
  94. const hits = _.map(response.hits, buildHitViewModel)
  95. return updateHits(hits, response.nbHits)
  96. }
  97. }
  98. )
  99. }
  100. })
  101. export default App.controller('LearnController', function() {})