learn.js 3.2 KB

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