learn.js 3.1 KB

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