learn.js 3.2 KB

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