learn.js 3.2 KB

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