From f61093739e8fbb78443e235bff42f7f3da1ea77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhamatti=20Niemel=C3=A4?= Date: Wed, 17 Oct 2018 20:11:12 +0300 Subject: [PATCH] Open Graph Protocol metadata for version pages 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 ``` Could resolve #236. --- assets/elm_logo.svg | 39 +++++++++++++++++++++++++++++++++++++++ src/backend/Main.hs | 7 ++++--- src/backend/ServeFile.hs | 34 +++++++++++++++++++++++++++------- 3 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 assets/elm_logo.svg diff --git a/assets/elm_logo.svg b/assets/elm_logo.svg new file mode 100644 index 00000000..7d669f0a --- /dev/null +++ b/assets/elm_logo.svg @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/backend/Main.hs b/src/backend/Main.hs index e10eb8c9..39037d76 100644 --- a/src/backend/Main.hs +++ b/src/backend/Main.hs @@ -187,7 +187,7 @@ 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 @@ -195,7 +195,8 @@ serveVersion memory author project maybeVersion info = 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 @@ -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 diff --git a/src/backend/ServeFile.hs b/src/backend/ServeFile.hs index 82225444..7f3c49d3 100644 --- a/src/backend/ServeFile.hs +++ b/src/backend/ServeFile.hs @@ -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) @@ -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 @@ -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) @@ -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||] + ) maybeDescription + in + [r| + + + |] + <> 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| - |] <> title <> [r||] <> canonicalLink <> [r| + |] <> title <> [r||] <> canonicalLink <> ogpMetadata <> [r|