File: OpenGraph.hs

package info (click to toggle)
haskell-hakyll 4.16.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 928 kB
  • sloc: haskell: 6,504; xml: 44; makefile: 9
file content (71 lines) | stat: -rw-r--r-- 2,467 bytes parent folder | download | duplicates (2)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{- |

Open Graph metadata, as described at <https://ogp.me/>.  This
implementation supports the following properties:

+------------------+----------------------------------------------------+
| @og:type@        | __Hardcoded__ value @"article"@                    |
+------------------+----------------------------------------------------+
| @og:url@         | __Required__ concatenation of @root@ and @url@     |
|                  | context fields, both of which are required.        |
+------------------+----------------------------------------------------+
| @og:title@       | __Required__ title of article, from @title@ field. |
+------------------+----------------------------------------------------+
| @og:description@ | __Optional__ brief description taken from context  |
|                  | field @og-description@, if set.                    |
+------------------+----------------------------------------------------+
| @og:image@       | __Optional__ image URL taken from context          |
|                  | field @og-image@, if set.                          |
+------------------+----------------------------------------------------+

To use, add 'openGraphField' to the template context:

@
let
  context = 'defaultContext' <> …
  postContext = context <> 'openGraphField' "opengraph" context
@

and update the template:

@
\<head>
  \<title>$title$</title>
  \<link rel="stylesheet" type="text\/css" href="\/css\/default.css" />
  $if(opengraph)$$opengraph$$endif$
\</head>
@

See also "Hakyll.Web.Meta.TwitterCard".

-}
module Hakyll.Web.Meta.OpenGraph
  ( openGraphField
  ) where

import Hakyll.Core.Compiler
import Hakyll.Core.Item
import Hakyll.Web.Template
import Hakyll.Web.Template.Context

openGraphField :: String -> Context String -> Context String
openGraphField k ctx = functionField k $ \_args i -> do
  template <- openGraphTemplate
  itemBody <$> applyTemplate template ctx i

openGraphTemplate :: Compiler Template
openGraphTemplate = do
  makeItem openGraphTemplateString >>= compileTemplateItem

openGraphTemplateString :: String
openGraphTemplateString =
  "<meta property=\"og:type\" content=\"article\" />\
  \<meta property=\"og:url\" content=\"$root$$url$\" />\
  \<meta property=\"og:title\" content=\"$title$\" />\
  \$if(og-description)$\
  \<meta property=\"og:description\" content=\"$og-description$\" />\
  \$endif$\
  \$if(og-image)$\
  \<meta property=\"og:image\" content=\"$og-image$\" />\
  \$endif$\
  \"