aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue/Tag.hs
blob: 9c0c98f2f20a3cbf27b27c02b0f93b325c02d9b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Issue.Tag (Tag (..), extractTags, internalTags) where

import Data.Binary (Binary)
import Data.Maybe (catMaybes)
import Data.Text (Text)
import Data.Text qualified as T
import GHC.Generics (Generic)

data Tag = Tag Text Text deriving (Show, Generic, Binary)

extractTags :: Text -> [Tag]
extractTags =
  catMaybes
    . map
      ( ( \case
            ((T.uncons -> Just ('@', k)) : v) ->
              Just (Tag k (T.unwords v))
            _ -> Nothing
        )
          . T.words
      )
    . T.lines

internalTags :: Text -> [Tag]
internalTags title =
  [ Tag "id" (toSpinalCase title)
  ]

toSpinalCase :: Text -> Text
toSpinalCase = T.replace " " "-" . T.filter keep . T.toLower
  where
    keep = (`elem` (concat [[' ', '-'], ['a' .. 'z'], ['0' .. '9']]))