Procházet zdrojové kódy

Add data constructors for link elements

jherve před 1 rokem
rodič
revize
f6960acff5

+ 10 - 0
src/LinkedIn.purs

@@ -20,6 +20,7 @@ import Effect (Effect)
 import Partial.Unsafe (unsafePartial)
 import Web.DOM (Document, Node)
 import Web.DOM.Document as D
+import Web.DOM.Element (getAttribute)
 import Web.DOM.Element as E
 import Web.DOM.Node (nodeName, nodeType, textContent)
 import Web.DOM.Node as N
@@ -76,6 +77,7 @@ queryAll' constructor selector doc = do
 
 data DetachedNode =
   DetachedElement {tag :: String, content :: String, id :: Maybe String, classes :: List String}
+  | DetachedA {content :: String, href :: String}
   | DetachedComment String
   | DetachedText String
 
@@ -118,6 +120,14 @@ toDetached node = unsafePartial $ toDetached' (nodeType node) node where
       tag = E.tagName el
     elementToDetached el tag txt
 
+  elementToDetached el "A" text = do
+    href <- getAttribute "href" el
+
+    pure $ DetachedA {
+      content: normalize text,
+      href: unsafePartial $ fromJust href
+    }
+
   elementToDetached el tag text = do
     id <- E.id el
     -- On SVG elements "className" returns a weird "SVGString" type that cannot be trimmed

+ 3 - 1
src/LinkedIn/Profile/Utils.purs

@@ -9,7 +9,7 @@ import Data.List as L
 import Data.Maybe (Maybe(..))
 import LinkedIn (DetachedNode(..))
 import LinkedIn.UIElements.Parser (uiElementP)
-import LinkedIn.UIElements.Types (UIElement)
+import LinkedIn.UIElements.Types (UIElement(..))
 import Parsing (runParser, ParseError)
 
 maybeGetInList ::
@@ -38,3 +38,5 @@ toUIElement ∷ DetachedNode → Either ParseError UIElement
 toUIElement (DetachedElement {content}) = runParser content uiElementP
 toUIElement (DetachedComment str) = runParser str uiElementP
 toUIElement (DetachedText str) = runParser str uiElementP
+toUIElement (DetachedA {content, href}) = map toLink $ runParser content uiElementP
+  where toLink ui = UILink href ui

+ 1 - 0
src/LinkedIn/UIElements/Types.purs

@@ -39,6 +39,7 @@ data UIElement =
   | UITimeSpan TimeSpan
   | UIPlainText String
   | UIDotSeparated UIElement UIElement
+  | UILink String UIElement
 
 derive instance Generic UIElement _
 instance Show UIElement where