Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linkify sponsor description #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Helpers.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module Helpers exposing (formatTemperature)
module Helpers exposing (formatTemperature, linkify)

import Html.Styled exposing (Html, text, a, div)
import Html.Styled.Attributes exposing (href)


primitiveRound : String -> Int -> Maybe String
Expand Down Expand Up @@ -28,3 +31,21 @@ formatTemperature temp =

Nothing ->
"invalid"


linkify : String -> Html msg
linkify string =
div [] (List.map linkifyWord (String.words string))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method currently fails, if the URL isn't terminated with a space. So if it is at the end of a sentence the URL would include the dot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe we should work with RegEx instead? Then we'd be a bit more flexible. Although I like the simplicity of List.map on String.words.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe we should work with RegEx instead? Then we'd be a bit more flexible. Although I like the simplicity of List.map on String.words.

I had a fist version which just worked with String.words and then concatenated the text back together. But I then failed to create a Html object out of it which didn't escape the HTML in the string.



linkifyWord : String -> Html msg
linkifyWord word =
if isUrl word then
a [href word] [text word]
else
text (word ++ " ")


isUrl : String -> Bool
isUrl =
flip String.startsWith >> (flip List.any) [ "http://", "https://" ]
4 changes: 2 additions & 2 deletions src/Views.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Css exposing (..)
import Css.Foreign as Foreign
import Css.Reset
import Dict
import Helpers exposing (formatTemperature)
import Helpers exposing (formatTemperature, linkify)
import Html.Styled exposing (Html, fromUnstyled)
import Html.Styled exposing (h1, h2, h3, h4, h5, h6, div, p, text, a, img, strong, footer)
import Html.Styled.Attributes as Attr exposing (id, class, css, src, href)
Expand Down Expand Up @@ -306,7 +306,7 @@ sensorDescription now sensor sponsor =
[ text sponsor.name ]
, p
[]
[ text sponsor.description ]
[ linkify sponsor.description ]
]
)
sponsor
Expand Down