" ++ (text |> replace "&" "&" |> replace "<" "<" |> replace ">" ">" ) ++ "
" ++ (if page /= -1 then "— p. " ++ fromInt page ++ "" else "" ) ) excerpts ) ++ """ """ ) export : Model -> Posix -> Cmd msg export model time = let sortedBooks = sortBooks RecencySort True model.excerptCountMap model.favCountMap (values model.books) titles = map (.title >> replace "&" "and") sortedBooks timeString = join "-" [ toYear utc time |> fromInt , toMonth utc time |> monthToInt |> padN , toDay utc time |> padN ] ++ "T" ++ join ":" (map (\f -> f utc time |> padN) [ toHour, toMinute, toSecond ] ) ++ "Z" epubId = hex timeString in [ [ container , generateToc titles , generateTocNcx epubId titles , generateContent epubId timeString titles ] , indexedMap (\i { id, title, authors } -> generateChapter i (replace " & " " and " title) authors (model.excerpts |> values |> filter (.bookId >> (==) id) |> sortBy .page ) ) sortedBooks ] |> concat |> map (\( path, text ) -> ( path, Regex.replace trimRx (always " ") text ) ) |> createEpub padN : Int -> String padN = fromInt >> padLeft 2 '0' monthToInt : Month -> Int monthToInt month = case month of Jan -> 1 Feb -> 2 Mar -> 3 Apr -> 4 May -> 5 Jun -> 6 Jul -> 7 Aug -> 8 Sep -> 9 Oct -> 10 Nov -> 11 Dec -> 12 ================================================ FILE: src/JsonParser.elm ================================================ module JsonParser exposing (decodeStoredModel) import Json.Decode exposing ( Decoder , Error , bool , decodeString , float , index , int , list , map2 , nullable , string , succeed ) import Json.Decode.Pipeline exposing (optional, required) import Tuple exposing (pair) import Types exposing (Book, Excerpt, StoredModel) import Utils exposing (defaultSemanticThreshold) decodeStoredModel : String -> Result Error StoredModel decodeStoredModel = succeed StoredModel |> required "excerpts" (list excerptDecoder) |> required "books" (list bookDecoder) |> optional "hiddenExcerpts" (list string) [] |> optional "bookmarks" (list (map2 pair (index 0 string) (index 1 string))) [] |> optional "semanticThreshold" float defaultSemanticThreshold |> optional "version" string "" |> optional "didJoinMailingList" bool False |> decodeString excerptDecoder : Decoder Excerpt excerptDecoder = succeed Excerpt |> required "id" string |> required "text" string |> required "bookId" string |> optional "date" int 0 |> optional "page" int -1 |> optional "notes" string "" |> optional "isFavorite" bool False |> optional "sourceUrl" (nullable string) Nothing |> optional "lenses" (list (map2 pair (index 0 string) (index 1 (list string)))) [] bookDecoder : Decoder Book bookDecoder = succeed Book |> required "id" string |> required "title" string |> required "authors" (list string) |> optional "rating" float 0 |> optional "sortIndex" int 0 |> optional "tags" (list string) [] |> required "slug" string |> optional "notes" string "" ================================================ FILE: src/KindleParser.elm ================================================ module KindleParser exposing (parse) import DateTime exposing (fromRawParts, toPosix) import Dict import List exposing (drop, filter, filterMap, head, indexedMap, reverse, take) import Parser exposing ( (|.) , (|=) , Parser , Step(..) , andThen , backtrackable , chompIf , chompUntil , chompWhile , deadEndsToString , end , getChompedString , int , loop , map , oneOf , problem , run , spaces , succeed , symbol ) import String exposing (trim) import Time exposing (Month(..), posixToMillis) import Types exposing (BookMap, ExcerptMap) import Utils exposing (makeExcerpt) type alias Metadata = { type_ : MetadataType , page : Maybe Int , location : Maybe Int , date : Maybe Int } type MetadataType = Highlight | Bookmark | Note | Unknown separator : String separator = "==========" parse : String -> Result String ( ExcerptMap, BookMap ) parse input = case run (loop ( [], Dict.empty, Dict.empty ) blockLoop) input of Ok ( _, excerpts, books ) -> Ok ( excerpts, books ) Err e -> Err <| "Failed to parse Kindle highlights: " ++ deadEndsToString e type alias State = ( List String, ExcerptMap, BookMap ) blockLoop : State -> Parser (Step State State) blockLoop ( lastIds, excerpts, books ) = oneOf [ succeed (\entry -> Loop (updateState entry ( lastIds, excerpts, books )) ) |= entryParser , succeed (Done ( lastIds, excerpts, books )) |. end ] updateState : Maybe Entry -> State -> State updateState mEntry ( lastIds, excerpts, books ) = case mEntry of Just (HighlightEntry e) -> let ( excerpt, book ) = makeExcerpt e.title e.author e.content e.page e.date "" Nothing newExcerpts = Dict.insert excerpt.id excerpt excerpts newBooks = Dict.update book.id (\mBook -> Just <| case mBook of Just b -> { b | sortIndex = max b.sortIndex excerpt.date } Nothing -> book ) books in ( excerpt.id :: lastIds, newExcerpts, newBooks ) Just (NoteEntry n) -> case lastIds of id :: _ -> let newExcerpts = Dict.update id (Maybe.map (\e -> { e | notes = n.content })) excerpts in ( lastIds, newExcerpts, books ) [] -> ( lastIds, excerpts, books ) Nothing -> ( lastIds, excerpts, books ) type Entry = HighlightEntry { title : String , author : String , content : String , page : Maybe Int , date : Maybe Int } | NoteEntry { title : String , author : String , content : String , page : Maybe Int , date : Maybe Int } entryParser : Parser (Maybe Entry) entryParser = succeed (\( title, author ) meta content -> let page = case meta.page of Just p -> Just p Nothing -> Maybe.map (max 1 << (//) 15) meta.location in case meta.type_ of Highlight -> Just (HighlightEntry { title = title , author = author , content = content , page = page , date = meta.date } ) Note -> Just (NoteEntry { title = title , author = author , content = content , page = page , date = meta.date } ) _ -> Nothing ) |= titleAuthorParser |. lineBreak |= metadataParser |. lineBreak |. spaces |= contentParser |. symbol separator |. spaces lineBreak : Parser () lineBreak = oneOf [ symbol "\u{000D}\n" , symbol "\n" ] titleAuthorParser : Parser ( String, String ) titleAuthorParser = getChompedString (chompUntil "\n") |> map splitTitleAuthor splitTitleAuthor : String -> ( String, String ) splitTitleAuthor s = let trimmed = trim s in if String.endsWith ")" trimmed then case String.indices "(" trimmed |> reverse |> head of Just openParenIndex -> ( trim (String.left openParenIndex trimmed) , trim (String.slice (openParenIndex + 1) (String.length trimmed - 1) trimmed ) ) Nothing -> splitByDash trimmed else splitByDash trimmed splitByDash : String -> ( String, String ) splitByDash s = case String.split " - " s of [ t, a ] -> ( trim t, trim a ) _ -> ( trim s, "" ) metadataParser : Parser Metadata metadataParser = getChompedString (chompUntil "\n") |> andThen (\line -> case run subMetadataParser (trim line) of Ok m -> succeed m Err _ -> succeed { type_ = Unknown , page = Nothing , location = Nothing , date = Nothing } ) subMetadataParser : Parser Metadata subMetadataParser = succeed (\type_ mPage mLocation date -> { type_ = type_ , page = mPage , location = mLocation , date = date } ) |. symbol "- Your " |= typeParser |= oneOf [ backtrackable (succeed Just |. oneOf [ chompUntil "Page ", chompUntil "page " ] |. oneOf [ symbol "Page ", symbol "page " ] |= int ) , succeed Nothing ] |= oneOf [ backtrackable (succeed Just |. oneOf [ chompUntil "Location ", chompUntil "location " ] |. oneOf [ symbol "Location ", symbol "location " ] |= int |. chompWhile (\c -> Char.isDigit c || c == '-') ) , succeed Nothing ] |= oneOf [ backtrackable (succeed identity |. chompUntil "Added on " |. symbol "Added on " |= dateParser ) , succeed Nothing ] typeParser : Parser MetadataType typeParser = oneOf [ symbol "Highlight" |> map (always Highlight) , symbol "Bookmark" |> map (always Bookmark) , symbol "Note" |> map (always Note) , succeed Unknown ] monthToEnum : String -> Maybe Month monthToEnum s = case String.toLower (trim s) of "january" -> Just Jan "february" -> Just Feb "march" -> Just Mar "april" -> Just Apr "may" -> Just May "june" -> Just Jun "july" -> Just Jul "august" -> Just Aug "september" -> Just Sep "october" -> Just Oct "november" -> Just Nov "december" -> Just Dec _ -> Nothing monthFromInt : Int -> Month monthFromInt n = case n of 1 -> Jan 2 -> Feb 3 -> Mar 4 -> Apr 5 -> May 6 -> Jun 7 -> Jul 8 -> Aug 9 -> Sep 10 -> Oct 11 -> Nov 12 -> Dec _ -> Jan dateParser : Parser (Maybe Int) dateParser = loop ( [], [] ) (\( nums, words ) -> oneOf [ backtrackable (getChompedString (chompWhile Char.isDigit) |> andThen (\s -> if s == "" then problem "not a number" else case String.toInt s of Just n -> succeed (Loop ( n :: nums, words )) Nothing -> problem "not a number" ) ) , backtrackable (getChompedString (chompWhile Char.isAlpha) |> andThen (\s -> if s == "" then problem "empty" else succeed (Loop ( nums, s :: words )) ) ) , chompIf (always True) |> map (always (Loop ( nums, words ))) , succeed (Done ( reverse nums, reverse words )) ] ) |> map (\( nums, words ) -> let yearMatch = nums |> indexedMap Tuple.pair |> filter (\( _, n ) -> n > 1900) |> head ( year, dateNums, timeNums ) = case yearMatch of Just ( idx, y ) -> ( y , take idx nums , drop (idx + 1) nums ) Nothing -> ( 2026, [], nums ) monthFromWords = filterMap monthToEnum words |> head day = case ( monthFromWords, dateNums ) of ( Just _, dVal :: _ ) -> dVal ( Nothing, _ :: dVal :: _ ) -> dVal ( _, dVal :: _ ) -> dVal _ -> 1 month = case ( monthFromWords, dateNums ) of ( Just m, _ ) -> m ( Nothing, mVal :: _ ) -> monthFromInt mVal _ -> Jan ( h, mi, s ) = case timeNums of h1 :: m1 :: s1 :: _ -> ( h1, m1, s1 ) h1 :: m1 :: _ -> ( h1, m1, 0 ) h1 :: _ -> ( h1, 0, 0 ) _ -> ( 0, 0, 0 ) meridian = filter (\w -> let lw = String.toLower w in lw == "am" || lw == "pm" ) words |> head hour = case meridian |> Maybe.map String.toLower of Just "pm" -> if h < 12 then h + 12 else h Just "am" -> if h == 12 then 0 else h _ -> h in fromRawParts { day = day , month = month , year = year } { hours = hour, minutes = mi, seconds = s, milliseconds = 0 } |> Maybe.map (toPosix >> posixToMillis) ) contentParser : Parser String contentParser = separator |> chompUntil |> getChompedString |> map trim ================================================ FILE: src/Main.elm ================================================ module Main exposing (main) import Browser exposing (application) import Browser.Dom exposing (getElement, setViewport) import Browser.Navigation as Nav import CsvParser import Debounce import Dict exposing (get, insert, keys, remove, values) import Epub import File import File.Select as Select import Http import Json.Decode as Decode import JsonParser exposing (decodeStoredModel) import KindleParser import List exposing ( all , concatMap , drop , filter , filterMap , foldl , head , indexedMap , isEmpty , length , map , map2 , member , sort , sortBy , take ) import Maybe exposing (andThen, withDefault) import Model exposing (ModalMsg(..), Model) import Msg exposing (Msg(..)) import Platform.Cmd exposing (batch, none) import Ports exposing (..) import Random exposing (generate) import Random.List exposing (shuffle) import Router exposing ( Route(..) , excerptToRoute , routeParser , searchToRoute , titleSlugToRoute ) import Set exposing (diff, toList, union) import String exposing (fromInt, join, toLower, trim) import Task exposing (attempt, perform) import Time exposing (posixToMillis) import Types exposing ( BookSort(..) , ExcerptSort(..) , ExcerptTab(..) , Lens(..) , Page(..) , PendingExcerpt , SearchMode(..) , StoredModel , TagSort(..) ) import Update.Extra as Update exposing (addCmd) import Url exposing (Url, percentEncode) import Url.Parser exposing (parse) import Utils exposing ( appName , countLabel , dedupe , defaultSemanticThreshold , delay , excerptCountLabel , fetchLensText , findMatches , getAuthorRouteMap , getAuthors , getCounts , getTagCounts , getTitleRouteMap , insertOnce , lensToString , makeExcerpt , modelToStoredModel , removeItem , slugify , toDict , untaggedKey , upsert ) import Views.Base exposing (view) import Views.Landing exposing (landingPageBooks) minSemanticQueryLen : Int minSemanticQueryLen = 5 embeddingBatchSize : Int embeddingBatchSize = 10 excerptNeighborK : Int excerptNeighborK = 5 bookNeighborK : Int bookNeighborK = 6 authorNeighborK : Int authorNeighborK = 5 debounceConfig : Debounce.Config Msg debounceConfig = { strategy = Debounce.soon 999 , transform = DebounceMsg } mimeTxt : String mimeTxt = "text/plain" mimeCsv : String mimeCsv = "text/csv" mimeJson : String mimeJson = "application/json" demoJsonPath : String demoJsonPath = "/demo/demo.json" createModel : Maybe StoredModel -> List String -> ( String, String, String ) -> Bool -> Url -> Nav.Key -> Model createModel mStoredModel supportIssues ( version, mailingListUrl, mailingListField ) demoMode url key = let restored = withDefault { excerpts = [] , books = [] , hiddenExcerpts = [] , bookmarks = [] , semanticThreshold = defaultSemanticThreshold , version = "" , didJoinMailingList = False } mStoredModel ( titleRouteMap, booksWithSlugs ) = getTitleRouteMap restored.books books = toDict booksWithSlugs tags = restored.books |> concatMap .tags |> dedupe ( exCount, favCount ) = getCounts restored.excerpts in { page = MainPage (values books) Nothing , demoMode = demoMode , excerpts = toDict restored.excerpts , books = books , semanticThreshold = restored.semanticThreshold , neighborMap = Dict.empty , bookNeighborMap = Dict.empty , authorNeighborMap = Dict.empty , semanticRankMap = Dict.empty , hiddenExcerpts = Set.fromList restored.hiddenExcerpts , completedEmbeddings = Set.empty , embeddingsReady = False , authorEmbeddingsReady = False , tags = tags , tagCounts = getTagCounts books , tagSort = TagAlphaSort , showTagHeader = not (isEmpty tags) , titleRouteMap = titleRouteMap , authorRouteMap = getAuthorRouteMap restored.books , excerptCountMap = exCount , favCountMap = favCount , pendingTag = Nothing , isDragging = False , reverseSort = True , modalMessage = Nothing , url = url , key = key , bookSort = RecencySort , excerptSort = ExcerptPageSort , bookmarks = restored.bookmarks |> Dict.fromList , idToShowDetails = Dict.empty , idToActiveTab = Dict.empty , searchQuery = "" , searchDebounce = Debounce.init , version = version , mailingListEmail = "" , mailingListUrl = mailingListUrl , mailingListField = mailingListField , didJoinMailingList = restored.didJoinMailingList , supportIssues = supportIssues , showHoverUi = False } main : Program ( Maybe String, List String, ( String, String, String ) ) Model Msg main = application { init = init , update = update , view = \m -> { title = case m.page of MainPage _ Nothing -> appName LandingPage _ _ -> appName ++ " — organize your book highlights with AI" _ -> (case m.page of MainPage _ (Just tag) -> "#" ++ tag SearchPage query _ _ _ _ -> "🔍 " ++ query TitlePage book _ _ -> book.title AuthorPage author _ -> author ExcerptPage excerpt book -> book.title ++ " p. " ++ fromInt excerpt.page SettingsPage -> "Settings" ImportPage -> "Import" MonkPage -> "Monk-Mode" CreatePage _ _ _ -> "New excerpt" NotFoundPage _ -> "404" _ -> "" ) ++ " | " ++ appName , body = [ view m ] } , subscriptions = always <| Sub.batch [ receiveExcerptNeighbors ReceiveNeighbors , receiveBookNeighbors ReceiveBookNeighbors , receiveAuthorNeighbors ReceiveAuthorNeighbors , receiveExcerptEmbeddings ReceiveEmbeddings , receiveBookEmbeddings (always ReceiveBookEmbeddings) , receiveAuthorEmbeddings (always ReceiveAuthorEmbeddings) , receiveUnicodeNormalized ReceiveUnicodeNormalized , receiveSemanticSearch ReceiveSemanticSearch , receiveSemanticRank ReceiveSemanticRank , syncState SyncState ] , onUrlChange = UrlChanged , onUrlRequest = LinkClicked } init : ( Maybe String, List String, ( String, String, String ) ) -> Url -> Nav.Key -> ( Model, Cmd Msg ) init ( mStateString, supportIssues, params ) url key = let model = createModel (case decodeStoredModel (withDefault "" mStateString) of Ok storedModel -> Just storedModel _ -> Nothing ) supportIssues params False url key in update (UrlChanged url) model |> (if Dict.isEmpty model.excerpts then identity else addCmd (model |> modelToStoredModel |> handleNewExcerpts) ) store : ( Model, Cmd Msg ) -> ( Model, Cmd Msg ) store ( model, cmd ) = if model.demoMode then ( model, cmd ) else ( model, batch [ cmd, model |> modelToStoredModel |> setStorage ] ) update : Msg -> Model -> ( Model, Cmd Msg ) update message model = let noOp = ( model, none ) in case message of NoOp -> noOp RestoreState maybeModel demoMode -> let model_ = createModel maybeModel model.supportIssues ( model.version , model.mailingListUrl , model.mailingListField ) demoMode model.url model.key in update (UrlChanged model.url) model_ |> addCmd (batch [ model_ |> modelToStoredModel |> initWithClear , if demoMode then setDemoEmbeddings (keys model_.excerpts) else model_ |> modelToStoredModel |> setStorage ] ) |> addCmd (Nav.pushUrl model.key "/") ParseJsonText forceDemoExit text -> let demoMode = if forceDemoExit then False else model.demoMode in case decodeStoredModel text of Ok storedModel -> update (RestoreState (Just storedModel) demoMode) { model | modalMessage = Just <| InfoMsg <| "Restored " ++ (storedModel.excerpts |> length |> excerptCountLabel ) ++ "." , demoMode = demoMode , completedEmbeddings = Set.empty } Err e -> ( { model | modalMessage = Just <| ErrMsg (Decode.errorToString e) } , none ) ParseCsvText text -> case CsvParser.parse text of Ok ( excerpts, books ) -> update (MergeNewExcerpts excerpts books) model |> addCmd (Nav.pushUrl model.key "/") Err err -> ( { model | modalMessage = Just <| ErrMsg err }, none ) SyncState sModel -> let ( exCount, favCount ) = getCounts sModel.excerpts in ( { model | excerpts = toDict sModel.excerpts , books = toDict sModel.books , hiddenExcerpts = Set.fromList sModel.hiddenExcerpts , bookmarks = Dict.fromList sModel.bookmarks , excerptCountMap = exCount , favCountMap = favCount } , none ) |> Update.andThen update (UrlChanged model.url) DragEnter -> ( { model | isDragging = True }, none ) DragLeave -> ( { model | isDragging = False }, none ) GotFile msg file -> ( { model | isDragging = False } , perform msg (File.toString file) ) GotDroppedFile file -> let mime = File.mime file mMsg = if mime == mimeTxt then Just LoadKindleFile else if mime == mimeCsv then Just ParseCsvText else if mime == mimeJson then Just <| ParseJsonText True else Nothing in case mMsg of Just msg -> update (GotFile msg file) model _ -> ( { model | modalMessage = Just <| ErrMsg <| "Unsupported file type (" ++ mime ++ ")" } , none ) PickKindleFile -> ( model, Select.file [ mimeTxt ] (GotFile LoadKindleFile) ) LoadKindleFile text -> ( model, requestUnicodeNormalized text ) ReceiveUnicodeNormalized text -> case KindleParser.parse text of Ok ( excerpts, books ) -> if Dict.isEmpty excerpts then ( { model | modalMessage = Just <| ErrMsg "No highlights found in file." } , none ) else update (MergeNewExcerpts excerpts books) model Err err -> ( { model | modalMessage = Just <| ErrMsg err } , none ) MergeNewExcerpts newExcerpts newBooks -> let hiddenPred = \id _ -> not <| Set.member id model.hiddenExcerpts unseenExcerpts = if model.demoMode then newExcerpts else Dict.diff newExcerpts model.excerpts |> Dict.filter hiddenPred bookVals = unseenExcerpts |> Dict.foldl (\_ excerpt acc -> Dict.update excerpt.bookId (Maybe.map (\book -> { book | sortIndex = max book.sortIndex excerpt.date } ) ) acc ) (if model.demoMode then newBooks else Dict.union model.books newBooks ) |> values ( titleRouteMap, booksWithSlugs ) = getTitleRouteMap bookVals excerpts = if model.demoMode then newExcerpts else Dict.union model.excerpts newExcerpts |> Dict.filter hiddenPred ( exCount, favCount ) = excerpts |> values |> getCounts books = booksWithSlugs |> filter (\{ id } -> get id exCount |> withDefault 0 |> (/=) 0 ) |> toDict in store ( { model | demoMode = False , modalMessage = let n = Dict.size unseenExcerpts in Just <| InfoMsg <| countLabel "new excerpt" n ++ " imported." , excerpts = excerpts , books = books , excerptCountMap = exCount , favCountMap = favCount , titleRouteMap = titleRouteMap , authorRouteMap = getAuthorRouteMap bookVals , embeddingsReady = False , neighborMap = Dict.empty , completedEmbeddings = if model.demoMode then Set.empty else model.completedEmbeddings , tags = if model.demoMode then [] else model.tags , tagCounts = if model.demoMode then Dict.empty else getTagCounts books , showTagHeader = if model.demoMode then False else model.showTagHeader } , batch [ model |> modelToStoredModel |> (if model.demoMode then initWithClear else handleNewExcerpts ) , Nav.pushUrl model.key "/" ] ) ClearModal -> ( { model | modalMessage = Nothing }, none ) ShowRandom -> ( model , generate GotRandomIndex (Random.int 0 ((model.excerpts |> Dict.size) - 1)) ) GotRandomIndex n -> case model.excerpts |> values |> drop n |> head of Just excerpt -> ( model , Nav.pushUrl model.key (excerptToRoute model.books excerpt) ) _ -> noOp UpdateNotes id text -> store ( { model | excerpts = Dict.update id (Maybe.map (\excerpt -> { excerpt | notes = text })) model.excerpts , page = case model.page of ExcerptPage excerpt book -> ExcerptPage { excerpt | notes = text } book TitlePage book excerpts _ -> TitlePage book (map (\excerpt -> if excerpt.id == id then { excerpt | notes = text } else excerpt ) excerpts ) False _ -> model.page } , none ) UpdateBookNotes id text -> let f = \book -> { book | notes = text } in store ( { model | books = Dict.update id (Maybe.map f) model.books , page = case model.page of TitlePage book excerpts editMode -> TitlePage (f book) excerpts editMode _ -> model.page } , none ) UpdatePendingTag text -> ( { model | pendingTag = Just text }, none ) AddTag -> case model.page of TitlePage book excerpts _ -> case model.pendingTag of Just tag -> let tagN = tag |> trim |> toLower |> slugify newTagSet = insertOnce book.tags tagN in if tagN == "" || tagN == untaggedKey then ( { model | pendingTag = Nothing }, none ) else let books = Dict.update book.id (Maybe.map (\b -> { b | tags = newTagSet }) ) model.books in store ( { model | books = books , tags = insertOnce model.tags tagN , tagCounts = getTagCounts books , pendingTag = Nothing , page = TitlePage { book | tags = newTagSet } excerpts False } , none ) _ -> noOp _ -> noOp RemoveTag tag -> case model.page of TitlePage book excerpts _ -> let newTagSet = removeItem book.tags tag books = Dict.update book.id (Maybe.map (\b -> { b | tags = newTagSet })) model.books in store ( { model | books = books , tags = books |> values |> concatMap .tags |> dedupe , tagCounts = getTagCounts books , page = TitlePage { book | tags = newTagSet } excerpts False } , none ) _ -> noOp SetRating book n -> let newBook = { book | rating = n } in store ( { model | books = insert book.id newBook model.books , page = case model.page of TitlePage _ excerpts _ -> TitlePage newBook excerpts False _ -> model.page } , none ) SetTagSort sort -> ( { model | tagSort = sort }, none ) DeleteExcerpt excerpt -> let newExcerpts = remove excerpt.id model.excerpts ( books, bookmarks ) = case get excerpt.bookId model.excerptCountMap of Just n -> if n == 1 then ( remove excerpt.bookId model.books , remove excerpt.bookId model.bookmarks ) else ( model.books , case get excerpt.bookId model.bookmarks of Just id -> if id == excerpt.id then remove excerpt.bookId model.bookmarks else model.bookmarks _ -> model.bookmarks ) _ -> ( model.books, model.bookmarks ) ( exCount, favCount ) = newExcerpts |> values |> getCounts in store ( { model | hiddenExcerpts = Set.insert excerpt.id model.hiddenExcerpts , excerpts = newExcerpts , books = books , bookmarks = bookmarks , excerptCountMap = exCount , favCountMap = favCount , page = case model.page of TitlePage oldBook oldExcerpts _ -> TitlePage (withDefault oldBook (get oldBook.id books)) (filter (\e -> e.id /= excerpt.id) oldExcerpts ) False _ -> model.page , tagCounts = getTagCounts books , completedEmbeddings = Set.remove excerpt.id model.completedEmbeddings , neighborMap = Dict.empty , bookNeighborMap = Dict.empty , modalMessage = Nothing } , batch [ deleteExcerpt ( excerpt.id , ( excerpt.bookId , newExcerpts |> Dict.filter (\_ { bookId } -> bookId == excerpt.bookId) |> values |> map .id ) , bookNeighborK ) , case model.page of ExcerptPage { id } _ -> if id == excerpt.id then Nav.pushUrl model.key "/" else none TitlePage book ents _ -> if book.id == excerpt.bookId && length ents == 1 then Nav.pushUrl model.key "/" else none _ -> none ] ) DeleteBook book -> let exIds = model.excerpts |> Dict.filter (\_ { bookId } -> bookId == book.id) |> values |> map .id |> Set.fromList tagCounts = foldl (\tag acc -> Dict.update tag (Maybe.map ((+) -1)) acc ) model.tagCounts book.tags excerpts = Dict.filter (\_ { bookId } -> bookId /= book.id) model.excerpts ( exCount, favCount ) = excerpts |> values |> getCounts newBooks = remove book.id model.books in store ( { model | modalMessage = Nothing , excerpts = excerpts , books = newBooks , excerptCountMap = exCount , favCountMap = favCount , bookNeighborMap = Dict.empty , neighborMap = Dict.empty , authorNeighborMap = Dict.empty , hiddenExcerpts = union model.hiddenExcerpts exIds , completedEmbeddings = diff model.completedEmbeddings exIds , titleRouteMap = remove book.slug model.titleRouteMap , tagCounts = tagCounts , tags = newBooks |> values |> concatMap .tags |> dedupe , bookmarks = remove book.id model.bookmarks } , batch [ Nav.pushUrl model.key "/" , deleteBook ( book.id, toList exIds ) ] ) EnterBookEditMode -> case model.page of TitlePage book excerpts _ -> ( { model | page = TitlePage book excerpts True }, none ) _ -> noOp ExitBookEditMode -> case model.page of TitlePage book excerpts _ -> ( { model | page = TitlePage { book | title = withDefault book.title (get book.id model.books |> Maybe.map .title ) } excerpts False } , none ) _ -> noOp SetPendingBookTitle title -> case model.page of TitlePage book excerpts editMode -> ( { model | page = TitlePage { book | title = title } excerpts editMode } , none ) _ -> noOp SetPendingBookAuthor author -> case model.page of TitlePage book excerpts editMode -> ( { model | page = TitlePage { book | authors = String.split "/" author } excerpts editMode } , none ) _ -> noOp SetBookEdits -> case model.page of TitlePage book excerpts _ -> let newBook = { book | title = trim book.title , authors = book.authors |> map trim |> filter (not << String.isEmpty) } in if trim newBook.title == "" then update ExitBookEditMode model else let ( titleRouteMap, booksWithSlugs ) = insert book.id newBook model.books |> values |> getTitleRouteMap newBooks = toDict booksWithSlugs in store ( { model | page = TitlePage book excerpts False , books = newBooks , titleRouteMap = titleRouteMap } , Nav.replaceUrl model.key (titleSlugToRoute (get book.id newBooks |> Maybe.map .slug |> withDefault "" ) ) ) _ -> noOp ShowConfirmation text action -> ( { model | modalMessage = Just <| ConfirmationMsg text action } , none ) Sort -> store ( { model | reverseSort = not model.reverseSort }, none ) ToggleTagHeader -> ( { model | showTagHeader = not model.showTagHeader }, none ) ScrollToElement result -> case result of Ok element -> ( model , perform (always NoOp) (setViewport 0 element.element.y) ) Err _ -> noOp ExportJson -> ( model, model |> modelToStoredModel |> exportJson ) ImportJson -> ( model , Select.file [ mimeJson ] (GotFile (ParseJsonText True)) ) ImportCsv -> ( model , Select.file [ mimeCsv ] (GotFile ParseCsvText) ) ExportEpub time -> ( model, Epub.export model time ) RequestEmbeddings -> let nextBatch = diff (diff (model.excerpts |> keys |> Set.fromList) model.completedEmbeddings ) model.hiddenExcerpts |> toList |> filterMap (\id -> get id model.excerpts) |> take embeddingBatchSize |> map (\excerpt -> ( excerpt.id, excerpt.text )) in if isEmpty nextBatch then ( model , model.books |> values |> map (\{ id } -> ( id , model.excerpts |> values |> filter (.bookId >> (==) id) |> map .id ) ) |> requestBookEmbeddings ) else ( { model | embeddingsReady = False } , requestExcerptEmbeddings nextBatch ) ReceiveEmbeddings ids -> update RequestEmbeddings { model | completedEmbeddings = union model.completedEmbeddings (Set.fromList ids) } ReceiveBookEmbeddings -> ( { model | embeddingsReady = True } , case model.page of TitlePage book _ _ -> batch [ requestBookNeighbors ( book.id, bookNeighborK ) , requestSemanticRank ( book.id , model.excerpts |> Dict.filter (\_ { bookId } -> bookId == book.id ) |> values |> map .id ) ] ExcerptPage excerpt _ -> requestExcerptNeighbors ( excerpt.id, excerptNeighborK, True ) SearchPage query _ _ _ _ -> requestSemanticSearch ( query, model.semanticThreshold ) AuthorPage _ _ -> model |> getAuthors |> requestAuthorEmbeddings _ -> none ) ReceiveAuthorEmbeddings -> ( { model | authorEmbeddingsReady = True } , case model.page of AuthorPage author _ -> requestAuthorNeighbors ( author, authorNeighborK ) _ -> none ) ReceiveNeighbors ( targetId, idScores ) -> if Dict.member targetId model.excerpts then ( { model | neighborMap = insert targetId (filterMap (\( id, score ) -> case get id model.excerpts of Just excerpt -> Just ( excerpt.id, score ) _ -> Nothing ) idScores ) model.neighborMap } , none ) else noOp ReceiveBookNeighbors ( targetId, idScores ) -> if Dict.member targetId model.books then ( { model | bookNeighborMap = insert targetId (filterMap (\( id, score ) -> case get id model.books of Just book -> Just ( book.id, score ) _ -> Nothing ) idScores ) model.bookNeighborMap } , none ) else noOp ReceiveAuthorNeighbors ( targetAuthor, idScores ) -> ( { model | authorNeighborMap = insert targetAuthor idScores model.authorNeighborMap } , none ) ReceiveSemanticSearch ( _, idScores ) -> case model.page of SearchPage query mode books excerpts _ -> ( { model | page = SearchPage query mode books excerpts (filter (\( id, _ ) -> not <| foldl (\excerpt acc -> acc || excerpt.id == id ) False excerpts ) idScores ) } , none ) _ -> noOp ReceiveSemanticRank ( bookId, ids ) -> let model_ = { model | semanticRankMap = insert bookId ids model.semanticRankMap } in case model.page of TitlePage book _ _ -> if book.id == bookId && model.excerptSort == ExcerptSemanticSort then update (SortExcerpts model.excerptSort) model_ else ( model_, none ) _ -> ( model_, none ) SetSemanticThreshold s -> case String.toFloat s of Just n -> store ( { model | semanticThreshold = n }, none ) _ -> noOp LinkClicked urlRequest -> case urlRequest of Browser.Internal url -> if url == model.url then noOp else ( model, Nav.pushUrl model.key (Url.toString url) ) Browser.External href -> ( model, Nav.load href ) UrlChanged url -> let model_ = { model | url = url , pendingTag = Nothing , searchQuery = "" } scrollTop = perform (always NoOp) (setViewport 0 0) showLanding = Dict.isEmpty model.books rootRedirect = ( model_, Nav.pushUrl model.key "/" ) in case parse routeParser url of Just RootRoute -> ( { model_ | page = if showLanding then LandingPage [] Dict.empty else MainPage (values model.books) Nothing } , batch [ scrollTop , if showLanding then batch [ generate GotLandingData (Random.pair (shuffle landingPageBooks) (Random.list (length landingPageBooks) (Random.int 3 79) ) ) , Http.get { url = demoJsonPath , expect = Http.expectWhatever (always NoOp) } , fetchDemoEmbeddings () ] else none ] ) Just (TitleRoute slug mFragment) -> case get slug model.titleRouteMap |> andThen (\id -> get id model.books) of Just book -> let excerpts = model.excerpts |> Dict.filter (\_ { bookId } -> bookId == book.id ) |> values |> sortBy (\{ page, date } -> if page == -1 then date else page ) in ( { model_ | page = TitlePage book excerpts False , excerptSort = ExcerptPageSort , showHoverUi = True } , batch ((case mFragment of Just excerptId -> attempt ScrollToElement (getElement excerptId) _ -> case parse routeParser model.url of Just (TitleRoute lastSlug _) -> if lastSlug == slug then none else scrollTop _ -> scrollTop ) :: (if model.embeddingsReady then [ if not (Dict.member book.id model.bookNeighborMap) then requestBookNeighbors ( book.id, bookNeighborK ) else none , if not (Dict.member book.id model.semanticRankMap) then requestSemanticRank ( book.id , map .id excerpts ) else none ] else [] ) ++ [ delay 2000 (HideHoverUiState book.id) ] ) ) _ -> if showLanding then rootRedirect else ( { model_ | page = NotFoundPage "Title not found." } , none ) Just (ExcerptRoute titleSlug excerptSlug) -> let mExcerpt = get excerptSlug model.excerpts mBook = get titleSlug model.titleRouteMap |> andThen (\id -> get id model.books) in case ( mExcerpt, mBook ) of ( Just excerpt, Just book ) -> ( { model_ | page = ExcerptPage excerpt book } , batch [ scrollTop , if model.embeddingsReady && not (Dict.member excerpt.id model.neighborMap) then requestExcerptNeighbors ( excerpt.id, excerptNeighborK, True ) else none , if model.demoMode then fetchLensText excerpt.id Succinct else none ] ) _ -> if showLanding then rootRedirect else ( { model_ | page = NotFoundPage "Excerpt not found." } , none ) Just (AuthorRoute slug) -> case get slug model.authorRouteMap of Just author -> ( { model_ | page = AuthorPage author (model.books |> Dict.filter (\_ b -> member author b.authors) |> values ) } , batch [ if model.authorEmbeddingsReady then if not <| Dict.member author model.authorNeighborMap then requestAuthorNeighbors ( author, authorNeighborK ) else none else if model.embeddingsReady then model |> getAuthors |> requestAuthorEmbeddings else none , scrollTop ] ) _ -> if showLanding then rootRedirect else ( { model_ | page = NotFoundPage "Author not found." } , none ) Just (TagRoute tag) -> if tag == untaggedKey || member tag model.tags then ( { model_ | page = MainPage (Dict.filter (if tag == untaggedKey then \_ book -> isEmpty book.tags else \_ book -> member tag book.tags ) model.books |> values ) (Just tag) } , scrollTop ) else if showLanding then rootRedirect else ( { model_ | page = NotFoundPage "Tag not found." } , scrollTop ) Just (SearchRoute query) -> if showLanding then rootRedirect else case query of Just text -> let ( debounce, cmd ) = Debounce.push debounceConfig text model.searchDebounce in ( { model_ | searchDebounce = debounce , searchQuery = text , page = case model.page of SearchPage _ _ _ _ _ -> model.page _ -> SearchPage text TextMatches [] [] [] } , cmd ) _ -> ( model_, none ) Just SettingsRoute -> ( { model_ | page = SettingsPage }, scrollTop ) Just ImportRoute -> ( { model_ | page = ImportPage }, scrollTop ) Just MonkRoute -> ( { model_ | page = MonkPage }, scrollTop ) Just (CreateRoute mTitle mAuthor mText mSource mPage) -> let pendingEx = PendingExcerpt (withDefault "" mTitle) (withDefault "" mAuthor) (withDefault "" mText) mPage (withDefault "" mSource) in if all (not << String.isEmpty) [ pendingEx.title, pendingEx.author, pendingEx.text ] then update (GetTime (CreateExcerpt pendingEx)) model else ( { model_ | page = CreatePage pendingEx (values model.books |> map .title |> sort) (values model.authorRouteMap) } , scrollTop ) _ -> ( { model_ | page = NotFoundPage "Route not found." } , none ) SortBooks sort -> ( { model | bookSort = sort, reverseSort = sort /= TitleSort } , none ) SortExcerpts sort -> ( { model | excerptSort = sort , page = case model.page of TitlePage book excerpts _ -> TitlePage book (case sort of ExcerptSemanticSort -> case get book.id model.semanticRankMap of Just ids -> filterMap (\( id, _ ) -> get id model.excerpts ) ids _ -> sortBy .page excerpts _ -> sortBy .page excerpts ) False _ -> model.page } , none ) SetBookmark bookId excerptId -> store ( { model | bookmarks = case get bookId model.bookmarks of Just prevExcerptId -> if prevExcerptId == excerptId then remove bookId model.bookmarks else insert bookId excerptId model.bookmarks _ -> insert bookId excerptId model.bookmarks } , none ) ToggleFavorite excerpt -> let newExcerpt = { excerpt | isFavorite = not excerpt.isFavorite } in store ( { model | excerpts = insert newExcerpt.id newExcerpt model.excerpts , favCountMap = Dict.update newExcerpt.id (Maybe.map ((+) (if newExcerpt.isFavorite then 1 else -1 ) ) ) model.favCountMap , page = case model.page of ExcerptPage _ book -> ExcerptPage newExcerpt book TitlePage book excerpts _ -> TitlePage book (map (\ex -> if ex.id == newExcerpt.id then newExcerpt else ex ) excerpts ) False _ -> model.page } , none ) SetExcerptTab excerpt tab toggle -> let { id } = excerpt in ( { model | idToActiveTab = insert id tab model.idToActiveTab , idToShowDetails = if toggle then Dict.update id (withDefault False >> not >> Just) model.idToShowDetails else model.idToShowDetails } , case tab of Related -> if not (Dict.member id model.neighborMap) && model.embeddingsReady then requestExcerptNeighbors ( id, excerptNeighborK, True ) else none Lenses lens _ -> let lensKey = lensToString lens in if not <| foldl (\( k, v ) acc -> acc || (k == lensKey && not (isEmpty v)) ) False excerpt.lenses then if model.demoMode then fetchLensText id lens else none else none _ -> none ) ScrollToTop -> ( model, scrollToTop () ) HideHoverUiState id -> case model.page of TitlePage book _ _ -> if book.id == id then ( { model | showHoverUi = False }, none ) else noOp _ -> noOp OnSearchStart query -> if String.isEmpty query then ( { model | searchQuery = "" }, Nav.back model.key 1 ) else ( { model | searchQuery = query , page = case model.page of SearchPage _ _ _ _ _ -> model.page _ -> SearchPage query TextMatches [] [] [] } , if String.isEmpty model.searchQuery then Nav.pushUrl model.key (searchToRoute query) else Nav.replaceUrl model.key (searchToRoute query) ) OnSearchEnd val -> let query = trim val ( mode, prevSemantic ) = case model.page of SearchPage _ m _ _ semanticMatches -> ( Just m, Just semanticMatches ) _ -> ( Nothing, Nothing ) in if String.isEmpty query then noOp else if case model.page of SearchPage _ _ _ _ _ -> False _ -> True then noOp else ( { model | page = SearchPage query (withDefault TextMatches mode) (findMatches query (\b -> b.title ++ " " ++ join " " b.authors) (values model.books) ) (model.excerpts |> values |> findMatches query (\e -> e.text ++ " " ++ e.notes) |> sortBy .bookId ) (if String.length query >= minSemanticQueryLen then withDefault [] prevSemantic else [] ) } , if String.length query >= minSemanticQueryLen then requestSemanticSearch ( query, model.semanticThreshold ) else none ) SetSearchTab mode -> case model.page of SearchPage query _ books excerpts semanticMatches -> ( { model | page = SearchPage query mode books excerpts semanticMatches } , none ) _ -> noOp DebounceMsg msg -> let ( debounce, cmd ) = Debounce.update debounceConfig (Debounce.takeLast (\t -> Task.perform OnSearchEnd (Task.succeed t)) ) msg model.searchDebounce in ( { model | searchDebounce = debounce } , cmd ) StartDemo -> if isEmpty model.supportIssues then ( { model | demoMode = True } , Http.get { url = demoJsonPath, expect = Http.expectString GotDemoData } ) else ( { model | modalMessage = Just <| InitErrMsg ("Missing support for " ++ String.join ", " model.supportIssues ) } , none ) GotDemoData result -> case result of Ok text -> update (ParseJsonText False text) model _ -> noOp GotLandingData ( titles, nums ) -> case model.page of LandingPage _ _ -> let books = indexedMap (\i ( title, author ) -> { id = String.fromInt i , title = title , authors = [ author ] , rating = 0 , sortIndex = 0 , tags = [] , slug = "" , notes = "" } ) titles in ( { model | page = LandingPage books (map2 (\{ id } n -> ( id, n )) books nums |> Dict.fromList ) } , none ) _ -> noOp GetTime msg -> ( model, perform msg Time.now ) UpdatePendingExcerpt pExcerpt -> case model.page of CreatePage _ titles authors -> ( { model | page = CreatePage pExcerpt titles authors }, none ) _ -> noOp CreateExcerpt pendingExcerpt time -> let ( excerpt, book ) = makeExcerpt pendingExcerpt.title pendingExcerpt.author pendingExcerpt.text pendingExcerpt.page (time |> posixToMillis |> Just) "" (if pendingExcerpt.sourceUrl |> trim |> String.isEmpty then Nothing else pendingExcerpt.sourceUrl |> trim |> Just ) in case get excerpt.id model.excerpts of Just existingExcerpt -> ( model , Nav.pushUrl model.key (excerptToRoute model.books existingExcerpt) ) _ -> let model_ = { model | neighborMap = Dict.empty , bookNeighborMap = Dict.empty , embeddingsReady = False , excerpts = insert excerpt.id excerpt model.excerpts , excerptCountMap = upsert model.excerptCountMap book.id ((+) 1) 1 } in store (case get book.id model.books of Just _ -> let newBooks = Dict.update book.id (Maybe.map (\b -> { b | sortIndex = max b.sortIndex excerpt.date } ) ) model.books in ( { model_ | books = newBooks , semanticRankMap = remove book.id model.semanticRankMap , tagCounts = getTagCounts newBooks } , Nav.pushUrl model.key (excerptToRoute newBooks excerpt) ) _ -> let ( titleRouteMap, booksWithSlugs ) = insert book.id book model.books |> values |> getTitleRouteMap newBooks = toDict booksWithSlugs in ( { model_ | books = newBooks , titleRouteMap = titleRouteMap , authorRouteMap = getAuthorRouteMap booksWithSlugs } , Nav.pushUrl model.key (excerptToRoute newBooks excerpt) ) ) |> Update.andThen update RequestEmbeddings PendingTitleBlur -> case model.page of CreatePage pExcerpt titles authors -> case model.books |> values |> filter (\book -> book.title == pExcerpt.title) |> head of Just book -> ( { model | page = CreatePage { pExcerpt | author = withDefault "" (head book.authors) } titles authors } , none ) _ -> noOp _ -> noOp UpdateMailingListEmail val -> ( { model | mailingListEmail = val }, none ) SubscribeToMailingList -> if String.isEmpty model.mailingListEmail then noOp else store ( { model | didJoinMailingList = True } , Http.post { url = model.mailingListUrl , body = Http.stringBody "application/x-www-form-urlencoded" (model.mailingListField ++ "=" ++ percentEncode model.mailingListEmail ) , expect = Http.expectWhatever (always NoOp) } ) ReceiveLensText id lensType result -> case result of Ok lensText -> let lensKey = lensToString lensType f = \excerpt -> { excerpt | lenses = ( lensKey, [ lensText ] ) :: excerpt.lenses } in store ( { model | excerpts = Dict.update id (Maybe.map f) model.excerpts , page = case model.page of ExcerptPage excerpt book -> ExcerptPage (f excerpt) book TitlePage book excerpts _ -> TitlePage book (map (\e -> if e.id == id then f e else e ) excerpts ) False _ -> model.page } , none ) _ -> noOp ================================================ FILE: src/Model.elm ================================================ module Model exposing (ModalMsg(..), Model) import Browser.Navigation as Nav import Debounce exposing (Debounce) import Dict exposing (Dict) import Msg exposing (Msg) import Set exposing (Set) import Types exposing ( Author , BookMap , BookSort , CountMap , ExcerptMap , ExcerptSort , ExcerptTab , Id , NeighborMap , Page , Tag , TagSort ) import Url exposing (Url) type alias Model = { page : Page , demoMode : Bool , excerpts : ExcerptMap , books : BookMap , semanticThreshold : Float , neighborMap : NeighborMap , bookNeighborMap : NeighborMap , authorNeighborMap : NeighborMap , semanticRankMap : NeighborMap , hiddenExcerpts : Set Id , completedEmbeddings : Set Id , embeddingsReady : Bool , authorEmbeddingsReady : Bool , titleRouteMap : Dict String Id , authorRouteMap : Dict String Author , excerptCountMap : CountMap , favCountMap : CountMap , tags : List Tag , tagCounts : Dict Tag Int , tagSort : TagSort , showTagHeader : Bool , pendingTag : Maybe Tag , isDragging : Bool , reverseSort : Bool , modalMessage : Maybe ModalMsg , url : Url , key : Nav.Key , bookSort : BookSort , excerptSort : ExcerptSort , bookmarks : Dict Id Id , idToShowDetails : Dict Id Bool , idToActiveTab : Dict Id ExcerptTab , searchQuery : String , searchDebounce : Debounce String , version : String , mailingListUrl : String , mailingListField : String , mailingListEmail : String , didJoinMailingList : Bool , supportIssues : List String , showHoverUi : Bool } type ModalMsg = InfoMsg String | ErrMsg String | InitErrMsg String | ConfirmationMsg String Msg ================================================ FILE: src/Msg.elm ================================================ module Msg exposing (Msg(..)) import Browser exposing (UrlRequest) import Browser.Dom exposing (Element, Error) import Debounce import File exposing (File) import Http import Time exposing (Posix) import Types exposing ( Author , Book , BookMap , BookSort , Excerpt , ExcerptMap , ExcerptSort , ExcerptTab , Id , Lens , PendingExcerpt , ScorePairs , SearchMode , StoredModel , Tag , TagSort ) import Url exposing (Url) type Msg = NoOp | RestoreState (Maybe StoredModel) Bool | MergeNewExcerpts ExcerptMap BookMap | ParseJsonText Bool String | ParseCsvText String | ShowRandom | GotRandomIndex Int | DragEnter | DragLeave | GotFile (String -> Msg) File | GotDroppedFile File | LoadKindleFile String | PickKindleFile | DeleteExcerpt Excerpt | DeleteBook Book | EnterBookEditMode | ExitBookEditMode | SetPendingBookTitle String | SetPendingBookAuthor String | SetBookEdits | UpdateNotes Id String | UpdateBookNotes Id String | SetBookmark Id Id | ToggleFavorite Excerpt | UpdatePendingTag Tag | AddTag | RemoveTag Tag | SetRating Book Float | SetTagSort TagSort | Sort | ToggleTagHeader | ScrollToElement (Result Error Element) | ExportJson | ImportJson | ImportCsv | SyncState StoredModel | ClearModal | ShowConfirmation String Msg | ExportEpub Posix | RequestEmbeddings | ReceiveEmbeddings (List Id) | ReceiveBookEmbeddings | ReceiveAuthorEmbeddings | ReceiveNeighbors ( Id, ScorePairs ) | ReceiveBookNeighbors ( Id, ScorePairs ) | ReceiveAuthorNeighbors ( Author, ScorePairs ) | ReceiveSemanticSearch ( String, ScorePairs ) | ReceiveSemanticRank ( Id, ScorePairs ) | SetSemanticThreshold String | LinkClicked UrlRequest | UrlChanged Url | SortBooks BookSort | SortExcerpts ExcerptSort | SetExcerptTab Excerpt ExcerptTab Bool | ScrollToTop | HideHoverUiState Id | OnSearchStart String | OnSearchEnd String | SetSearchTab SearchMode | ReceiveUnicodeNormalized String | DebounceMsg Debounce.Msg | StartDemo | GotDemoData (Result Http.Error String) | GotLandingData ( List ( String, String ), List Int ) | GetTime (Posix -> Msg) | UpdatePendingExcerpt PendingExcerpt | PendingTitleBlur | CreateExcerpt PendingExcerpt Posix | SubscribeToMailingList | UpdateMailingListEmail String | ReceiveLensText Id Lens (Result Http.Error String) ================================================ FILE: src/Ports.elm ================================================ port module Ports exposing (..) import Types exposing (Author, Id, ScorePairs, StoredModel) port setStorage : StoredModel -> Cmd msg port scrollToTop : () -> Cmd msg port exportJson : StoredModel -> Cmd msg port handleNewExcerpts : StoredModel -> Cmd msg port requestExcerptEmbeddings : List ( Id, String ) -> Cmd msg port receiveExcerptEmbeddings : (List Id -> msg) -> Sub msg port requestBookEmbeddings : List ( Id, List Id ) -> Cmd msg port receiveBookEmbeddings : (() -> msg) -> Sub msg port requestAuthorEmbeddings : List ( Id, List Id ) -> Cmd msg port receiveAuthorEmbeddings : (() -> msg) -> Sub msg port deleteExcerpt : ( Id, ( Id, List Id ), Int ) -> Cmd msg port deleteBook : ( Id, List Id ) -> Cmd msg port requestExcerptNeighbors : ( Id, Int, Bool ) -> Cmd msg port receiveExcerptNeighbors : (( Id, ScorePairs ) -> msg) -> Sub msg port requestBookNeighbors : ( Id, Int ) -> Cmd msg port receiveBookNeighbors : (( Id, ScorePairs ) -> msg) -> Sub msg port requestAuthorNeighbors : ( Author, Int ) -> Cmd msg port receiveAuthorNeighbors : (( Author, ScorePairs ) -> msg) -> Sub msg port requestSemanticRank : ( Id, List Id ) -> Cmd msg port receiveSemanticRank : (( Id, ScorePairs ) -> msg) -> Sub msg port requestUnicodeNormalized : String -> Cmd msg port receiveUnicodeNormalized : (String -> msg) -> Sub msg port requestSemanticSearch : ( String, Float ) -> Cmd msg port receiveSemanticSearch : (( String, ScorePairs ) -> msg) -> Sub msg port fetchDemoEmbeddings : () -> Cmd msg port setDemoEmbeddings : List Id -> Cmd msg port syncState : (StoredModel -> msg) -> Sub msg port initWithClear : StoredModel -> Cmd msg ================================================ FILE: src/Router.elm ================================================ module Router exposing ( Route(..) , authorToRoute , excerptToRoute , routeParser , searchToRoute , tagToRoute , titleSlugToRoute ) import Dict exposing (get) import Types exposing (Author, BookMap, Excerpt, Id, Tag, Title) import Url.Builder exposing (absolute) import Url.Parser exposing ( (>) , (>) , Parser , fragment , map , oneOf , s , string , top ) import Url.Parser.Query as Query import Utils exposing (slugify) type Route = RootRoute | TitleRoute Title (Maybe String) | ExcerptRoute Title Id | AuthorRoute Author | TagRoute Tag | SearchRoute (Maybe String) | SettingsRoute | ImportRoute | MonkRoute | CreateRoute (Maybe String) (Maybe String) (Maybe String) (Maybe String) (Maybe Int) routeParser : Parser (Route -> a) a routeParser = oneOf [ map RootRoute top , map TitleRoute (s "title" > string > fragment identity) , map ExcerptRoute (s "title" > string > string) , map AuthorRoute (s "author" > string) , map TagRoute (s "tag" > string) , map SearchRoute (s "search" > Query.string "q") , map SettingsRoute (s "settings") , map ImportRoute (s "import") , map MonkRoute (s "monk-mode") , map CreateRoute (s "create" > Query.string "title" > Query.string "author" > Query.string "text" > Query.string "sourceUrl" > Query.int "page" ) ] excerptToRoute : BookMap -> Excerpt -> String excerptToRoute books excerpt = case get excerpt.bookId books of Just book -> absolute [ "title", book.slug, excerpt.id ] [] _ -> "" titleSlugToRoute : String -> String titleSlugToRoute slug = absolute [ "title", slug ] [] authorToRoute : Author -> String authorToRoute author = absolute [ "author", slugify author ] [] tagToRoute : Tag -> String tagToRoute tag = absolute [ "tag", slugify tag ] [] searchToRoute : String -> String searchToRoute query = absolute [ "search" ] [ Url.Builder.string "q" query ] ================================================ FILE: src/Types.elm ================================================ module Types exposing ( Author , Book , BookMap , BookSort(..) , CountMap , Excerpt , ExcerptMap , ExcerptSort(..) , ExcerptTab(..) , Id , Lens(..) , NeighborMap , Page(..) , PendingExcerpt , ScorePairs , SearchMode(..) , StoredModel , Tag , TagSort(..) , Title ) import Dict exposing (Dict) type alias Id = String type alias Title = String type alias Author = String type alias Tag = String type alias ScorePairs = List ( Id, Float ) type alias Book = { id : Id , title : Title , authors : List Author , rating : Float , sortIndex : Int , tags : List Tag , slug : String , notes : String } type alias Excerpt = { id : Id , text : String , bookId : Id , date : Int , page : Int , notes : String , isFavorite : Bool , sourceUrl : Maybe String , lenses : List ( String, List String ) } type alias PendingExcerpt = { title : Title , author : Author , text : String , page : Maybe Int , sourceUrl : String } type alias ExcerptMap = Dict Id Excerpt type alias BookMap = Dict Id Book type alias NeighborMap = Dict Id ScorePairs type alias CountMap = Dict Id Int type alias StoredModel = { excerpts : List Excerpt , books : List Book , hiddenExcerpts : List Id , bookmarks : List ( Id, Id ) , semanticThreshold : Float , version : String , didJoinMailingList : Bool } type Page = MainPage (List Book) (Maybe Tag) | SearchPage String SearchMode (List Book) (List Excerpt) ScorePairs | TitlePage Book (List Excerpt) Bool | AuthorPage Author (List Book) | ExcerptPage Excerpt Book | NotFoundPage String | SettingsPage | LandingPage (List Book) CountMap | ImportPage | MonkPage | CreatePage PendingExcerpt (List Title) (List Author) type BookSort = RecencySort | TitleSort | NumSort | RatingSort | FavSort type ExcerptSort = ExcerptPageSort | ExcerptFavSort | ExcerptSemanticSort type TagSort = TagAlphaSort | TagNumSort type ExcerptTab = Related | Lenses Lens Int | Notes | Etc type SearchMode = TextMatches | SemanticMatches type Lens = Succinct | Metaphor ================================================ FILE: src/Utils.elm ================================================ module Utils exposing ( appName , countLabel , dedupe , defaultSemanticThreshold , delay , excerptCountLabel , fetchLensText , findMatches , formatNumber , formatScore , getAuthorRouteMap , getAuthors , getCount , getCounts , getExcerptDomId , getTagCounts , getTitleRouteMap , insertOnce , juxt , lensToString , makeExcerpt , modelToStoredModel , null , ratingEl , removeItem , repoUrl , rx , rx_ , slugify , sortBooks , titleCountLabel , toDict , untaggedKey , upsert ) import Base64 exposing (fromBytes) import Bytes.Encode exposing (encode, sequence, unsignedInt8) import Char exposing (isDigit) import Dict exposing (Dict, empty, get, insert, member, update, values) import Html exposing (Html, div, span, text) import Html.Attributes exposing (class, classList) import Http import List exposing ( all , concatMap , filter , foldl , foldr , isEmpty , length , map , partition , reverse , sortBy , sortWith ) import MD5 exposing (bytes) import Maybe exposing (withDefault) import Model exposing (Model) import Msg exposing (Msg(..)) import Process exposing (sleep) import Regex exposing (Match, Regex, replace) import Set exposing (Set) import String exposing (fromInt, join, split, toLower, trim) import Task import Types exposing ( Author , Book , BookMap , BookSort(..) , CountMap , Excerpt , Id , Lens(..) , StoredModel , Tag ) appName : String appName = "Emdash" repoUrl : String repoUrl = "https://github.com/dmotz/emdash" untaggedKey : String untaggedKey = "untagged" defaultSemanticThreshold : Float defaultSemanticThreshold = 0.3 inc : Int -> Int inc = (+) 1 rx : String -> Regex rx = Regex.fromString >> withDefault Regex.never rx_ : String -> Regex rx_ = Regex.fromStringWith { caseInsensitive = True, multiline = False } >> withDefault Regex.never formatNumber : Int -> String formatNumber = fromInt >> replace (rx "\\B(?=(\\d{3})+(?!\\d))") (always ",") asSet : (comparable -> Set comparable -> Set comparable) -> List comparable -> comparable -> List comparable asSet f xs x = xs |> Set.fromList |> f x |> Set.toList insertOnce : List comparable -> comparable -> List comparable insertOnce = asSet Set.insert removeItem : List comparable -> comparable -> List comparable removeItem = asSet Set.remove dedupe : List comparable -> List comparable dedupe = Set.fromList >> Set.toList modelToStoredModel : Model -> StoredModel modelToStoredModel model = { excerpts = values model.excerpts , books = values model.books , hiddenExcerpts = Set.toList model.hiddenExcerpts , bookmarks = Dict.toList model.bookmarks , semanticThreshold = model.semanticThreshold , version = model.version , didJoinMailingList = model.didJoinMailingList } juxt : (a -> b) -> (a -> c) -> a -> ( b, c ) juxt f g x = ( f x, g x ) toDict : List { a | id : comparable } -> Dict comparable { a | id : comparable } toDict = map (juxt .id identity) >> Dict.fromList upsert : Dict comparable a -> comparable -> (a -> a) -> a -> Dict comparable a upsert dict id f default = if member id dict then update id (Maybe.map f) dict else insert id default dict phraseMatch : Regex -> (a -> String) -> a -> Bool phraseMatch regex accessor x = x |> accessor |> toLower |> Regex.contains regex findMatches : String -> (a -> String) -> List a -> List a findMatches query accessor xs = let ( phraseMatches, rest ) = partition (phraseMatch (rx_ <| "\\b" ++ query) accessor) xs wordsRx = "^" ++ (split " " query |> map (\word -> "(?=.*\\b" ++ word ++ ")") |> String.concat ) ++ ".*$" |> rx_ in phraseMatches ++ filter (\x -> Regex.contains wordsRx (toLower (accessor x))) rest getTagCounts : BookMap -> Dict Tag Int getTagCounts bookMap = let books = values bookMap in books |> concatMap .tags |> foldl (\tag acc -> update tag (withDefault 0 >> inc >> Just) acc) empty |> insert untaggedKey (books |> filter (.tags >> isEmpty) |> length) getExcerptDomId : Id -> String getExcerptDomId = (++) "excerpt-" countLabel : String -> Int -> String countLabel label n = formatNumber n ++ " " ++ label ++ (if n == 1 then "" else "s" ) formatScore : Float -> Html msg formatScore = (*) 100 >> round >> fromInt >> (\s -> s ++ "%") >> text excerptCountLabel : Int -> String excerptCountLabel = countLabel "excerpt" titleCountLabel : Int -> String titleCountLabel = countLabel "title" normalizeTitle : String -> String normalizeTitle = toLower >> replace (rx "^(the )") (always "") sortBooks : BookSort -> Bool -> CountMap -> CountMap -> List Book -> List Book sortBooks sort reverseSort exCounts favCounts = (case sort of RecencySort -> sortBy .sortIndex TitleSort -> sortWith (\a b -> compare (a |> .title |> normalizeTitle) (b |> .title |> normalizeTitle) ) NumSort -> sortBy <| .id >> getCount exCounts RatingSort -> sortBy .rating FavSort -> sortBy <| .id >> getCount favCounts ) >> (if reverseSort then reverse else identity ) ratingEl : Book -> Html msg ratingEl book = let baseInt = truncate book.rating in div [ classList [ ( "ratingNum", True ), ( "unrated", book.rating == 0 ) ] ] (if book.rating == 0 then [ text "—" ] else if ceiling book.rating > baseInt then [ if baseInt == 0 then null else book.rating |> truncate |> fromInt |> text , span [ class "half" ] [ text "1/2" ] ] else [ text (String.fromFloat book.rating) ] ) null : Html msg null = text "" getTitleRouteMap : List Book -> ( Dict String Id, List Book ) getTitleRouteMap = sortBy .sortIndex >> foldl (\book ( slugToId, newBooks ) -> let slug = case get (slugify book.title) slugToId of Just _ -> slugify (book.title ++ " by " ++ join " & " book.authors ) _ -> slugify book.title in ( insert slug book.id slugToId , { book | slug = slug } :: newBooks ) ) ( Dict.empty, [] ) getAuthorRouteMap : List Book -> Dict String Author getAuthorRouteMap = concatMap (.authors >> map (juxt slugify identity)) >> Dict.fromList slugify : String -> String slugify = replace (rx "\\s") (always "-") >> replace (rx "[^\\w-]") (always "") apostropheRx : Regex apostropheRx = rx "(\\w)(')(\\w)" apostropheReplacer : Match -> String apostropheReplacer match = String.concat <| map (\sub -> let s = withDefault "" sub in if s == "'" then "’" else s ) match.submatches replaceApostrophes : String -> String replaceApostrophes = replace apostropheRx apostropheReplacer authorSplitRx : Regex authorSplitRx = rx "[;&]|\\sand\\s" footnoteRx : Regex footnoteRx = rx "([^\\s\\d]{2,})(\\d+)" footnoteReplacer : Match -> String footnoteReplacer match = String.concat <| map (\sub -> let s = withDefault "" sub in if all isDigit (String.toList s) then "" else s ) match.submatches hashId : String -> Id hashId = bytes >> map unsignedInt8 >> sequence >> encode >> fromBytes >> withDefault "" >> String.replace "==" "" >> String.replace "+" "-" >> String.replace "/" "_" getBookId : String -> List String -> Id getBookId title authors = hashId (title ++ join " / " authors) getExcerptId : String -> String -> Int -> Id getExcerptId text bookId page = hashId (text ++ bookId ++ String.fromInt page) makeExcerpt : String -> String -> String -> Maybe Int -> Maybe Int -> String -> Maybe String -> ( Excerpt, Book ) makeExcerpt titleRaw authorRaw excerptText mPage mDate notes mUrl = let title = replaceApostrophes titleRaw authors = authorRaw |> replaceApostrophes |> Regex.split authorSplitRx |> map trim page = withDefault -1 mPage bookId = getBookId title authors date = withDefault 0 mDate in ( { id = getExcerptId excerptText bookId page , text = replace footnoteRx footnoteReplacer excerptText , bookId = bookId , date = date , page = page , notes = notes , isFavorite = False , sourceUrl = mUrl , lenses = [] } , { id = bookId , title = title , authors = authors , rating = 0 , sortIndex = date , tags = [] , slug = "" , notes = "" } ) getAuthors : Model -> List ( Id, List Id ) getAuthors model = model.excerpts |> Dict.foldl (\_ excerpt acc -> foldl (\author acc2 -> insert author (excerpt.id :: withDefault [] (get author acc2) ) acc2 ) acc (withDefault [] (get excerpt.bookId model.books |> Maybe.map .authors) ) ) Dict.empty |> Dict.toList getCounts : List Excerpt -> ( CountMap, CountMap ) getCounts = foldr (\{ bookId, isFavorite } ( exCount, favCount ) -> ( upsert exCount bookId inc 1 , if isFavorite then upsert favCount bookId inc 1 else favCount ) ) ( Dict.empty, Dict.empty ) getCount : CountMap -> Id -> Int getCount dict id = get id dict |> withDefault 0 lensToString : Lens -> String lensToString lens = case lens of Succinct -> "succinct" Metaphor -> "metaphor" fetchLensText : Id -> Lens -> Cmd Msg fetchLensText id lens = Http.get { url = "/demo/lenses/" ++ id ++ "-" ++ lensToString lens ++ ".txt" , expect = Http.expectString (ReceiveLensText id lens) } delay : Float -> a -> Cmd a delay ms msg = Task.perform (always msg) (sleep ms) ================================================ FILE: src/Views/AuthorInfo.elm ================================================ module Views.AuthorInfo exposing (authorInfo) import Dict exposing (get) import Html exposing (Html, a, div, h1, h2, h5, li, text, ul) import Html.Attributes exposing (class, href) import List exposing (foldl, length, map) import Msg exposing (Msg) import Router exposing (authorToRoute) import Types exposing (Book, CountMap, NeighborMap) import Utils exposing (excerptCountLabel, getCount, titleCountLabel) authorInfo : String -> List Book -> NeighborMap -> CountMap -> Html Msg authorInfo author books neighborMap countMap = div [ class "authorInfo" ] [ h1 [] [ text author ] , h2 [] [ titleCountLabel (length books) ++ ", " ++ (books |> foldl (\{ id } acc -> acc + getCount countMap id) 0 |> excerptCountLabel ) |> text ] , div [ class "related" ] [ h5 [] [ text "Related: " ] , ul [] (case get author neighborMap of Just ids -> map (\( neighbor, _ ) -> li [] [ a [ href <| authorToRoute neighbor ] [ text neighbor ] ] ) ids _ -> [ li [ class "wait" ] [ text "…" ] ] ) ] ] ================================================ FILE: src/Views/Base.elm ================================================ module Views.Base exposing (view) -- import Regex import Dict exposing (Dict, get, size) import Html exposing ( Html , a , aside , br , button , code , div , footer , h2 , h3 , h4 , hr , img , li , main_ , p , span , sup , text , ul ) import Html.Attributes exposing (alt, class, classList, draggable, href, id, src, target) import Html.Events exposing (onClick) import Html.Keyed as Keyed import List exposing (filter, isEmpty, length, map, reverse, sortBy) import Maybe exposing (withDefault) import Model exposing (ModalMsg(..), Model) import Msg exposing (Msg(..)) import Router exposing (tagToRoute) import Set import String exposing (join) import Types exposing ( BookMap , BookSort(..) , ExcerptSort(..) , ExcerptTab(..) , Lens(..) , Page(..) , Tag , TagSort(..) ) import Utils exposing (appName, formatNumber, getCount, null, repoUrl, untaggedKey) import Views.AuthorInfo exposing (authorInfo) import Views.BookInfo exposing (bookInfo) import Views.BookList exposing (bookList) import Views.Button exposing (actionButton) import Views.Create exposing (createView) import Views.EmbeddingProgress exposing (embeddingProgress) import Views.Excerpt exposing (excerptView) import Views.ExcerptList exposing (excerptList) import Views.Import exposing (importView) import Views.Landing exposing (landingView) import Views.MonkSignup exposing (monkSignup) import Views.SearchInput exposing (searchInput) import Views.SearchResults exposing (searchResults) import Views.Settings exposing (settingsView) import Views.Toolbar exposing (toolbar) view : Model -> Html Msg view model = div [ id "root" ] ((case model.page of LandingPage books countMap -> [ landingView books countMap model.didJoinMailingList ] _ -> [ a [ class "logo", href "/" ] [ img [ src "/images/logo.svg", draggable "false", alt appName ] [] , case model.page of MainPage _ _ -> null _ -> div [ class "hint" ] [ text "Back to the index" ] ] , toolbar , if model.demoMode && model.page /= ImportPage then div [ class "demoNotice" ] [ aside [] [ text "Feel free to peruse this sample library. Make yourself at home." , br [] [] , button [ onClick ShowRandom ] [ text "Try viewing a random excerpt." ] ] , div [] [ span [] [ text "❧" ] , a [ href "/import" ] [ text <| "Ready to use " ++ appName ++ " with your own collection?" ] ] ] else null , main_ [] [ searchInput model.searchQuery , let completedCount = Set.size model.completedEmbeddings totalCount = size model.excerpts progressView = if model.embeddingsReady || completedCount == 0 || completedCount >= totalCount then Nothing else Just <| embeddingProgress completedCount totalCount in case model.page of MainPage books mTag -> div [ class "fullWidth" ] [ tagHeader (mTag /= Nothing || model.showTagHeader) model.books model.tagSort model.tags model.tagCounts mTag , bookSorter model.bookSort model.reverseSort , bookList books model.excerptCountMap model.favCountMap model.bookSort model.reverseSort ] SearchPage query mode books excerpts semanticMatches -> div [ class "searchPage fullWidth" ] [ searchResults mode model.books model.excerpts books excerpts semanticMatches model.excerptCountMap model.favCountMap query ] TitlePage book excerpts editMode -> let excerpts_ = if model.excerptSort == ExcerptFavSort then filter .isFavorite excerpts else excerpts in div [ classList [ ( "showHints", model.showHoverUi ) ] ] [ bookInfo book model.books model.tags model.pendingTag model.bookNeighborMap (getCount model.excerptCountMap book.id) (get book.id model.bookmarks) model.excerptSort progressView editMode , if isEmpty excerpts_ then div [ class "noFav" ] [ text <| "No " ++ (if model.excerptSort == ExcerptFavSort then "favorites" else "excerpts" ) ++ " yet" ] else null , excerptList excerpts_ model.excerpts model.books model.neighborMap model.idToShowDetails model.idToActiveTab model.demoMode (get book.id model.bookmarks |> withDefault "" ) progressView ] AuthorPage author books -> div [] [ authorInfo author books model.authorNeighborMap model.excerptCountMap , bookSorter model.bookSort model.reverseSort , bookList books model.excerptCountMap model.favCountMap model.bookSort model.reverseSort ] ExcerptPage excerpt _ -> div [] [ ul [ class "excerpts" ] [ excerptView model.excerpts model.books (withDefault [] (get excerpt.id model.neighborMap ) ) True (withDefault (if model.demoMode then Lenses Succinct 0 else Related ) (get excerpt.id model.idToActiveTab ) ) model.demoMode -1 True False progressView excerpt ] ] NotFoundPage msg -> div [ class "notFound" ] [ h2 [] [ text "Alas!" ] , h3 [] [ text msg ] , a [ href "/" ] [ text "Return to the index." ] ] SettingsPage -> settingsView model.version (size model.excerpts) (size model.books) (size model.authorRouteMap) (length model.tags) model.semanticThreshold ImportPage -> importView (model.demoMode || Dict.isEmpty model.excerpts ) model.isDragging CreatePage pExcerpt books authors -> createView pExcerpt books authors MonkPage -> monkSignup model.didJoinMailingList _ -> null ] ] ) ++ footer [] [ div [ class "links" ] [ a [ href "/import" ] [ text "Import excerpts" ] , if Dict.isEmpty model.excerpts then null else a [ href "/settings" ] [ text "Settings" ] , a [ href <| repoUrl ++ "/issues" , target "_blank" ] [ text "Report a bug" ] , a [ href repoUrl , target "_blank" ] [ text "Source code" ] ] , div [ class "fleuron" ] [ text "❦" ] ] :: (case model.modalMessage of Just msgType -> [ div [ class "modal" ] [ div [ class "modalBox" ] (case msgType of ErrMsg msg -> [ p [] [ text "An error occurred parsing the file:" ] , div [ class "error" ] [ code [] [ text msg ] ] , actionButton [ onClick ClearModal ] [ text "Dismiss" ] ] InitErrMsg msg -> [ h4 [] [ text "This is awkward…" ] , p [] [ text <| appName ++ " uses some very new web features that your browser doesnʼt support." ++ " Please update your browser/OS to the latest version and try again." ] , br [] [] , p [] [ text "Details:" ] , div [ class "error" ] [ code [] [ text msg ] ] , actionButton [ onClick ClearModal ] [ text "Understood" ] ] InfoMsg msg -> [ p [] [ text msg ] , actionButton [ onClick ClearModal ] [ text "OK" ] ] ConfirmationMsg msg onConfirm -> [ p [] [ text msg ] , div [ class "confirm" ] [ actionButton [ class "okButton" , onClick onConfirm ] [ text "Yes, delete" ] , actionButton [ onClick ClearModal ] [ text "No, cancel" ] ] ] ) ] ] _ -> [] ) ) bookSorter : BookSort -> Bool -> Html Msg bookSorter activeSort reverseSort = div [ class "modeHeading center" ] [ ul [] (map (\sort -> li [ classList [ ( "active", sort == activeSort ) ] ] [ button [ onClick <| SortBooks sort ] [ span [] [ text <| sortToString sort ] ] , if sort == activeSort then button [ onClick Sort, class "sorter" ] [ span [] [ span [ classList [ ( "arrow", True ) , ( "reverse", reverseSort ) ] ] [ text "▲" ] , activeSort |> sortToBounds |> (if reverseSort then reverse else identity ) |> join "–" |> text ] ] else null ] ) [ RecencySort, TitleSort, RatingSort, NumSort, FavSort ] ) ] tagHeader : Bool -> BookMap -> TagSort -> List Tag -> Dict Tag Int -> Maybe Tag -> Html Msg tagHeader show allBooks tagSort tags tagCounts mActiveTag = div [ class "tagHeader" ] [ div [ class "tabs" ] [ button [ onClick ToggleTagHeader, class "active" ] [ text "Tags" ] ] , if show then div [] [ ul [ class "modeHeading" ] (map (\sort -> li [ classList [ ( "active", sort == tagSort ) ] ] [ button [ onClick <| SetTagSort sort ] [ span [] [ text <| case sort of TagAlphaSort -> "A–Z" TagNumSort -> "№ titles" ] ] ] ) [ TagAlphaSort, TagNumSort ] ) , Keyed.ul [ class "tags" ] (map (\tag -> ( tag , li [ class "tag" , classList [ ( "active" , case mActiveTag of Just t -> tag == t _ -> tag == allBooksKey ) , ( "special" , tag == allBooksKey || tag == untaggedKey ) ] ] [ a [ href <| if tag == allBooksKey then "/" else tagToRoute tag ] [ text tag ] , if tagSort == TagNumSort then sup [ class "count" ] [ text <| if tag == allBooksKey then allBooks |> size |> formatNumber else get tag tagCounts |> withDefault 0 |> formatNumber ] else null ] ) ) ([ allBooksKey, untaggedKey ] ++ (if tagSort == TagNumSort then tags |> sortBy (\tag -> get tag tagCounts |> withDefault 0 ) |> reverse else tags ) ) ) ] else null , hr [] [] ] allBooksKey : String allBooksKey = "all" sortToString : BookSort -> String sortToString sort = case sort of RecencySort -> "Recent" TitleSort -> "Title" RatingSort -> "Rating" NumSort -> "№ excerpts" FavSort -> "№ favorites" sortToBounds : BookSort -> List String sortToBounds sort = case sort of RecencySort -> [ "older", "newer" ] TitleSort -> [ "A", "Z" ] RatingSort -> [ "worse", "better" ] _ -> [ "less", "more" ] ================================================ FILE: src/Views/BookInfo.elm ================================================ module Views.BookInfo exposing (bookInfo) import Dict exposing (get) import Html exposing ( Html , a , br , button , details , div , em , form , h1 , h2 , h5 , input , li , section , span , summary , text , textarea , ul ) import Html.Attributes as H exposing ( class , classList , href , placeholder , spellcheck , step , style , target , type_ , value ) import Html.Events exposing (onClick, onInput, onSubmit) import Html.Keyed as Keyed import List exposing (indexedMap, intersperse, map, repeat) import Maybe exposing (withDefault) import Msg exposing (Msg(..)) import Router exposing (authorToRoute, titleSlugToRoute) import String exposing (fromInt, join) import Types exposing (Book, BookMap, ExcerptSort(..), Id, NeighborMap, Tag) import Utils exposing ( excerptCountLabel , formatScore , getExcerptDomId , null , ratingEl ) import Views.Button exposing (actionButton) import Views.TagSection exposing (tagSection) bookInfo : Book -> BookMap -> List Tag -> Maybe Tag -> NeighborMap -> Int -> Maybe Id -> ExcerptSort -> Maybe (Html Msg) -> Bool -> Html Msg bookInfo book books tags pendingTag bookNeighborMap count mBookmark excerptSort progressView editMode = div [ class "bookInfo" ] [ h1 [] [ text book.title ] , h2 [] ((map (\author -> a [ href <| authorToRoute author ] [ text author ]) book.authors |> intersperse (text " / ") ) ++ [ text <| " — " ++ excerptCountLabel count ] ) , section [ class "bookMeta" ] [ div [ class "col" ] [ h5 [] [ text "Related" ] , case progressView of Just view -> view _ -> Keyed.ul [ class "related" ] (case get book.id bookNeighborMap of Just ids -> indexedMap (\i ( id, score ) -> ( book.id ++ id , case get id books of Just neighbor -> li [ style "animation-delay" (fromInt (i * 99) ++ "ms") ] [ a [ class "title" , href <| titleSlugToRoute neighbor.slug ] [ text neighbor.title ] , span [ class "score" ] [ formatScore score , div [ class "hint" ] [ text "Similarity score" ] ] ] _ -> null ) ) ids _ -> li [ class "wait" ] [ text "…" ] :: repeat 5 (li [] [ br [] [] ]) |> indexedMap (\i el -> ( fromInt i, el )) ) ] , div [ class "col" ] [ div [ class "tagsRating" ] [ tagSection book.tags tags pendingTag , div [] [ h5 [] [ text "Rating" ] , div [ class "rating" ] [ ratingEl book , input [ type_ "range" , H.min "0" , H.max "5" , step "0.5" , value <| String.fromFloat book.rating , onInput <| String.toFloat >> withDefault 0 >> SetRating book ] [] ] ] ] , details [] [ summary [] [ h5 [] [ text "Edit" , if book.notes /= "" then span [] [ em [] [ text " &" ], text " view notes" ] else null ] ] , if editMode then form [ class "editTitle", onSubmit SetBookEdits ] [ input [ value book.title , onInput SetPendingBookTitle , placeholder "Title" , spellcheck False ] [] , input [ value <| join "/" book.authors , onInput SetPendingBookAuthor , placeholder "Author" , spellcheck False ] [] , div [] [ actionButton [] [ text "Save" ] , actionButton [ onClick ExitBookEditMode ] [ text "Cancel" ] ] ] else div [] [ actionButton [ onClick EnterBookEditMode ] [ text "Edit title / author" ] , actionButton [ onClick <| ShowConfirmation "Delete this title and all of its excerpts?" (DeleteBook book) ] [ text "Delete" ] , textarea [ placeholder "Add notes about this title here. Perhaps your review?" , value book.notes , onInput <| UpdateBookNotes book.id ] [] ] ] ] ] , div [ class "actions" ] [ div [ class "modeHeading" ] [ ul [] (map (\sort -> li [ classList [ ( "active", sort == excerptSort ) ] ] [ button [ onClick <| SortExcerpts sort ] [ span [] [ text <| sortToString sort , if sort == ExcerptSemanticSort && excerptSort /= ExcerptSemanticSort then div [ class "hint" ] [ text "Sort by most semantically relevant to all passages" ] else null ] ] ] ) [ ExcerptPageSort, ExcerptFavSort, ExcerptSemanticSort ] ) ] , case mBookmark of Just id -> a [ href <| "#" ++ getExcerptDomId id, target "_self" ] [ text "↧ Jump to last read excerpt" ] _ -> null ] ] sortToString : ExcerptSort -> String sortToString sort = case sort of ExcerptPageSort -> "Page №" ExcerptFavSort -> "Favorites" ExcerptSemanticSort -> "Relevance" ================================================ FILE: src/Views/BookList.elm ================================================ module Views.BookList exposing (bookList, bookView) import Html exposing (Html, a, div, img, li, span, text) import Html.Attributes exposing (alt, class, href, src, tabindex) import Html.Keyed as Keyed import List exposing (map) import Msg exposing (Msg) import Router exposing (titleSlugToRoute) import String exposing (fromInt, join) import Types exposing (Book, BookSort(..), CountMap) import Utils exposing (getCount, null, ratingEl, sortBooks) bookList : List Book -> CountMap -> CountMap -> BookSort -> Bool -> Html Msg bookList books exCounts favCounts sort reverseSort = let showRating = sort == RatingSort showFavCount = sort == FavSort in Keyed.ul [ class "bookList" ] (map (\book -> ( book.id , bookView book (getCount exCounts book.id) (getCount favCounts book.id) showRating showFavCount False ) ) (sortBooks sort reverseSort exCounts favCounts books) ) bookView : Book -> Int -> Int -> Bool -> Bool -> Bool -> Html Msg bookView book exCount favCount showRating showFavCount isLandingPage = (if isLandingPage then div else li ) [ class "book" ] [ a (if isLandingPage then [ href "#", tabindex -1 ] else [ href <| titleSlugToRoute book.slug ] ) [ div [ class "title" ] [ text book.title ] , div [ class "author" ] [ text <| join " / " book.authors ] , div [ class "count" ] [ text <| fromInt exCount ] , if showRating then ratingEl book else null , if showFavCount then div [ class "favCount" ] (if favCount > 0 then [ img [ class "icon" , src "/images/icons/favorite.svg" , alt "favorites" ] [] , text <| fromInt favCount ] else [ span [ class "unrated" ] [ text "—" ] ] ) else null ] ] ================================================ FILE: src/Views/Button.elm ================================================ module Views.Button exposing (actionButton) import Html exposing (button, div) import Html.Attributes exposing (class) actionButton : List (Html.Attribute msg) -> List (Html.Html msg) -> Html.Html msg actionButton attrs children = button (class "actionButton" :: attrs) [ div [ class "buttonContent" ] children , div [ class "buttonShadow" ] [] ] ================================================ FILE: src/Views/Citation.elm ================================================ module Views.Citation exposing (citation) import Html exposing (Html, a, cite, div, span, text) import Html.Attributes exposing (class, href) import Html.Events exposing (stopPropagationOn) import Json.Decode exposing (succeed) import List exposing (intersperse, map) import Msg exposing (Msg(..)) import Router exposing (authorToRoute, titleSlugToRoute) import String exposing (fromInt) import Types exposing (Book, Excerpt) import Utils exposing (formatScore, getExcerptDomId) citation : Excerpt -> Book -> Maybe Float -> Html Msg citation excerpt book mScore = cite [] ([ div [ class "title" ] [ a [ href <| titleSlugToRoute book.slug, stopLinkProp ] [ text book.title ] , span [ class "divider" ] [ text "•" ] ] , div [ class "author" ] ((map (\author -> a [ href <| authorToRoute author, stopLinkProp ] [ text author ] ) book.authors |> intersperse (text " / ") ) ++ (if excerpt.page /= -1 then [ span [ class "divider" ] [ text "•" ] ] else [] ) ) ] ++ (if excerpt.page /= -1 then [ a [ href <| titleSlugToRoute book.slug ++ "#" ++ getExcerptDomId excerpt.id , stopLinkProp , class "page" ] [ text <| "p. " ++ fromInt excerpt.page ] ] else [] ) ++ (case mScore of Just score -> [ div [ class "score" ] [ span [] [ formatScore score ] , div [ class "hint" ] [ text "Similarity score" ] ] ] _ -> [] ) ) stopLinkProp : Html.Attribute Msg stopLinkProp = stopPropagationOn "click" (succeed ( NoOp, True )) ================================================ FILE: src/Views/Create.elm ================================================ module Views.Create exposing (createView) import Html exposing ( Html , a , aside , datalist , div , em , form , h1 , input , label , option , span , text , textarea ) import Html.Attributes as H exposing ( class , disabled , href , id , list , placeholder , spellcheck , type_ , value ) import Html.Events exposing (onBlur, onClick, onInput) import List exposing (map) import Msg exposing (Msg(..)) import String exposing (isEmpty) import Types exposing (Author, PendingExcerpt, Title) import Views.Button exposing (actionButton) createView : PendingExcerpt -> List Title -> List Author -> Html Msg createView pendingExcerpt titles authors = div [ class "createPage" ] [ h1 [] [ text "Create a new excerpt" ] , aside [] [ text "To create excerpts in bulk, visit the " , a [ href "/import" ] [ text "import page" ] , text "." ] , form [] [ label [] [ textarea [ value pendingExcerpt.text , onInput (\s -> UpdatePendingExcerpt { pendingExcerpt | text = s } ) , spellcheck False , placeholder "Paste excerpt text here" ] [] , text "Excerpt text" ] , let listId = "titleList" in label [] [ input [ value pendingExcerpt.title , list listId , onInput (\s -> UpdatePendingExcerpt { pendingExcerpt | title = s } ) , onBlur PendingTitleBlur , spellcheck False ] [] , text "Book/article/essay title" , datalist [ id listId ] (map (\t -> option [ value t ] []) titles) ] , let listId = "authorList" in label [] [ input [ value pendingExcerpt.author , list listId , onInput (\s -> UpdatePendingExcerpt { pendingExcerpt | author = s } ) , spellcheck False ] [] , text "Author name" , datalist [ id listId ] (map (\t -> option [ value t ] []) authors) ] , div [] [ label [] [ input [ type_ "number" , H.min "0" , value <| case pendingExcerpt.page of Just page -> if page > -1 then String.fromInt page else "" _ -> "" , onInput (\s -> UpdatePendingExcerpt { pendingExcerpt | page = String.toInt s } ) ] [] , div [] [ text "Page № " , em [] [ text "optional" ] ] ] , label [] [ input [ type_ "url", value pendingExcerpt.sourceUrl ] [] , div [] [ text "Source " , span [ class "smallCaps" ] [ text "url" ] , em [] [ text "optional" ] ] ] ] ] , actionButton [ onClick <| GetTime (CreateExcerpt pendingExcerpt) , disabled <| isEmpty pendingExcerpt.text || isEmpty pendingExcerpt.title || isEmpty pendingExcerpt.author ] [ text "Create" ] ] ================================================ FILE: src/Views/EmbeddingProgress.elm ================================================ module Views.EmbeddingProgress exposing (embeddingProgress) import Html exposing (Html, div, p, progress, text) import Html.Attributes exposing (class, value) import Msg exposing (Msg) import String exposing (fromFloat) import Utils exposing (formatNumber) embeddingProgress : Int -> Int -> Html Msg embeddingProgress done total = div [ class "embeddingProgress" ] [ p [] [ text "Analyzing new excerpts…" ] , div [] [ progress [ Html.Attributes.max "1" , value <| fromFloat (toFloat done / toFloat total) ] [] ] , text <| formatNumber done ++ " / " ++ formatNumber total ] ================================================ FILE: src/Views/Excerpt.elm ================================================ module Views.Excerpt exposing (excerptView) import Dict exposing (get) import Html exposing ( Html , a , blockquote , button , details , div , em , figcaption , figure , hr , img , li , p , section , span , summary , text , textarea , ul ) import Html.Attributes exposing ( alt , class , classList , draggable , href , id , placeholder , src , style , value ) import Html.Events exposing (onClick, onInput) import Html.Keyed exposing (node) import Html.Lazy exposing (lazy5) import List exposing (foldl, head, indexedMap, isEmpty, map) import Maybe exposing (andThen) import Msg exposing (Msg(..)) import Router exposing (excerptToRoute) import String exposing (endsWith, fromInt, split) import Tuple exposing (first) import Types exposing ( BookMap , Excerpt , ExcerptMap , ExcerptTab(..) , Lens(..) , ScorePairs ) import Utils exposing (getExcerptDomId, lensToString, null) import Views.Button exposing (actionButton) import Views.Citation exposing (citation) import Views.Snippet exposing (snippetView) excerptView : ExcerptMap -> BookMap -> ScorePairs -> Bool -> ExcerptTab -> Bool -> Int -> Bool -> Bool -> Maybe (Html Msg) -> Excerpt -> Html Msg excerptView excerpts books neighbors showDetails activeTab showLensTab i perma isMarked mProgress excerpt = li [ classList [ ( "excerpt", True ), ( "permalink", perma ) ] , id <| getExcerptDomId excerpt.id ] [ figure [] [ figcaption [ class "meta" ] [ if perma then null else div [] [ text <| fromInt (i + 1) ] , button [ onClick (ToggleFavorite excerpt) , classList [ ( "favorite", True ) , ( "active", excerpt.isFavorite ) ] ] [ img [ class "icon" , draggable "false" , src <| "/images/icons/favorite" ++ (if excerpt.isFavorite then "-filled" else "" ) ++ ".svg" , alt <| (if excerpt.isFavorite then "remove" else "add" ) ++ " favorite" ] [] , div [ class "hint left swapRight" ] [ text <| (if excerpt.isFavorite then "Unmark" else "Mark" ) ++ " as favorite" ] ] , if perma then null else button [ classList [ ( "bookmark", True ) , ( "active", isMarked ) ] , onClick (SetBookmark excerpt.bookId excerpt.id) ] [ img [ class "icon" , draggable "false" , src <| "/images/icons/bookmark" ++ (if isMarked then "-filled" else "" ) ++ ".svg" , alt <| (if isMarked then "remove" else "add" ) ++ " bookmark" ] [] , div [ class "hint left swapRight" ] [ text <| (if isMarked then "Unmark" else "Mark" ) ++ " as last reviewed" ] ] , if perma then null else a [ class "page", href <| excerptToRoute books excerpt ] [ text <| if excerpt.page == -1 then "¶" else "p. " ++ fromInt excerpt.page , div [ class "hint left swapRight" ] [ text "Permalink" ] ] ] , blockquote [] [ text excerpt.text ] , if perma then case get excerpt.bookId books of Just book -> citation excerpt book Nothing _ -> null else null , div [ classList [ ( "tabs", True ), ( "active", showDetails ) ] ] (map (\tab -> button [ onClick <| SetExcerptTab excerpt tab (not perma && (not showDetails || tab == activeTab) ) , classList [ ( "active" , showDetails && tab == activeTab ) ] ] [ text <| case tab of Related -> "Related" Lenses _ _ -> "Lenses" Notes -> "Notes" ++ (if String.isEmpty excerpt.notes then "" else " °" ) Etc -> "&c." ] ) ((if showLensTab then [ case activeTab of Lenses lensType index -> Lenses lensType index _ -> Lenses Succinct 0 ] else [] ) ++ [ Related , Notes , Etc ] ) ) , if showDetails then div [ class "details" ] [ case activeTab of Related -> section [ class "relatedExcerpts" ] [ case mProgress of Just progressView -> progressView _ -> if isEmpty neighbors then null else ul [ class "neighbors" ] (indexedMap (\n ( id, score ) -> case get id excerpts of Just neighbor -> lazy5 snippetView books (Just score) Nothing n neighbor _ -> null ) neighbors ) ] Lenses activeLens lensIndex -> section [ class "lenses" ] [ div [ class "modeHeading" ] [ ul [] (map (\lens -> li [ classList [ ( "active" , lens == activeLens ) ] ] [ button [ onClick <| SetExcerptTab excerpt (Lenses lens lensIndex) False ] [ span [] (case lens of Succinct -> [ img [ class "icon" , src "/images/icons/succinct.svg" , alt "" ] [] , text "Succinct" ] Metaphor -> [ img [ class "icon" , src "/images/icons/metaphor.svg" , alt "" ] [] , text "Metaphor" ] ) ] ] ) [ Succinct, Metaphor ] ) ] , div [] (let lensString = lensToString activeLens in case excerpt.lenses |> Dict.fromList |> get lensString |> andThen head of Just lensText -> [ node "p" [ class "lensText" ] (foldl (\c ( xs, ms ) -> ( xs ++ [ ( excerpt.id ++ lensString ++ fromInt ms , span [ style "animation-delay" (fromInt ms ++ "ms") ] [ text <| c ++ " " ] ) ] , ms + 33 + (if endsWith "." c then 333 else if endsWith "," c then 133 else 0 ) ) ) ( [], 333 ) (split " " lensText) |> first ) , details [] [ summary [] [ text "Lenses?" ] , p [] [ text "Lenses are an upcoming feature of " , a [ href "/monk-mode" ] [ em [] [ text "Monk-Mode" ] ] , text " which uses generative AI to rephrase and summarize ideas." ] ] ] _ -> [ p [ class "loading" ] [ text "Loading…" ] ] ) ] Notes -> section [ class "notes" ] [ textarea [ onInput <| UpdateNotes excerpt.id , value excerpt.notes , placeholder "Add notes here" ] [ text excerpt.notes ] ] Etc -> section [] [ actionButton [ onClick <| ShowConfirmation "Delete this excerpt?" (DeleteExcerpt excerpt) ] [ text "× Delete" , div [ class "hint" ] [ text "Remove this excerpt from your collection" ] ] ] ] else null , hr [] [] ] ] ================================================ FILE: src/Views/ExcerptList.elm ================================================ module Views.ExcerptList exposing (excerptList) import Dict exposing (Dict, get) import Html exposing (Html) import Html.Attributes exposing (class) import Html.Keyed as Keyed import List exposing (indexedMap) import Maybe exposing (withDefault) import Msg exposing (Msg) import Types exposing (BookMap, Excerpt, ExcerptMap, ExcerptTab(..), Id, NeighborMap) import Views.Excerpt exposing (excerptView) excerptList : List Excerpt -> ExcerptMap -> BookMap -> NeighborMap -> Dict Id Bool -> Dict Id ExcerptTab -> Bool -> Id -> Maybe (Html Msg) -> Html Msg excerptList excerpts excerptMap books neighbors idToShowDetails idToActiveTab showLensTab bookmark mProgress = Keyed.ul [ class "excerpts" ] (indexedMap (\i excerpt -> ( excerpt.id , excerptView excerptMap books (withDefault [] (get excerpt.id neighbors)) (withDefault False (get excerpt.id idToShowDetails)) (withDefault Related (get excerpt.id idToActiveTab)) showLensTab i False (bookmark == excerpt.id) mProgress excerpt ) ) excerpts ) ================================================ FILE: src/Views/Import.elm ================================================ module Views.Import exposing (importView) import File import Html exposing ( Attribute , Html , a , aside , br , code , details , div , em , h1 , h3 , li , ol , p , section , span , summary , text ) import Html.Attributes exposing (class, classList, disabled, href, target) import Html.Events exposing (onClick, preventDefaultOn) import Json.Decode as Decode exposing (Decoder) import List exposing (intersperse, map) import Msg exposing (Msg(..)) import Utils exposing (appName, null, repoUrl) import Views.Button exposing (actionButton) importView : Bool -> Bool -> Html Msg importView emptyOrDemo isDragging = div [ class "import" ] [ h1 [] [ text "Imports ", em [] [ text "&" ], text " exports" ] , section [] [ if emptyOrDemo then aside [] [ text "Ready to import your collection? Drop a file or read the instructions below." ] else null , div [ classList [ ( "dropZone", True ), ( "active", isDragging ) ] , on "dragenter" (Decode.succeed DragEnter) , on "dragover" (Decode.succeed DragEnter) , on "dragleave" (Decode.succeed DragLeave) , on "drop" dropDecoder ] [ p [] [ h3 [] [ text "Drop a file here" ] , text "(Kindle clippings " , span [ class "smallCaps" ] [ text "txt" ] , text ", " , span [ class "smallCaps" ] [ text "csv" ] , text ", or " , span [ class "smallCaps" ] [ text "json" ] , text ")" ] ] , div [] [ div [ class "buttonStack" ] [ actionButton [ onClick PickKindleFile ] [ text "Import from Kindle" ] , p [] [ text "Import new excerpts from a Kindle clippings file." , details [] [ summary [] [ text "How?" ] , ol [] [ li [] [ text "Plug your Kindle in via " , span [ class "smallCaps" ] [ text "usb" ] , text "." ] , li [] [ text "Find " , code [] [ text "Kindle/Documents/My Clippings.txt" ] , text " in a file browser." ] , li [] [ text "Drag it onto this page or click the button above." ] , li [] [ text "Repeat this process whenever you highlight new excerpts and theyʼll be added to your collection." ] ] ] ] , actionButton [ onClick ImportCsv ] [ text "Import " , span [ class "smallCaps" ] [ text "csv" ] ] , p [] [ text "Import new excerpts from a " , span [ class "smallCaps" ] [ text "csv" ] , text " file." , details [] [ summary [] [ text "Format details" ] , p [] [ text "Provide rows of excerpts in the following schema: " , br [] [] , br [] [] , code [] (map text [ "title," , "author," , "text," , "pageNum (optional)," , "date (unix time, optional)," , "notes (optional)," , "sourceUrl (optional)" ] |> intersperse (br [] []) ) ] ] ] , actionButton [ onClick ImportJson ] [ text "Import " , span [ class "smallCaps" ] [ text "json" ] ] , p [] [ text <| "Restore your collection and all settings via a previously exported " ++ appName ++ " " , span [ class "smallCaps" ] [ text "json" ] , text " file. This will replace all existing state." ] , actionButton [ disabled True ] [ text "Import ???" ] , p [] [ text "Need another way to import your excerpts? " , br [] [] , a [ href <| repoUrl ++ "/issues", target "_blank" ] [ text "Open an issue here" ] , text "." ] , p [] [ text "You can also manually " , a [ href "/create" ] [ text "create new excerpts" ] , text " or automatically add them via " , span [ class "smallCaps" ] [ text "url" ] , text " parameters." , br [] [] , details [] [ summary [] [ text "How?" ] , code [] (map text [ "https://emdash.ai/create" , "?title=The Work Title" , "&author=The Author" , "&text=The text of the excerpt" ] |> intersperse (br [] []) ) ] ] ] , div [ class "buttonStack" ] [ actionButton [ onClick ExportJson ] [ text "Export " , span [ class "smallCaps" ] [ text "json" ] ] , p [] [ text "Exports your full collection including tags, notes, and ratings for safekeeping." ] , actionButton [ onClick (GetTime ExportEpub) ] [ text "Export " , span [ class "smallCaps" ] [ text "epub" ] ] , p [] [ text "Exports your excerpts into an organized " , span [ class "smallCaps" ] [ text "epub" ] , text " file for review on an e-reader." ] ] ] ] ] dropDecoder : Decoder Msg dropDecoder = Decode.at [ "dataTransfer", "files" ] (Decode.oneOrMore (\f _ -> GotDroppedFile f) File.decoder) on : String -> Decoder msg -> Attribute msg on event decoder = preventDefaultOn event (Decode.map (\m -> ( m, True )) decoder) ================================================ FILE: src/Views/Landing.elm ================================================ module Views.Landing exposing (landingPageBooks, landingView) import Html exposing ( Html , a , aside , div , em , h1 , h2 , h3 , hr , img , li , main_ , p , section , span , text , ul ) import Html.Attributes exposing ( alt , attribute , class , draggable , href , src , style , target ) import Html.Events exposing (onClick) import List exposing (drop, isEmpty, length, map, range, reverse, take) import Msg exposing (Msg(..)) import Types exposing (Book, CountMap) import Utils exposing (appName, getCount) import Views.BookList exposing (bookView) import Views.Button exposing (actionButton) import Views.MonkSignup exposing (monkSignup) landingView : List Book -> CountMap -> Bool -> Html Msg landingView bookList countMap didSubmitEmail = let bookCols = 6 speed = 10 colSize = length bookList // bookCols bookLists = range 0 (bookCols - 1) |> map (\n -> bookList |> drop (n * colSize) |> take colSize) list1 = take (bookCols // 2) bookLists list2 = drop (bookCols // 2) bookLists in div [ class "landing" ] [ div [ class "anim", attribute "aria-hidden" "true" ] (if isEmpty bookList then [] else map (\col -> div [ class "bookShelf" ] (map (\books -> let bookViews = map (\book -> bookView book (getCount countMap book.id) 0 False False True ) books duration = style "animation-duration" (String.fromInt (length books * speed) ++ "s") in div [ class "bookCol" ] [ div [ duration ] bookViews , div [ duration ] bookViews ] ) col ) ) [ map reverse list2 ++ list1, list2 ++ map reverse list1 ] ) , main_ [] [ img [ src "/images/logo.svg", class "logo", draggable "false", alt appName ] [] , section [ class "cta" ] [ h1 [] [ text <| appName ++ " uses AI to organize text snippets so you can " , em [] [ text "actually remember & learn from" ] , text " what you read." ] , aside [] [ text "Oh and itʼs free & open-source." ] , div [] [ actionButton [ onClick StartDemo ] [ text "Try an ", em [] [ text "instant" ], text " demo" ] , img [ src "/images/landing/flower-yellow.png" , class "botanical1" , alt "" ] [] , aside [] [ text "Please, click." ] ] , img [ src "/images/landing/orange.png" , class "botanical2" , alt "" ] [] ] , hr [] [] , hr [] [] , section [ class "features" ] [ h2 [] [ text "Featuring" ] , div [] [ img [ src "/images/landing/flower-white.png", alt "" ] [] , img [ src "/images/landing/flower-red.png", alt "" ] [] , ul [] [ li [] [ h3 [] [ text "Conceptual cousins" ] , p [] [ text <| "On-device AI analysis finds passages with similar ideas " ++ "from other authors, often from a different angle." ] ] , li [] [ h3 [] [ text "Instant semantic search" ] , p [] [ text <| "Find what youʼre looking for with both full-text " ++ "search and deeper semantic matching of fuzzy ideas." ] ] , li [] [ h3 [] [ text "Tag, rate, note, reflect" ] , p [] [ text <| "Organize with tags, add ratings, and " ++ "annotate your thoughts. Export back to " , span [ class "smallCaps" ] [ text "epub" ] , text " for review on your e-reader." ] ] , li [] [ h3 [] [ text "Roll the dice, change your lens" ] , p [] [ text <| "Unearth ideas youʼve forgotten about via " ++ "random discovery. Rephrase dense concepts and re-explain with metaphors." ] ] , li [] [ h3 [] [ text "No lock-in" ] , p [] [ text "Bring in your highlights from your Kindle or as " , span [ class "smallCaps" ] [ text "json" ] , text ", " , span [ class "smallCaps" ] [ text "csv" ] , text ", or manual input. Export instantly to the same open formats." ] ] , li [] [ h3 [] [ text "Open-source " , span [] [ text "&" ] , text " offline first" ] , p [] [ text <| "On-device analysis means your collection stays on " ++ "your device until you opt into advanced features." ] ] ] ] ] , hr [] [] , hr [] [] , monkSignup didSubmitEmail , section [ class "coda" ] [ aside [] [ text "Thank you for reading." ] , p [] [ em [] [ text "ex libris " ] , a [ href "https://oxism.com", target "_blank" ] [ text "oxism.com" ] , text " • A.D. MMXXIV" ] ] ] ] landingPageBooks : List ( String, String ) landingPageBooks = [ ( "Dune", "Frank Herbert" ) , ( "Mindfulness in Plain English", "Henepola Gunaratana" ) , ( "The Odyssey", "Homer" ) , ( "A Confederacy of Dunces", "John Kennedy Toole" ) , ( "On the Shortness of Life", "Seneca the Younger" ) , ( "How to Change Your Mind", "Michael Pollan" ) , ( "Fragments", "Heraclitus" ) , ( "The Enchiridion", "Epictetus" ) , ( "The Sirens of Titan", "Kurt Vonnegut" ) , ( "Gödel, Escher, Bach", "Douglas Hofstadter" ) , ( "Being Aware of Being Aware", "Rupert Spira" ) , ( "Essays and Aphorisms", "Arthur Schopenhauer" ) , ( "Perfume", "Patrick Süskind" ) , ( "A Brief History of Thought", "Luc Ferry" ) , ( "Blood Meridian", "Cormac McCarthy" ) , ( "2001", "Arthur C. Clarke" ) , ( "Phaedo", "Plato" ) , ( "Prometheus Rising", "Robert Anton Wilson" ) , ( "Letter from a Birmingham Jail", "Martin Luther King Jr." ) , ( "The Old Man and the Sea", "Ernest Hemingway" ) , ( "Amusing Ourselves to Death", "Neil Postman" ) , ( "The Little Prince", "Antoine de Saint-Exupéry" ) , ( "Alice’s Adventures in Wonderland", "Lewis Carroll" ) , ( "The Order of Time", "Carlo Rovelli" ) , ( "Invisible Cities", "Italo Calvino" ) , ( "Hard-Boiled Wonderland and the End of the World", "Haruki Murakami" ) , ( "The Metamorphosis", "Franz Kafka" ) , ( "Notes from Underground", "Fyodor Dostoevsky" ) , ( "Heaven and Hell", "Aldous Huxley" ) , ( "The Society of the Spectacle", "Guy Debord" ) , ( "The Crying of Lot 49", "Thomas Pynchon" ) , ( "Oedipus Rex", "Sophocles" ) , ( "Civilization and its Discontents", "Sigmund Freud" ) , ( "Ways of Seeing", "John Berger" ) , ( "The True Believer", "Eric Hoffer" ) , ( "Flatland", "Edwin Abbott Abbott" ) , ( "The Iliad", "Homer" ) , ( "The Republic", "Plato" ) , ( "On the Genealogy of Morals", "Friedrich Nietzsche" ) , ( "Middlemarch", "George Eliot" ) , ( "Maxims", "François de La Rochefoucauld" ) , ( "Simulacra and Simulation", "Jean Baudrillard" ) , ( "Understanding Media", "Marshall McLuhan" ) , ( "The Secret History", "Donna Tartt" ) , ( "Pedro Páramo", "Juan Rulfo" ) , ( "Six Easy Pieces", "Richard Feynman" ) , ( "Seeing Like a State", "James C. Scott" ) , ( "The Count of Monte Cristo", "Alexandre Dumas" ) , ( "Candide", "Voltaire" ) ] ================================================ FILE: src/Views/MonkSignup.elm ================================================ module Views.MonkSignup exposing (monkSignup) import Html exposing ( Html , aside , div , em , form , h2 , img , input , li , section , text , ul ) import Html.Attributes exposing (alt, class, placeholder, src, type_) import Html.Events exposing (onInput, onSubmit) import Msg exposing (Msg(..)) import Views.Button exposing (actionButton) monkSignup : Bool -> Html Msg monkSignup didJoinMailingList = section [ class "monk" ] [ img [ src "/images/landing/mushroom.png", class "mushrooms", alt "" ] [] , div [] [ aside [] [ text "Coming eventually" ] , h2 [] [ text "Monk-Mode" ] , ul [] [ li [] [ text "Lenses — " , em [] [ text "summarize & rephrase complex ideas" ] ] , li [] [ text "Socratic switch — " , em [] [ text "interview your books" ] ] , li [] [ text "Cross-device syncing and backup" ] , li [] [ text "Publishing / sharing excerpts" ] , li [] [ text "Sturdier gardening tools" ] ] , form [ onSubmit SubscribeToMailingList ] (if didJoinMailingList then [ aside [] [ text <| "Thanks for joining the waitlist, " ++ "weʼll be in touch with updates." ] ] else [ aside [] [ text "Care to sign up for the waitlist?" ] , div [] [ input [ type_ "email" , placeholder "Your email address" , onInput UpdateMailingListEmail ] [] , actionButton [] [ text "Submit" ] ] ] ) ] ] ================================================ FILE: src/Views/SearchInput.elm ================================================ module Views.SearchInput exposing (searchInput) import Html exposing (Html, button, div, input, text) import Html.Attributes exposing ( attribute , autocomplete , class , placeholder , spellcheck , title , value ) import Html.Events exposing (onClick, onInput) import Msg exposing (Msg(..)) searchInput : String -> Html Msg searchInput searchQuery = div [ class "search" ] [ input [ onInput OnSearchStart , spellcheck False , placeholder "Search your library" , value searchQuery , attribute "enterkeyhint" "search" , attribute "autocapitalize" "off" , autocomplete False ] [] , button [ onClick <| OnSearchStart "" , class (if String.isEmpty searchQuery then "" else "active" ) , title "Clear" ] [ text "✕" ] ] ================================================ FILE: src/Views/SearchResults.elm ================================================ module Views.SearchResults exposing (searchResults) import Dict exposing (get) import Html exposing (Html, button, div, li, p, span, text, ul) import Html.Attributes exposing (class, classList) import Html.Events exposing (onClick) import Html.Keyed as Keyed import List exposing (filterMap, indexedMap, isEmpty, length, map, take) import Msg exposing (Msg(..)) import Types exposing ( Book , BookMap , BookSort(..) , CountMap , Excerpt , ExcerptMap , ScorePairs , SearchMode(..) ) import Utils exposing (juxt, null) import Views.BookList exposing (bookList) import Views.Snippet exposing (snippetView) maxResults : Int maxResults = 100 searchResults : SearchMode -> BookMap -> ExcerptMap -> List Book -> List Excerpt -> ScorePairs -> CountMap -> CountMap -> String -> Html Msg searchResults mode bookMap excerptMap books matches semanticMatches excerptCounts favCounts query = let textMatches = map (juxt identity (always Nothing)) matches semMatches = filterMap (\( id, score ) -> get id excerptMap |> Maybe.map (\excerpt -> ( excerpt, Just score ) ) ) semanticMatches ( list, label ) = if mode == TextMatches then ( textMatches, "text matches" ) else ( semMatches, "semantic matches" ) in div [ class "searchResults" ] [ if isEmpty books then null else bookList books excerptCounts favCounts TitleSort False , div [ class "snippets" ] [ div [ class "modeHeading" ] [ ul [] (map (\( m, title, len ) -> li [ classList [ ( "active", m == mode ) ] , onClick (SetSearchTab m) ] [ button [] [ span [] [ text title ] , div [ class "count" ] [ text <| if len > maxResults then ">" ++ String.fromInt maxResults else String.fromInt len ] ] ] ) [ ( TextMatches, "Text matches", length textMatches ) , ( SemanticMatches, "Semantic matches", length semMatches ) ] ) ] , if isEmpty list then p [ class "noResults" ] [ text <| "No " ++ label ++ " found." ] else let q = if mode == TextMatches then Just query else Nothing in Keyed.ul [] (indexedMap (\i ( excerpt, mScore ) -> ( excerpt.id , snippetView bookMap mScore q i excerpt ) ) (take maxResults list) ) ] ] ================================================ FILE: src/Views/Settings.elm ================================================ module Views.Settings exposing (settingsView) import Html exposing (Html, a, div, em, h1, h2, input, label, li, p, pre, section, sup, text, ul) import Html.Attributes exposing (class, href, step, type_, value) import Html.Events exposing (onInput) import List exposing (map) import Msg exposing (Msg(..)) import String exposing (fromFloat, fromInt) import Utils exposing (appName, formatNumber, repoUrl) settingsView : String -> Int -> Int -> Int -> Int -> Float -> Html Msg settingsView version excerptCount bookCount authorCount tagCount semanticThreshold = div [ class "settings" ] [ h1 [] [ text "Settings ", em [] [ text "&c." ] ] , section [] [ div [] [ h2 [] [ text "Semantic search threshold" ] , div [] [ label [] [ input [ type_ "range" , Html.Attributes.min "0.10" , Html.Attributes.max "0.95" , step "0.01" , value <| fromFloat semanticThreshold , onInput SetSemanticThreshold ] [] , text <| fromInt (floor (semanticThreshold * 100)) , sup [] [ text "%" ] ] ] , p [] [ text "Lower values yield more semantic matches." ] , h2 [] [ text "Import/export excerpts" ] , p [] [ text "Visit the " , a [ href "/import" ] [ text "import page" ] , text "." ] , h2 [] [ text "Monk-Mode" ] , p [] [ a [ href "/monk-mode" ] [ em [] [ text "Monk-Mode" ] ] , text <| " is a forthcoming set of features to further enhance your " ++ appName ++ " experience. Consider joining the waitlist." ] ] , div [] [ h2 [] [ text "Statistics" ] , ul [] (map (\( name, n ) -> li [] [ text <| formatNumber n ++ " " ++ name ++ (if n /= 1 then "s" else "" ) ] ) [ ( "excerpt", excerptCount ) , ( "title", bookCount ) , ( "author", authorCount ) , ( "tag", tagCount ) ] ) , h2 [] [ text "Version" ] , p [] [ pre [] [ text version ] ] , h2 [] [ text "Colophon" ] , p [ class "colophon" ] [ text <| appName ++ " is an open-source wisdom-indexer created by " , a [ href "https://oxism.com" ] [ text "Dan Motzenbecker" ] , text "." ] , p [ class "colophon" ] [ text "Itʼs written in " , a [ href "https://elm-lang.org" ] [ text "Elm" ] , text " and typeset in " , a [ href "https://en.wikipedia.org/wiki/EB_Garamond" ] [ text "EB Garamond" ] , text ". Read the " , a [ href repoUrl ] [ text "source code here" ] , text "." ] ] ] ] ================================================ FILE: src/Views/Snippet.elm ================================================ module Views.Snippet exposing (snippetView) import Dict exposing (get) import Html exposing (Html, a, blockquote, div, li, mark, text) import Html.Attributes exposing (class, href, style) import List exposing (indexedMap) import Msg exposing (Msg) import Regex import Router exposing (excerptToRoute) import String exposing (fromInt, join, split) import Types exposing (Book, BookMap, Excerpt) import Utils exposing (appName, null, rx_) import Views.Citation exposing (citation) snippetView : BookMap -> Maybe Float -> Maybe String -> Int -> Excerpt -> Html Msg snippetView books mScore query i excerpt = case get excerpt.bookId books of Just book -> let inner = innerSnippet excerpt book mScore query in li [ class "snippet" , style "animation-delay" (fromInt (i * 99) ++ "ms") ] [ a [ href <| excerptToRoute books excerpt ] (inner ++ [ div [ class "clone" ] inner ] ) ] _ -> null innerSnippet : Excerpt -> Book -> Maybe Float -> Maybe String -> List (Html Msg) innerSnippet excerpt book mScore query = [ blockquote [] (case query of Just q -> addHighlighting excerpt.text q _ -> [ text excerpt.text ] ) , citation excerpt book mScore ] addHighlighting : String -> String -> List (Html msg) addHighlighting str query = str |> Regex.replace (rx_ ("\\b(" ++ (query |> split " " |> join "|") ++ ")")) (\{ match } -> sigil ++ match ++ sigil) |> split sigil |> indexedMap (\i s -> if modBy 2 i == 1 then mark [] [ text s ] else text s ) sigil : String sigil = "__" ++ appName ++ "_splitter__" ================================================ FILE: src/Views/TagSection.elm ================================================ module Views.TagSection exposing (tagSection) import Html exposing ( Html , a , button , datalist , div , form , h5 , input , li , option , text , ul ) import Html.Attributes exposing ( attribute , autocomplete , class , disabled , href , id , list , placeholder , spellcheck , value ) import Html.Events exposing (onClick, onInput, onSubmit) import List exposing (filter, isEmpty, map, member) import Msg exposing (Msg(..)) import Router exposing (tagToRoute) import Types exposing (Tag) import Utils exposing (null) import Views.Button exposing (actionButton) tagSection : List Tag -> List Tag -> Maybe Tag -> Html Msg tagSection tags globalTags pendingTag = div [] [ h5 [] [ text "Tags" ] , if not (isEmpty tags) then div [ class "tags" ] [ ul [] (map (\tag -> li [ class "tag" ] [ button [ onClick <| RemoveTag tag , class "tagDelete" ] [ text "×" ] , a [ href <| tagToRoute tag ] [ text tag ] ] ) tags ) ] else null , let datalistId = "tagDatalist" pendTag = Maybe.withDefault "" pendingTag in form [ class "tagInput", onSubmit AddTag ] [ datalist [ id datalistId ] (map (\tag -> option [ value tag ] []) (filter (\tag -> member tag tags |> not |> (&&) (String.contains pendTag tag) ) globalTags ) ) , input [ onInput UpdatePendingTag , value pendTag , list datalistId , placeholder "Add tag" , autocomplete False , attribute "autocapitalize" "off" , attribute "enterkeyhint" "done" , spellcheck False ] [] , actionButton [ disabled <| pendTag == "" ] [ text "+" ] ] ] ================================================ FILE: src/Views/Toolbar.elm ================================================ module Views.Toolbar exposing (toolbar) import Html exposing (Html, a, button, div, img, text) import Html.Attributes exposing (alt, class, draggable, href, src) import Html.Events exposing (onClick) import Msg exposing (Msg(..)) toolbar : Html Msg toolbar = div [ class "toolbar" ] [ a [ href "/settings" ] [ img [ class "icon" , src "/images/icons/settings.svg" , alt "Settings et cetera" , draggable "false" ] [] , div [ class "hint left" ] [ text "Settings &c." ] ] , a [ href "/import" ] [ img [ class "icon" , src "/images/icons/import.svg" , alt "Import & export excerpts" , draggable "false" ] [] , div [ class "hint left" ] [ text "Import & export excerpts" ] ] , a [ href "/create" ] [ img [ class "icon" , src "/images/icons/create.svg" , alt "Create a new excerpt" , draggable "false" ] [] , div [ class "hint left" ] [ text "Create a new excerpt" ] ] , button [ onClick ShowRandom ] [ img [ class "icon" , src "/images/icons/random.svg" , alt "Random excerpt" , draggable "false" ] [] , div [ class "hint left" ] [ text "Discover a random excerpt" ] ] , button [ onClick ScrollToTop ] [ img [ class "icon" , src "/images/icons/scroll-top.svg" , alt "Create excerpt" , draggable "false" ] [] , div [ class "hint left" ] [ text "Scroll to top" ] ] ] ================================================ FILE: src/main.js ================================================ import {get, set, createStore} from 'idb-keyval' import SharedWorker from '@okikio/sharedworker' import {Elm} from './Main.elm' import {version} from '../package.json' import './styles/main.sass' const dbNs = 'emdash' const stateKey = 'state' const writeMs = 333 const bcKey = 'BroadcastChannel' const supportsBroadcastChannel = bcKey in window const worker = new SharedWorker(new URL('./worker.js', import.meta.url), { name: 'emdash', type: 'module' }) const channel = supportsBroadcastChannel && new BroadcastChannel(dbNs) const messageToPort = { processNewExcerpts: 'receiveExcerptEmbeddings', initWithClear: 'receiveExcerptEmbeddings', computeExcerptEmbeddings: 'receiveExcerptEmbeddings', computeBookEmbeddings: 'receiveBookEmbeddings', computeAuthorEmbeddings: 'receiveAuthorEmbeddings', requestExcerptNeighbors: 'receiveExcerptNeighbors', requestBookNeighbors: 'receiveBookNeighbors', requestAuthorNeighbors: 'receiveAuthorNeighbors', requestSemanticRank: 'receiveSemanticRank', semanticSearch: 'receiveSemanticSearch', setDemoEmbeddings: 'receiveExcerptEmbeddings' } const msgWorker = (method, payload) => worker && worker.port.postMessage({method, ...payload}) const downloadFile = (name, data) => { const a = document.createElement('a') const url = URL.createObjectURL(data) a.href = url a.download = name a.click() URL.revokeObjectURL(url) } let app let writeTimer let zipWorker let zipWorkerReady !(async () => { console.log(`${dbNs} v${version} ⁓ habent sua fata libelli`) let restored = null let stateStore try { await new Promise((res, rej) => { const testNs = `${dbNs}:test` const dbReq = indexedDB.open(testNs) dbReq.onerror = rej dbReq.onsuccess = () => { res() indexedDB.deleteDatabase(testNs) } }) stateStore = createStore(`${dbNs}:${stateKey}`, stateKey) restored = await get(stateKey, stateStore) } catch (e) { console.warn('cannot open DB for writing') } const flags = [ restored || null, [!supportsBroadcastChannel && bcKey].filter(Boolean), [ version, import.meta.env.VITE_MAILING_LIST_URL || '', import.meta.env.VITE_MAILING_LIST_FIELD || '' ] ] try { app = Elm.Main.init({flags}) } catch (e) { console.warn('malformed restored state:', restored) app = Elm.Main.init({flags: [null, ...flags.slice(1)]}) } if (channel) { channel.onmessage = ({data}) => app.ports.syncState.send(data) } app.ports.initWithClear.subscribe(state => msgWorker('initWithClear', {books: state.books, excerpts: state.excerpts}) ) app.ports.handleNewExcerpts.subscribe(state => msgWorker('processNewExcerpts', {excerpts: state.excerpts}) ) app.ports.exportJson.subscribe(state => downloadFile( `${dbNs}_backup_${new Date().toLocaleDateString()}.json`, new Blob([JSON.stringify(state)], {type: 'text/plain'}) ) ) app.ports.setStorage.subscribe(state => { clearTimeout(writeTimer) if (stateStore) { writeTimer = setTimeout(() => { set(stateKey, JSON.stringify(state), stateStore) channel.postMessage(state) }, writeMs) } }) app.ports.createEpub.subscribe(async pairs => { if (!zipWorker) { zipWorkerReady = new Promise(res => { zipWorker = new Worker(new URL('./zip-worker.js', import.meta.url), { type: 'module' }) zipWorker.onmessage = ({data}) => { if (!data) { res() return } downloadFile(...data) } }) } await zipWorkerReady zipWorker.postMessage(pairs) }) app.ports.requestExcerptEmbeddings.subscribe(targets => msgWorker('computeExcerptEmbeddings', {targets}) ) app.ports.requestBookEmbeddings.subscribe(targets => msgWorker('computeBookEmbeddings', {targets}) ) app.ports.requestAuthorEmbeddings.subscribe(targets => msgWorker('computeAuthorEmbeddings', {targets}) ) app.ports.deleteExcerpt.subscribe( ([targetId, [bookId, bookExcerptIds], k]) => { msgWorker('deleteExcerpt', {targetId, bookId, bookExcerptIds}) msgWorker('requestBookNeighbors', {target: bookId, k}) } ) app.ports.deleteBook.subscribe(([bookId, bookExcerptIds]) => msgWorker('deleteBook', {bookId, bookExcerptIds}) ) app.ports.requestExcerptNeighbors.subscribe(([target, k]) => msgWorker('requestExcerptNeighbors', {target, k}) ) app.ports.requestAuthorNeighbors.subscribe(([target, k]) => msgWorker('requestAuthorNeighbors', {target, k}) ) app.ports.requestBookNeighbors.subscribe(([target, k]) => msgWorker('requestBookNeighbors', {target, k}) ) app.ports.requestSemanticSearch.subscribe(([query, threshold]) => msgWorker('semanticSearch', {query, threshold}) ) app.ports.requestSemanticRank.subscribe(([bookId, excerptIds]) => msgWorker('requestSemanticRank', {bookId, excerptIds}) ) app.ports.scrollToTop.subscribe(() => window.scrollTo({top: 0, left: 0, behavior: 'smooth'}) ) app.ports.requestUnicodeNormalized.subscribe(str => app.ports.receiveUnicodeNormalized.send(str.normalize('NFC')) ) app.ports.fetchDemoEmbeddings.subscribe(() => msgWorker('fetchDemoEmbeddings') ) app.ports.setDemoEmbeddings.subscribe(ids => msgWorker('setDemoEmbeddings', {ids}) ) worker.port.onmessage = ({data}) => { if (!data?.method) { console.log('worker:', data) return } app.ports[messageToPort[data.method]].send(data.data) } })() window.addEventListener( 'input', ({target}) => { if (target.type !== 'text') { return } const {value, selectionStart} = target requestAnimationFrame(() => { if (target.value !== value) { target.selectionStart = target.selectionEnd = selectionStart - (value.length - target.value.length) } }) }, true ) ================================================ FILE: src/styles/book-info.sass ================================================ @use 'vars' as * .bookInfo margin: 0 auto h1, h2 user-select: text h1 font-style: italic @include mobile font-size: 4rem h2 margin-bottom: 0 font-size: 2.4rem .tagsRating display: flex flex-direction: column > div margin-bottom: 3rem &:first-child flex: 1 &:last-child @include mobile padding-left: 3rem @include mobile flex-direction: row .ratingNum font-size: 2.4rem &.unrated color: $mid3 .half font-variant-numeric: diagonal-fractions .rating display: flex align-items: center @include mobile flex-direction: column .ratingNum width: 4rem @include mobile width: 100% margin-bottom: 1.2rem &.unrated position: relative color: $mid3 top: -0.3rem input[type='range'] width: 100% max-width: 9.9rem margin: 0 padding: 0 .bookMeta margin: 3rem auto display: flex @include mobile flex-direction: column margin-bottom: 0 .col display: flex flex-direction: column padding: 1rem 0 flex: 1 padding-right: 3rem border-right: 1px solid $mid2 @include mobile border: none &:last-child display: flex flex-direction: column justify-content: space-between padding-right: 2rem padding-left: 2rem @include mobile padding: 0 margin: 2rem 0 h5 margin-bottom: 1rem .tags padding: 0 text-align: left details h5 display: inline-block margin-bottom: 0 > div margin-top: 1rem button + button margin-left: 1rem textarea margin-top: 1.5rem span color: $mid3 font-weight: 100 .editTitle input width: 100% margin: 1rem 0 .related li font-size: 2rem line-height: 1.3 opacity: 0 animation: fadeIn 333ms forwards + li margin-top: 1rem &:hover .score opacity: 1 .score position: absolute display: inline-flex margin-left: 0.5rem vertical-align: super opacity: 0 @include fade @include mobile display: none !important .embeddingProgress margin: 4rem 0 .tags padding: 0 4rem text-align: center max-width: 150rem margin: 0 auto @include mobile padding: 0 .actions display: flex justify-content: space-between align-items: center font-size: 1.8rem @include mobile flex-direction: column-reverse .modeHeading margin: 0 @include mobile margin-top: 4rem .noFav padding: 8rem font-size: 1.8rem text-align: center .authorInfo .related margin: 2rem 0 margin-bottom: 4rem ul, h5, li display: inline li margin-right: 0.4em ================================================ FILE: src/styles/books.sass ================================================ @use 'vars' as * .bookList width: 100% max-width: 150rem margin: 0 auto display: grid grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr)) gap: 4rem justify-items: center padding: 2rem @include mobile padding: 0 grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)) gap: 2rem .author font-size: 1.3rem overflow: hidden display: -webkit-box -webkit-box-orient: vertical -webkit-line-clamp: 2 .count, .favCount font-size: 1.3rem .book transform: scale(0.95) transition: transform .2s &:hover transform: scale(0.99) a text-decoration: none &:after transform: none a transition: all .2s text-decoration: none display: flex flex-direction: column width: 20rem height: 30rem border: 1px solid $fg padding: 2rem align-items: center justify-content: center text-align: center position: relative @include mobile width: 14rem height: 21rem &:after transition: transform .2s content: '' border: 1px solid $mid2 width: 100% height: 100% position: absolute z-index: -1 pointer-events: none transform: translateX(.8rem) translateY(.8rem) .title margin-bottom: 2rem font-size: 2rem overflow: hidden display: -webkit-box -webkit-box-orient: vertical -webkit-line-clamp: 4 padding: 0 0.4rem @include mobile font-size: 1.8rem .count, .ratingNum, .favCount position: absolute .count bottom: 1rem right: 1rem .ratingNum, .favCount top: 1rem right: 1rem .authorInfo max-width: $maxWidth margin: 0 auto margin-bottom: 2rem + .modeHeading margin: 2rem 0 .favCount display: flex align-items: center ================================================ FILE: src/styles/create.sass ================================================ @use 'vars' as * .createPage font-size: 1.8rem h1 margin-bottom: 1rem form margin-top: 4rem > div display: flex @include mobile flex-direction: column label flex: 1 + label margin-left: 2rem flex: 3 @include mobile margin-left: 0 form, label display: flex flex-direction: column label margin-bottom: 2rem em color: $mid3 font-style: italic margin-left: 0.6rem input, textarea margin-bottom: .5rem font-size: 2rem textarea font-size: 1.4rem height: 24rem button margin-top: 3rem ================================================ FILE: src/styles/excerpts.sass ================================================ @use 'vars' as * .excerpts width: 100% margin: 0 auto margin-bottom: 8rem display: flex flex-direction: column align-items: center .excerpt width: 100% padding-top: 4rem margin-bottom: 2rem position: relative .tabs margin-top: 1.2rem opacity: 0 @include fade &.active opacity: 1 button:last-child text-transform: unset @include touch opacity: 1 hr opacity: 0 @include fade @include touch opacity: 1 .bookmark, .favorite position: relative margin: 0.2rem 0 opacity: 0 @include fade &.active opacity: 1 .icon margin-right: -8px &:hover .tabs, hr opacity: 1 .meta .page, a, .bookmark, .favorite opacity: 1 &.permalink figure > cite font-size: 2.4rem margin: 4rem 0 margin-bottom: 6rem @include mobile font-size: 1.8rem @include mobile padding-top: 0 .favorite margin-left: -0.4rem blockquote font-size: 2.4rem line-height: 1.8 user-select: text hyphens: auto @include mobile font-size: 2rem .meta position: absolute transform: translateX(-100%) display: flex flex-direction: column align-items: flex-end padding-right: 2rem font-size: 1.4rem margin-top: 0.4rem text-align: right line-height: 2 @include mobile margin-bottom: 1rem position: static flex-direction: row transform: none align-items: center > * margin-right: 1.8rem !important transform: none !important opacity: 1 !important .page, a opacity: 0 position: relative @include fade .detailsToggle position: absolute transform: translateX(-100%) color: $mid2 padding: 2rem 2rem 0 0 opacity: 0 &.active > span color: $fg transform: rotate(90deg) > span transition: all 0.2s display: inline-block font-size: 1.2rem &:hover .hint opacity: 1 .details section:not(.relatedExcerpts) padding: 3rem 0 .embeddingProgress margin: 4rem 0 + hr opacity: 1 .relatedExcerpts min-height: 0.4rem .neighbors margin: 4rem 0 .snippet opacity: 0 animation: fadeIn 333ms forwards .snippet $size: 1.8rem position: relative margin-bottom: 4rem > a text-decoration: none &:hover z-index: 99 .clone display: block z-index: 1 outline: 1px solid $mid1 @include dropShadow blockquote font-size: $size overflow: hidden display: -webkit-box -webkit-box-orient: vertical -webkit-line-clamp: 3 mark background: none box-shadow: inset 0 -8.5px 0 $highlight cite font-size: $size margin-top: 1.5rem .clone $pad: 3rem position: absolute width: calc(100% + #{$pad} * 2) top: -$pad left: -$pad background: $bg display: none padding: $pad blockquote overflow: visible display: block .score font-size: 1.2rem font-weight: 500 position: relative cite display: flex flex-wrap: wrap font-style: normal line-height: 1.5 > * flex-shrink: 0 .divider margin: 0 .4em color: $mid2 font-size: .8em .score margin-left: auto display: flex align-items: center span font-size: 1.2rem @include mobile flex-wrap: nowrap .title flex: 1 .divider, .author, .page display: none .showHints .favorite, .bookmark, .page, .excerpt .tabs, .excerpt hr opacity: 1 ================================================ FILE: src/styles/import.sass ================================================ @use 'vars' as * .import p, li, code user-select: text section margin-top: 3rem aside margin-bottom: 3rem > div:last-child display: flex @include mobile flex-direction: column > div flex: 1 padding: 0 5rem @include mobile padding: 0 padding-top: 4rem &:first-child padding-left: 0 @include mobile padding: 0 &:last-child border-left: 1px solid $mid2 padding-right: 0 @include mobile border: none border-top: 1px solid $mid2 code font-family: $monoFont font-size: 1.3rem details margin-top: 2rem summary margin-bottom: 1rem ol list-style: lower-roman li margin-bottom: 1.6rem .dropZone border: 1px dashed $mid2 display: flex align-items: center justify-content: center text-align: center margin-bottom: 4rem height: 15rem transition: border-color .2s * user-select: none h3 font-size: 2.2rem font-style: italic margin-bottom: 1rem &.active border-color: $fg @include touch display: none ================================================ FILE: src/styles/landing.sass ================================================ @use 'vars' as * $scrollSpeed: 20s $bookGap: 1rem $bookScale: 0.7 $maxColWidth: 90rem @mixin header font-size: 4rem margin-bottom: 1.8rem text-align: center @include mobile font-size: 3rem :root --center-gutter: 13rem @include mobile --center-gutter: #{$bookGap * $bookScale} .landing margin: 0 auto padding: 8rem 0 padding-top: 0 @include mobile padding-bottom: 0 * user-select: text h2 @include header img user-select: none main position: relative .logo width: 24rem margin: 0 auto margin-top: -12rem margin-bottom: -2rem position: relative z-index: 1 @include mobile width: 18rem margin-top: -10.5rem section, hr width: 100% max-width: $maxColWidth padding: 0 4rem margin: 0 auto section margin-top: 8rem .anim pointer-events: none opacity: .5 display: flex width: 100vw height: 25rem position: relative overflow: hidden margin-bottom: 5rem border-bottom: 1px solid $mid2 user-select: none .bookShelf position: absolute top: 0 left: calc(50% - var(--center-gutter)) display: flex justify-content: flex-end transform-origin: 0 0 transform: scale($bookScale) translateX(-100%) &:last-child left: calc(50% + var(--center-gutter)) transform: scale($bookScale) .bookCol margin-right: 2rem height: fit-content > div animation: scrollUp $scrollSpeed linear infinite @media (prefers-reduced-motion) animation: none &:nth-child(even) > div animation: scrollDown $scrollSpeed linear infinite @media (prefers-reduced-motion) animation: none transform: translateY(-100px) &:last-child margin-right: 0 .book list-style: none margin-bottom: $bookGap .cta padding: 0 padding-bottom: 8rem position: relative > div position: relative display: flex justify-content: center flex-direction: column align-items: center margin-top: 12rem margin-bottom: 2rem @include mobile margin-top: 6rem h1 font-weight: normal font-size: clamp(2.6rem, 4vw, 3.8rem) margin: 0 auto margin-bottom: 3rem padding: 0 6rem line-height: 1.8 text-align: center @include mobile padding: 0 img pointer-events: none position: absolute width: clamp(12rem, 20vw, 155px) user-select: none .botanical1 transform: translateX(-17rem) translateY(-3rem) z-index: -1 @include mobile transform: translateX(-14rem) translateY(-2rem) .botanical2 bottom: 0 right: 3rem object-position: 0 10rem @include mobile object-position: 0 7rem right: 0 .actionButton margin-bottom: 2rem user-select: none .buttonContent font-size: 3rem padding: 2rem 4rem border-width: 3px border-style: double em font-style: normal border-bottom: 3px solid $fg transition: border-bottom-color .3s .buttonShadow top: 4px left: 4px &:hover .buttonContent transform: translateX(-2px) translateY(-2px) em border-bottom-color: $accent &:active .buttonContent transform: translateX(4px) translateY(4px) @include mobile font-size: 2.6rem aside text-align: center p font-size: 1.6rem &::after content: '❦' font-size: 4rem display: block text-align: center margin-top: 4rem font-family: serif hr margin: 0 auto margin-bottom: 0.5rem .features margin-bottom: 16rem padding: 0 > div position: relative h2 margin-bottom: 6rem @include mobile font-size: 3rem margin-bottom: 4rem h2, h4 text-align: center ul display: grid grid: 1fr / 1fr 1fr grid-gap: 14rem margin-top: 12rem row-gap: 18rem @include mobile grid-template-columns: 1fr row-gap: 5rem margin-top: 6rem li text-align: center img position: absolute width: 19% left: 50% top: 16.3rem transform: translateX(-50%) &:nth-child(2) margin-top: 42rem width: 15% transform: translateX(-50%) rotate(-10deg) @include mobile right: 0 left: unset transform: translateX(80%) top: 32rem width: 12rem &:nth-child(2) left: 0 margin-top: 43rem width: 9rem transform: translateX(-77%) p font-size: 2.3rem line-height: 1.7 @include mobile padding: 0 0.5rem font-size: 2rem h3 font-size: 3rem line-height: 1.4 margin-bottom: 3rem text-align: center @include mobile margin-bottom: 1.5rem font-size: 2.6rem span font-style: italic footer margin-top: 8rem .coda text-align: center aside margin-bottom: 4rem .monk display: flex justify-content: center position: relative margin-top: 13rem !important margin-bottom: 8rem !important @include mobile padding: 0 !important margin-top: 10rem !important margin-bottom: 3rem !important h2 @include header > div border: 1px double $mid2 outline: 1px solid $mid2 outline-offset: 4px box-shadow: 0 0 0 4px $bg max-width: 500px padding: 6rem 5rem text-align: center position: relative z-index: 9 background: $bg @include mobile padding: 3rem 2rem ul font-size: 2rem li line-height: 1.6 + li margin-top: 1.4rem &::before content: '• ' @include mobile font-size: 1.8rem aside margin-bottom: 1rem form display: flex flex-direction: column align-items: center margin-top: 6rem > div width: 100% max-width: 45rem display: flex flex-direction: column justify-content: center align-items: center @include mobile flex-direction: column align-items: center aside max-width: 20rem input margin-top: 2rem margin-right: 1rem font-size: 1.8rem width: 100% text-align: center margin-bottom: 2rem &:invalid, &:placeholder-shown + button pointer-events: none @include mobile text-align: center width: 80% margin-bottom: 2rem .mushrooms position: absolute max-width: 20rem left: 50% transform: translateX(-28rem) translateY(-89%) pointer-events: none user-select: none @include mobile left: 74% max-width: 16rem main > .monk margin-top: 20rem !important @keyframes scrollUp from transform: translateY(0) to transform: translateY(calc(-100% - $bookGap)) @keyframes scrollDown from transform: translateY(-100%) to transform: translateY($bookGap) ================================================ FILE: src/styles/lenses.sass ================================================ @use 'vars' as * .lenses > * padding: 0 4rem @include mobile padding: 0 .loading text-align: center margin-top: 4rem font-size: 1.6rem font-style: italic p font-size: 2rem line-height: 1.8 margin-top: 2rem margin-bottom: 3rem user-select: text @include mobile font-size: 1.8rem details max-width: 34rem p font-size: 1.6rem margin: 0 summary color: $mid4 margin-bottom: 1.2rem .lensText span user-select: text opacity: 0 animation: fadeIn 333ms forwards ================================================ FILE: src/styles/main.sass ================================================ @use 'vars' as * @use 'landing' @use 'not-found' @use 'nav' @use 'search' @use 'tags' @use 'books' @use 'book-info' @use 'excerpts' @use 'tabs' @use 'settings' @use 'import' @use 'create' @use 'lenses' * box-sizing: border-box margin: 0 padding: 0 user-select: none ::selection background: $fg color: $bg text-shadow: none html font-size: 9px body color: $fg background: $bg font-family: $font font-size: $baseFontSize #root width: 100vw min-height: 100vh display: flex flex-direction: column overflow: hidden main display: flex flex-direction: column flex: 1 padding: 0 6rem @include mobile padding: 0 4.5rem > div margin: 0 auto margin-top: 8rem width: 100% max-width: $maxWidth &.fullWidth max-width: 100% margin-top: 0 &.searchPage margin-top: 0 @include mobile margin-top: 0 h1, h2, h3, h4, h5, h6 font-weight: normal h1 font-size: 5rem line-height: 1.2 margin-bottom: 1rem @include mobile font-size: 4rem h5 font-size: 2rem font-weight: 500 margin-bottom: 1rem p, aside line-height: 1.6 pre, code font-family: $monoFont aside font-style: italic button font-family: $font color: $fg appearance: none background: none outline: none border: none cursor: pointer font-size: $baseFontSize .actionButton position: relative display: inline-block .buttonContent font-family: $font background: $bg padding: 0.5rem 1rem border: 1px solid $fg color: $fg transition: all .2s ease-out will-change: transform user-select: none white-space: nowrap position: relative z-index: 2 .buttonShadow position: absolute display: block background: $fg width: calc(100% - 0.1px) height: 100% top: 2px left: 2px z-index: 1 &:hover .buttonContent transform: translateX(-1px) translateY(-1px) &:active .buttonContent transform: translateX(2px) translateY(2px) transition-duration: .1s &:focus-visible .buttonContent border-color: $accent &:disabled pointer-events: none .buttonContent border-color: $mid3 color: $mid3 .buttonShadow background: $mid3 ul list-style: none a @include linkStyle hr border: none border-bottom: 1px solid $mid3 input, textarea background: none font-family: $font outline: none border: 1px solid $mid2 border-radius: 0 color: $fg &::placeholder color: $mid3 &:focus border-color: $fg input border: none border-bottom: 1px solid $mid2 padding: 0 padding-top: 0.2rem padding-bottom: 0.4rem font-size: $baseFontSize accent-color: $fg textarea border: 1px solid $mid2 padding: 1rem font-family: $monoFont font-size: 1.4rem resize: none display: block width: 100% height: 12rem input[type='range'] accent-color: $fg appearance: none border: none margin-right: 1rem &:focus outline: none border: 0 @mixin rangeThumb appearance: none width: 1.4rem height: 1.4rem border-radius: 100% background: $fg border: 1px solid $bg box-shadow: none transform: translateY(-50%) cursor: ew-resize &::-webkit-slider-thumb @include rangeThumb &::-moz-range-thumb @include rangeThumb transform: none @mixin rangeTrack width: 100% height: 1px background: $mid2 &::-webkit-slider-runnable-track @include rangeTrack &::-moz-range-track @include rangeTrack summary cursor: pointer user-select: none &:hover text-decoration-color: $mid1 &:focus outline: none &::marker font-size: 0.75em progress accent-color: $fg height: 1rem .title font-style: italic .hidden visibility: hidden .icon width: 28px .hint @include fade position: absolute display: flex font-weight: normal align-items: center pointer-events: none width: max-content font-style: italic background: $fg color: $bg font-size: 1.5rem right: -2rem transform: translateX(100%) padding: 0.2rem 0.6rem opacity: 0 line-height: 1.4 padding-left: 0 @include mobile display: none &:before content: '' position: absolute top: 0 left: 0 z-index: -1 transform-origin: 0 0 height: 70.71067812% aspect-ratio: 1 transform: rotate(45deg) background: inherit &.left right: unset padding-left: 0.6rem padding-right: 0 left: -2rem transform: translateX(-100%) &:before left: unset right: 0 transform: translateX(100%) rotate(45deg) &.swapRight @include midWidth transform: translateX(100%) left: unset right: -2rem padding: 0.2rem 0.6rem padding-left: 0 &:before left: 0 right: unset transform: rotate(45deg) *:has(> .hint) display: flex align-items: center &:hover .hint opacity: 1 .smallCaps font-variant: small-caps font-weight: 500 .modeHeading margin: 0 auto margin-bottom: 1rem color: $mid4 display: flex justify-content: center position: relative width: fit-content &.center margin-bottom: 4rem @include mobile margin-bottom: 6rem ul display: flex margin-bottom: 0.6rem li border-left: 1px solid $mid2 text-align: center display: flex align-items: center justify-content: center position: relative &:last-child border-right: 1px solid $mid2 &.active button color: $fg span border-color: $fg .icon opacity: 1 button width: 11.88rem color: $mid4 font-size: 1.6rem display: flex justify-content: center line-height: 1 span padding: 0.6rem 0 font-size: inherit border-bottom: 1px solid transparent display: flex align-items: center &:hover color: $fg span border-color: $mid2 .icon opacity: 1 .icon width: 1.2rem margin-right: 0.3em opacity: .7 .sorter position: absolute bottom: -3.4rem font-size: 1.4rem white-space: nowrap span position: relative border-bottom: none !important .arrow font-size: 1rem padding: 0 line-height: 2.1 transform: translateX(-150%) position: absolute left: 0 &.reverse transform: translateX(-150%) rotate(180deg) @include mobile width: 100% ul width: 100% li flex: 1 button font-size: 1.5rem width: 100% padding: 0 0.6rem footer display: flex flex-direction: column padding: 4rem 0 margin-bottom: 4rem margin-top: auto align-items: center justify-content: center @include mobile padding: 4rem .links margin: 4rem 0 text-align: center a margin: 0 1rem display: inline-block margin-bottom: 1rem .fleuron font-size: 4rem font-family: serif .embeddingProgress flex: 1 justify-content: center display: flex flex-direction: column p font-style: italic progress margin: 0.8rem 0 div display: flex align-items: center p margin-left: 1rem .demoNotice position: fixed bottom: 2rem left: 2rem @include dropShadow background: $bg padding: 3rem z-index: 999 justify-content: center align-items: center border: 1px solid $accent font-size: 2rem animation: hover 2s infinite alternate ease-in-out aside font-size: 1.7rem margin-bottom: 2.5rem button font-size: inherit font-style: inherit margin-top: 1rem @include linkStyle span margin-right: 0.7rem font-size: 2rem @include mobile bottom: 0 left: 0 right: 0 border-bottom: none border-left: none border-right: none text-align: center padding: 2rem font-size: 1.6rem animation: none aside display: none .buttonStack button margin-bottom: 1.5rem p margin-bottom: 4rem .modal position: fixed display: flex justify-content: center align-items: center background: rgba(255, 255, 255, 0.9) inset: 0 z-index: 9999 .modalBox @include dropShadow font-size: 1.8rem border: 1px solid $mid2 padding: 4rem max-width: 80vw background: $bg max-height: 80vh display: flex flex-direction: column h4 font-size: 3rem margin-bottom: 1.5rem .error margin: 3rem 0 font-size: 1.4rem overflow: auto code user-select: text .confirm display: flex justify-content: center button + button margin-left: 2rem button margin-top: 3rem align-self: center .okButton .buttonContent border-color: $accent .buttonShadow background: $accent @keyframes hover from transform: none to transform: translateY(-5px) @keyframes fadeIn from opacity: 0 to opacity: 1 ================================================ FILE: src/styles/nav.sass ================================================ @use 'vars' as * $inset: 1.45rem #root > .logo, .toolbar z-index: 99 #root > .logo position: fixed top: 17px left: 16px width: 34.5px img width: 100% .toolbar position: fixed top: $inset right: $inset display: flex flex-direction: column align-items: center margin-left: auto @include mobile top: 0.8rem right: 0.5rem a, button display: flex align-items: center margin-bottom: 0.8rem &:hover img, .hint opacity: 1 .hint color: $bg ================================================ FILE: src/styles/not-found.sass ================================================ @use 'vars' as * .notFound flex: 1 text-align: center display: flex flex-direction: column justify-content: center h2, h3 font-size: 2.2rem margin-bottom: 2rem h2 font-style: italic h3 margin-bottom: 5rem a color: $mid4 &::before content: '↢' position: absolute transform: translateX(-150%) ================================================ FILE: src/styles/search.sass ================================================ @use 'vars' as * .search max-width: $maxWidth position: relative display: flex align-items: center padding-top: 1.6rem width: 100% margin: 0 auto margin-bottom: 4rem input font-size: 2rem width: 100% border-color: $mid2 text-align: center padding-left: 1.6rem padding-right: 1.6rem &:focus border-color: $accent button position: absolute right: 0 visibility: hidden &.active visibility: visible @media (max-width: 1000px) max-width: calc(100vw - 18rem) @include mobile max-width: calc(100% - 6rem) .searchResults display: flex justify-content: center @media (max-width: 1000px) max-width: calc(100vw - 200px) margin: 0 auto @include mobile flex-direction: column max-width: 100% > * flex: 2 padding: 2rem 0 .modeHeading margin-left: 0 margin-bottom: 4rem li &::after content: '' + li margin-left: 1.6rem @include mobile margin: 0 button width: 22rem position: relative @include mobile width: 100% .count font-size: 1.4rem display: inline-block font-weight: 500 position: absolute bottom: -2.2rem .bookList max-width: 60rem margin: 0 margin-right: 2rem align-items: flex-start justify-content: center display: flex flex-wrap: wrap align-content: flex-start @include mobile margin-bottom: 3rem .snippets flex: 3 max-width: $maxWidth .noResults text-align: center margin-top: 6rem ================================================ FILE: src/styles/settings.sass ================================================ @use 'vars' as * .settings h1 margin-bottom: 4rem h2 margin-bottom: 1.6rem section display: flex @include mobile flex-direction: column > div flex: 1 display: flex flex-direction: column align-items: flex-start &:first-child padding-right: 6rem &:last-child padding-left: 6rem border-left: 1px solid $mid2 @include mobile border: none @include mobile padding: 0 !important div margin-bottom: 1rem button margin-bottom: 1.5rem p margin-bottom: 4rem ul list-style: disc font-size: 1.8rem line-height: 1 margin-bottom: 4rem li margin-bottom: 2rem .colophon font-size: 1.8rem line-height: 2 margin-bottom: 1.5rem ================================================ FILE: src/styles/tabs.sass ================================================ @use 'vars' as * .tabs border-bottom: 1px solid $mid3 button font-size: 1.4rem font-style: italic text-transform: uppercase border: 1px solid transparent border-bottom: none padding: 1rem 1.5rem position: relative top: 1px &.active background: $bg border-color: $mid3 box-shadow: 0 3px 0 0 $bg &:hover color: $mid4 + hr margin-top: 0.4rem ================================================ FILE: src/styles/tags.sass ================================================ @use 'vars' as * .tag margin-right: 1rem margin-bottom: 1rem display: inline-flex align-items: center position: relative &:hover a color: $fg .tagDelete display: block &:after content: '•' margin-left: 1rem color: $mid2 @include mobile font-size: 1.2rem margin-left: 0.6rem &:last-child::after visibility: hidden a font-size: 2rem font-style: italic .count margin-left: 0.4rem margin-top: -0.8rem @include mobile font-size: 1rem &.active a color: $fg text-decoration-color: $accent &.special a font-style: normal .tagDelete font-size: 2rem align-self: flex-end line-height: 0.5 display: none position: absolute top: -0.4em right: 0.1em color: $mid2 transition: color .2s &:hover color: $fg + a color: $mid2 text-decoration: line-through 2px $fg .tagHeader display: flex flex-direction: column align-items: center margin-bottom: 4rem .tabs width: 60rem max-width: 100% text-align: center hr width: 60rem max-width: 100% border: none border-bottom: 1px solid $mid3 .modeHeading margin-top: 1.5rem margin-bottom: 2rem .tags margin-bottom: 2rem .tag:last-child::after content: '' .tagSort display: flex justify-content: center margin: 1rem button margin: 0 1rem &:hover color: $fg &.active color: $fg border-bottom: 2px solid $fg .tagInput margin-top: 0rem display: inline-flex button margin-left: 1rem @include touch display: none input border-color: $mid2 font-size: 2rem &:placeholder-shown + button visibility: hidden ================================================ FILE: src/styles/vars.sass ================================================ $font: 'EB Garamond', ui-serif, serif $monoFont: Consolas, Monaco, Courier, monospace $bg: #fff $mid1: #ddd $mid2: #ccc $mid3: #aaa $mid4: #777 $fg: #333 $accent: #0000ff $highlight: #ffff00 $baseFontSize: 1.6rem $maxWidth: 80rem @mixin linkStyle cursor: pointer color: $fg text-decoration: underline text-decoration-color: $mid1 text-decoration-thickness: 3px transition: text-decoration-color .3s &:hover text-decoration-color: $accent @mixin fade transition: opacity 0.2s @mixin dropShadow box-shadow: 0px 4px 16px rgba(0, 0, 0, 0.1) @mixin mobile @media (max-width: 733px) @content @mixin midWidth @media (max-width: 1270px) @content @mixin touch @media (hover: none) @content ================================================ FILE: src/worker.js ================================================ import * as tf from '@tensorflow/tfjs' import { setWasmPaths, version_wasm as wasmVersion } from '@tensorflow/tfjs-backend-wasm' import {load} from '@tensorflow-models/universal-sentence-encoder' import {createStore, del, delMany, entries, keys, setMany} from 'idb-keyval' const dbNs = 'emdash' const embKey = 'embeddings' const embSize = 512 const semanticSearchLimit = 203 const embsInProgress = {} const embStore = createStore(`${dbNs}:${embKey}`, embKey) const hasDb = new Promise(res => { const attempt = () => { const testNs = `${dbNs}:test` const dbReq = indexedDB.open(testNs) dbReq.onerror = () => { clearTimeout(timeout) res(false) } dbReq.onsuccess = () => { clearTimeout(timeout) res(true) indexedDB.deleteDatabase(testNs) } } let timeout = setTimeout(() => { attempt() timeout = setTimeout(() => res(false), 3000) }, 3000) attempt() }) let excerptEmbMap = {} let bookEmbMap = {} let authorEmbMap = {} let excerptIdToBookId = {} let excerptTensor let bookTensor let authorTensor let excerptKeyList let bookKeyList let authorKeyList let model let demoEmbedP const computeEmbeddings = async pairs => { const tensor = await (await model).embed(pairs.map(([, text]) => text)) const embeddings = await tensor.data() setTimeout(() => tensor.dispose()) return pairs.map(([id], i) => [ id, embeddings.slice(i * embSize, (i + 1) * embSize) ]) } const getTopK = async (tensor, ids, targetEmb, limit, dropFirst) => { const {values, indices} = tf.topk( tf.metrics.cosineProximity(targetEmb, tensor).neg(), Math.min(limit + +!!dropFirst, ids.length), true ) const [scores, inds] = ( await Promise.all([values.array(), indices.array()]) ).map(dropFirst ? xs => xs.slice(1) : xs => xs) return inds.map((n, i) => [ids[n], scores[i]]) } const semanticSearch = async (query, threshold) => { const tensor = await (await model).embed(query) const embedding = await tensor.data() setTimeout(() => tensor.dispose()) return ( await getTopK(excerptTensor, excerptKeyList, embedding, semanticSearchLimit) ).filter(([, v]) => v >= threshold) } const semanticSort = (bookId, exIds) => getTopK( exIds.map(id => excerptEmbMap[id]), exIds, bookEmbMap[bookId], exIds.length ) const computeAverages = (targets, map) => targets.forEach(([collId, ids]) => { if (ids.length) { const embs = ids.flatMap(id => excerptEmbMap[id] || []) if (embs.length) { map[collId] = embs .reduce((a, c) => a.map((n, i) => n + c[i])) .map(n => n / embs.length) } } }) const findExcerptNeighbors = async (targetId, k) => { const targetTitle = excerptIdToBookId[targetId] return ( await getTopK( excerptTensor, excerptKeyList, excerptEmbMap[targetId], excerptKeyList.length, true ) ).reduce( (a, c) => a.length === k ? a : excerptIdToBookId[c[0]] === targetTitle ? a : [...a, c], [] ) } const findBookNeighbors = (bookId, k) => getTopK(bookTensor, bookKeyList, bookEmbMap[bookId], k, true) const findAuthorNeighbors = async (authorId, k) => ( await getTopK(authorTensor, authorKeyList, authorEmbMap[authorId], k + 1) ).filter(([auth]) => auth !== authorId) const processNewExcerpts = async ({excerpts}, cb) => { if ((await hasDb) && !Object.keys(excerptEmbMap).length) { const storedEmbs = await entries(embStore) excerptEmbMap = Object.fromEntries([ ...storedEmbs, ...Object.entries(excerptEmbMap) ]) } excerptIdToBookId = { ...excerptIdToBookId, ...Object.fromEntries(excerpts.map(({id, bookId}) => [id, bookId])) } cb(Object.keys(excerptEmbMap)) } const updateCaches = () => { excerptTensor?.dispose() bookTensor?.dispose() excerptKeyList = Object.keys(excerptEmbMap) bookKeyList = Object.keys(bookEmbMap) excerptTensor = tf.tensor(Object.values(excerptEmbMap)) bookTensor = tf.tensor(Object.values(bookEmbMap)) } const methods = { processNewExcerpts, computeExcerptEmbeddings: ({targets}, cb) => { const [has, needed] = targets.reduce( ([has, needed], [id, text]) => excerptEmbMap[id] ? [[...has, id], needed] : [has, [...needed, [id, text]]], [[], []] ) if (!needed.filter(([id]) => !embsInProgress[id]).length) { return cb(has) } needed.forEach(([id]) => (embsInProgress[id] = true)) computeEmbeddings(needed).then(async embeddings => { cb(embeddings.map(([id]) => id).concat(has)) embeddings.forEach(([id, v]) => { excerptEmbMap[id] = v delete embsInProgress[id] }) if (await hasDb) { setMany(embeddings, embStore) } }) }, computeBookEmbeddings: ({targets}, cb) => { computeAverages(targets, bookEmbMap) updateCaches() cb(null) }, computeAuthorEmbeddings: ({targets}, cb) => { authorTensor?.dispose() computeAverages(targets, authorEmbMap) authorKeyList = Object.keys(authorEmbMap) authorTensor = tf.tensor(Object.values(authorEmbMap)) cb(null) }, requestExcerptNeighbors: async ({target, k}, cb) => cb([target, await findExcerptNeighbors(target, k)]), requestBookNeighbors: async ({target, k}, cb) => cb([target, bookEmbMap[target] ? await findBookNeighbors(target, k) : []]), requestAuthorNeighbors: async ({target, k}, cb) => cb([ target, authorEmbMap[target] ? await findAuthorNeighbors(target, k) : [] ]), requestSemanticRank: async ({bookId, excerptIds}, cb) => cb([ bookId, excerptIds.length ? await semanticSort(bookId, excerptIds) : [] ]), semanticSearch: ({query, threshold}, cb) => semanticSearch(query, threshold).then(matches => cb([query, matches])), deleteExcerpt: async ({targetId, bookId, bookExcerptIds}) => { delete excerptEmbMap[targetId] if (await hasDb) { del(targetId, embStore) } computeAverages([[bookId, bookExcerptIds]], bookEmbMap) updateCaches() }, deleteBook: async ({bookId, bookExcerptIds}) => { delete bookEmbMap[bookId] bookExcerptIds.forEach(id => { delete excerptEmbMap[id] delete excerptIdToBookId[id] }) if (await hasDb) { delMany(bookExcerptIds, embStore) } updateCaches() }, fetchDemoEmbeddings: () => (demoEmbedP = fetch('/demo/embs').then(r => r.arrayBuffer())), setDemoEmbeddings: async ({ids}, cb) => { let buff try { buff = await demoEmbedP } catch (e) { console.log(e) return } const vecSize = embSize * 4 excerptEmbMap = Object.fromEntries( Array(buff.byteLength / vecSize) .fill() .map((_, i) => [ids[i], new Float32Array(buff, i * vecSize, embSize)]) ) cb(ids) }, initWithClear: async (state, cb) => { excerptEmbMap = {} bookEmbMap = {} authorEmbMap = {} excerptIdToBookId = {} excerptTensor?.dispose() bookTensor?.dispose() authorTensor?.dispose() processNewExcerpts(state, cb) if (await hasDb) { keys(embStore).then(ids => { const toKeep = state.excerpts.map(({id}) => id) delMany( ids.filter(k => !toKeep.includes(k)), embStore ) }) } } } const setWasm = () => { setWasmPaths( `https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@${wasmVersion}/dist/` ) return tf.setBackend('wasm') } const start = port => { port.onmessage = ({data: {method, ...payload}}) => methods[method](payload, data => port.postMessage({method, data})) console.log = msg => port.postMessage(msg) model = ( 'OffscreenCanvas' in self ? tf .setBackend('webgl') .then(() => (tf.ENV.flags.HAS_WEBGL ? Promise.resolve() : setWasm())) : setWasm() ) .then(tf.ready) .then(() => console.log(`using ${tf.getBackend()} backend`)) .then(load) } tf.enableProdMode() self.onconnect = e => start(e.ports[0]) self.onerror = e => console.log(e) if (!('SharedWorkerGlobalScope' in self)) { start(self) } ================================================ FILE: src/zip-worker.js ================================================ import JsZip from 'jszip' self.onmessage = ({data}) => { const zip = new JsZip() zip.file('mimetype', 'application/epub+zip') zip.folder('META-INF') zip.folder('OEBPS') data.forEach(([path, text]) => zip.file( path, text .trim() // eslint-disable-next-line .replaceAll(/[^\u0009\u000a\u000d\u0020-\uD7FF\uE000-\uFFFD]/g, '') ) ) zip .generateAsync({type: 'blob'}) .then(zipData => self.postMessage([ `emdash_excerpts_${new Date().toLocaleDateString()}.epub`, zipData ]) ) } self.postMessage(null) ================================================ FILE: vite.config.js ================================================ import {defineConfig} from 'vite' import elmPlugin from 'vite-plugin-elm' import {ViteMinifyPlugin} from 'vite-plugin-minify' export default defineConfig({ publicDir: 'assets', server: {port: 1999}, plugins: [elmPlugin({debug: false}), ViteMinifyPlugin({})], esbuild: {legalComments: 'none'}, css: { preprocessorOptions: { sass: { api: 'modern' } } } })