Skip to content

Commit

Permalink
Open Graph Protocol metadata for version pages
Browse files Browse the repository at this point in the history
Renders OGP metadata for package documentation pages which could be used
to enable prettier rendering of Elm package links when shared at social
sites, chat rooms and forums.

For example `elm/http` would have following metadata section:

```html
<meta property="og:site_name" content="Elm Packages">
<meta property="og:title" content="http 1.0.0">
<meta property="og:image" content="https://package.elm-lang.org/assets/elm_logo.svg">
<meta property="og:description" content="Make HTTP requests">
```

Could resolve #236.
  • Loading branch information
iiska committed Oct 17, 2018
1 parent a8b9f08 commit f610937
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
39 changes: 39 additions & 0 deletions assets/elm_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions src/backend/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,16 @@ serveVersion memory author project maybeVersion info =
Nothing ->
S.pass

Just (Memory.Summary versions _ _) ->
Just (Memory.Summary versions maybeDetails _) ->
case verifyVersion maybeVersion versions of
Nothing ->
S.pass

Just version ->
case info of
Readme ->
ServeFile.version name version Nothing
ServeFile.version name version Nothing $
maybe Nothing (Just . fst) maybeDetails

Module asset ->
serveVersionHelp name version asset
Expand All @@ -220,7 +221,7 @@ serveVersionHelp name version asset =
_ ->
case Module.fromHyphenPath asset of
Just moduleName ->
ServeFile.version name version (Just moduleName)
ServeFile.version name version (Just moduleName) Nothing

Nothing ->
S.pass
Expand Down
34 changes: 27 additions & 7 deletions src/backend/ServeFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import qualified Elm.Package as Pkg

misc :: B.Builder -> Snap ()
misc title =
makeHtml title mempty
makeHtml title mempty (makeOgpMetadata title Nothing)



Expand All @@ -37,15 +37,18 @@ misc title =

project :: Pkg.Name -> Snap ()
project pkg =
makeHtml (B.stringUtf8 (Pkg.toString pkg)) mempty
let
title = (B.stringUtf8 (Pkg.toString pkg))
in
makeHtml title mempty (makeOgpMetadata title Nothing)



-- VERSION


version :: Pkg.Name -> Pkg.Version -> Maybe Module.Raw -> Snap ()
version pkg@(Pkg.Name _ prjct) vsn maybeName =
version :: Pkg.Name -> Pkg.Version -> Maybe Module.Raw -> Maybe Text.Text -> Snap ()
version pkg@(Pkg.Name _ prjct) vsn maybeName maybeDescription =
let
versionString =
Pkg.versionToString vsn
Expand All @@ -58,6 +61,7 @@ version pkg@(Pkg.Name _ prjct) vsn maybeName =
++ Text.unpack prjct ++ " " ++ versionString
in
makeHtml (B.stringUtf8 title) (makeCanonicalLink pkg maybeName)
(makeOgpMetadata (B.stringUtf8 title) maybeDescription)



Expand Down Expand Up @@ -106,19 +110,35 @@ renames =
(,)


-- OGP METADATA

makeOgpMetadata :: B.Builder -> Maybe Text.Text -> B.Builder
makeOgpMetadata title maybeDescription =
let
description =
maybe "" (B.stringUtf8 . Text.unpack . \d ->
[r|<meta property="og:description" content="|] <> d <> [r|">|]
) maybeDescription
in
[r|<meta property="og:type" content="website">
<meta property="og:site_name" content="Elm Packages">
<meta property="og:title" content="|] <> title <> [r|">
<meta property="og:image" content="https://package.elm-lang.org/assets/elm_logo.svg">|]
<> description


-- SKELETON


makeHtml :: B.Builder -> B.Builder -> Snap ()
makeHtml title canonicalLink =
makeHtml :: B.Builder -> B.Builder -> B.Builder -> Snap ()
makeHtml title canonicalLink ogpMetadata =
writeBuilder $
[r|<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" size="16x16, 32x32, 48x48, 64x64, 128x128, 256x256" href="/assets/favicon.ico">
<title>|] <> title <> [r|</title>|] <> canonicalLink <> [r|
<title>|] <> title <> [r|</title>|] <> canonicalLink <> ogpMetadata <> [r|
<link rel="stylesheet" href="/assets/highlight/styles/default.css?|] <> uniqueToken <> [r|">
<link rel="stylesheet" href="/assets/style.css?|] <> uniqueToken <> [r|">
<script src="/assets/highlight/highlight.pack.js?|] <> uniqueToken <> [r|"></script>
Expand Down

0 comments on commit f610937

Please sign in to comment.