learn.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. no-undef,
  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. define(['base', 'directives/mathjax', 'services/algolia-search'], function(
  18. App
  19. ) {
  20. App.controller('SearchWikiController', function(
  21. $scope,
  22. algoliaSearch,
  23. _,
  24. $modal
  25. ) {
  26. $scope.hits = []
  27. $scope.hits_total = 0
  28. $scope.config_hits_per_page = 20
  29. $scope.processingSearch = false
  30. $scope.clearSearchText = function() {
  31. $scope.searchQueryText = ''
  32. return updateHits([])
  33. }
  34. $scope.safeApply = function(fn) {
  35. const phase = $scope.$root.$$phase
  36. if (phase === '$apply' || phase === '$digest') {
  37. return $scope.$eval(fn)
  38. } else {
  39. return $scope.$apply(fn)
  40. }
  41. }
  42. const buildHitViewModel = function(hit) {
  43. const pagePath = hit.kb ? 'how-to/' : 'latex/'
  44. const pageSlug = encodeURIComponent(hit.pageName.replace(/\s/g, '_'))
  45. let section_underscored = ''
  46. if (hit.sectionName && hit.sectionName !== '') {
  47. section_underscored = '#' + hit.sectionName.replace(/\s/g, '_')
  48. }
  49. const section = hit._highlightResult.sectionName
  50. let pageName = hit._highlightResult.pageName.value
  51. if (section && section.value && section !== '') {
  52. pageName += ' - ' + section.value
  53. }
  54. let content = hit._highlightResult.content.value
  55. // Replace many new lines
  56. content = content.replace(/\n\n+/g, '\n\n')
  57. const lines = content.split('\n')
  58. // Only show the lines that have a highlighted match
  59. const matching_lines = []
  60. for (let line of Array.from(lines)) {
  61. if (!/^\[edit\]/.test(line)) {
  62. content += line + '\n'
  63. if (/<em>/.test(line)) {
  64. matching_lines.push(line)
  65. }
  66. }
  67. }
  68. content = matching_lines.join('\n...\n')
  69. const result = {
  70. name: pageName,
  71. url: `/learn/${pagePath}${pageSlug}${section_underscored}`,
  72. content
  73. }
  74. return result
  75. }
  76. var updateHits = (hits, hits_total = 0) => {
  77. $scope.safeApply(() => {
  78. $scope.hits = hits
  79. $scope.hits_total = hits_total
  80. })
  81. }
  82. $scope.search = function() {
  83. $scope.processingSearch = true
  84. const query = $scope.searchQueryText
  85. if (query == null || query.length === 0) {
  86. updateHits([])
  87. return
  88. }
  89. return algoliaSearch.searchWiki(
  90. query,
  91. {
  92. hitsPerPage: $scope.config_hits_per_page
  93. },
  94. function(err, response) {
  95. $scope.processingSearch = false
  96. if (response.hits.length === 0) {
  97. return updateHits([])
  98. } else {
  99. const hits = _.map(response.hits, buildHitViewModel)
  100. return updateHits(hits, response.nbHits)
  101. }
  102. }
  103. )
  104. }
  105. })
  106. return App.controller('LearnController', function() {})
  107. })