git.iko.soy/rowtype-yoga/purescript-docs-search
on master help sign in
added: SearchResult instead of IndexEntry
commit7bcebede7a90cfea1e595fb315e83fcd605724a6 authorklntsky <klntsky@gmail.com> dateMon 24 Jun 2019, 13:07 (7 years ago) parent5c1d481
2 files changed+39−15
src/Spago/Search/App/SearchResults.purs+3−2 src/Spago/Search/Index.purs+36−13
src/Spago/Search/App/SearchResults.purs+3−2
@@ -26,7 +26,7 @@ import Halogen.HTML.Properties as HP
2626
2727type State = { shown :: Boolean
2828 , mbIndex :: Maybe SearchIndex
29− , results :: Array IndexEntry
29+ , results :: Array SearchResult
3030 , input :: String
3131 }
3232
@@ -97,7 +97,8 @@ render state
9797 show (Array.length state.results) <>
9898 " definitions" ] <>
9999
100− [ HH.div_ $ state.results <#> \result -> HH.div_ [ HH.text (unwrap result).title ]
100+ [ HH.div_ $ state.results <#> \result ->
101+ HH.div_ [ HH.text (unwrap result).name ]
101102 ]
102103
103104renderSummary :: forall a b. String -> HH.HTML b a
src/Spago/Search/Index.purs+36−13
@@ -5,17 +5,23 @@ import Prelude
55import Spago.Search.Declarations (Declarations(..), IndexEntry(..))
66
77import Data.Foldable (foldr)
8+import Data.List (List)
89import Data.List as List
9−import Data.List ((:))
10+import Data.Maybe (Maybe(..))
1011import Data.Newtype (class Newtype)
1112import Data.Search.Trie (Trie, alter)
1213import Data.String.CodeUnits (toCharArray)
1314import Data.String.Common (toLower)
14−import Data.Maybe
1515
16−newtype SearchIndex = SearchIndex (Trie Char (List.List IndexEntry))
16+newtype SearchIndex = SearchIndex (Trie Char (List SearchResult))
1717
18−derive instance newtypeEmailAddress :: Newtype SearchIndex _
18+derive instance newtypeSearchIndex :: Newtype SearchIndex _
19+
20+newtype SearchResult
21+ = SearchResult { name :: String
22+ }
23+
24+derive instance newtypeSearchResult :: Newtype SearchResult _
1925
2026mkSearchIndex :: Array Declarations -> SearchIndex
2127mkSearchIndex = SearchIndex <<< foldr insertDeclarations mempty
@@ -23,12 +29,29 @@ mkSearchIndex = SearchIndex <<< foldr insertDeclarations mempty
2329 insertDeclarations (Declarations { name, declarations }) trie
2430 = foldr insertIndexEntry trie declarations
2531
26− insertIndexEntry entry@(IndexEntry { title })
27− = alter (List.fromFoldable $ toCharArray $ toLower title) updateEntries
28− where
29− updateEntries mbOldEntries =
30− case mbOldEntries of
31− Just oldEntries ->
32− Just $ entry : oldEntries
33− Nothing ->
34− Just $ List.singleton entry
32+ insertIndexEntry entry@(IndexEntry { title }) trie
33+ = foldr insertSearchResult trie (resultsForEntry entry)
34+
35+ insertSearchResult :: { path :: String
36+ , result :: SearchResult
37+ } -> Trie Char (List SearchResult) -> Trie Char (List SearchResult)
38+ insertSearchResult { path, result } trie =
39+ let path' = List.fromFoldable $ toCharArray $ toLower path in
40+ alter path' (updateResults result) trie
41+
42+ updateResults result mbOldResults =
43+ case mbOldResults of
44+ Just oldResults ->
45+ Just $ List.singleton result <> oldResults
46+ Nothing ->
47+ Just $ List.singleton result
48+
49+ resultsForEntry :: IndexEntry -> List { path :: String
50+ , result :: SearchResult
51+ }
52+ resultsForEntry (IndexEntry { title })
53+ = List.fromFoldable
54+ [ { path: title
55+ , result : SearchResult { name: title }
56+ }
57+ ]