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

Add Rib.Extra.OpenGraph #118

Merged
merged 4 commits into from
Mar 9, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- API exposes the CLI parser (`optparse-applicative`) for user-level composition
- Add `Rib.Parser.Pandoc.getToC` returning rendered Table of contents for a Pandoc document
- `Rib.Extra.CSS`: add `googleFonts` and `stylesheet`
- Add `Rib.Extra.OpenGraph` for Open Graph protocol
- Bug fixes:
- `routeUrl`: Fix incorrect substitution of "foo-index.html" with "foo-"

Expand Down
1 change: 1 addition & 0 deletions rib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ library
Rib.Route
Rib.Shake
Rib.Extra.CSS
Rib.Extra.OpenGraph
other-modules:
Rib.Server
hs-source-dirs: src
Expand Down
49 changes: 49 additions & 0 deletions src/Rib/Extra/OpenGraph.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Meta tags for The Open Graph protocol: https://ogp.me/
module Rib.Extra.OpenGraph
( OpenGraph (..),
)
where

import Lucid
import Lucid.Base (makeAttribute)
import Relude
import qualified Text.URI as URI

-- The OpenGraph metadata
--
-- This type can be directly rendered to HTML using `toHTML`.
data OpenGraph
= OpenGraph
{ _openGraph_title :: Text,
_openGraph_author :: Text,
_openGraph_description :: Maybe Text,
_openGraph_siteName :: Text,
_openGraph_type :: Text, -- TODO: ADT (different types with their own properties)
_openGraph_image :: Maybe URI.URI
}
deriving (Eq, Show)

instance ToHtml OpenGraph where
toHtmlRaw = toHtml
toHtml OpenGraph {..} = do
meta' "author" _openGraph_author
meta' "description" `mapM_` _openGraph_description
metaOg "title" _openGraph_title
metaOg "site_name" _openGraph_siteName
metaOg "type" _openGraph_type
whenJust _openGraph_image $ \uri -> do
if isJust (URI.uriScheme uri)
then metaOg "image" $ URI.render uri
else error $ "OGP image URL must be absolute. This URI is not: " <> URI.render uri
where
-- Open graph meta element
metaOg k v =
meta_
[ makeAttribute "property" $ "og:" <> k,
content_ v
]
meta' k v = meta_ [name_ k, content_ v]