Add Head.rootLanguage. Fixes #157.

This commit is contained in:
Dillon Kearns 2021-04-23 08:00:05 -07:00
parent 938bbd1014
commit ef8728961d
3 changed files with 95 additions and 5 deletions

View File

@ -317,9 +317,10 @@ async function compileCliApp(options) {
* @returns {string}
*/
function wrapHtml(fromElm, contentJsonString) {
const seoData = seo.gather(fromElm.head);
/*html*/
return `<!DOCTYPE html>
<html lang="en">
${seoData.rootElement}
<head>
<link rel="stylesheet" href="/style.css"></link>
<link rel="preload" href="/elm-pages.js" as="script">
@ -350,7 +351,7 @@ function wrapHtml(fromElm, contentJsonString) {
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
${seo.toString(fromElm.head)}
${seoData.headTags}
</head>
<body>
<div data-url="" display="none"></div>

View File

@ -1,4 +1,45 @@
module.exports = { toString };
module.exports = { toString, gather };
/** @typedef { { type: 'root'; keyValuePair: [string, string] } } RootTagModifier */
/**
* @param {( SeoTag | RootTagModifier )[]} tags
*/
function gather(tags) {
const withoutRootModifiers = tags.flatMap((value) => {
if (value.type === "root") {
return [];
} else {
return [value];
}
});
const rootModifiers = tags.flatMap((value) => {
if (value.type === "root") {
return [value];
} else {
return [];
}
});
return {
rootElement: headTag(rootModifiers),
headTags: toString(withoutRootModifiers),
};
}
/**
* @param {RootTagModifier[]} rootModifiers
*/
function headTag(rootModifiers) {
const rootModifiersMap = Object.fromEntries(
rootModifiers.map((modifier) => modifier.keyValuePair)
);
if (!("lang" in rootModifiersMap)) {
rootModifiersMap["lang"] = "en";
}
return `<html ${Object.entries(rootModifiersMap)
.map(pairToAttribute)
.join(" ")}>`;
}
function toString(/** @type { SeoTag[] } */ tags) {
return tags
@ -30,3 +71,11 @@ function appendJsonLdTag(/** @type {JsonLdTag} */ tagDetails) {
${JSON.stringify(tagDetails.contents)}
</script>`;
}
/**
*
* @param {[string, string]} param0
* @returns string
*/
function pairToAttribute([name, value]) {
return `${name}="${value}"`;
}

View File

@ -1,6 +1,6 @@
module Head exposing
( Tag, metaName, metaProperty
, rssLink, sitemapLink
, rssLink, sitemapLink, rootLanguage
, structuredData
, AttributeValue
, currentPageFullUrl, fullImageUrl, fullPageUrl, raw
@ -18,7 +18,7 @@ But this module might be useful if you have a special use case, or if you are
writing a plugin package to extend `elm-pages`.
@docs Tag, metaName, metaProperty
@docs rssLink, sitemapLink
@docs rssLink, sitemapLink, rootLanguage
## Structured Data
@ -44,6 +44,7 @@ writing a plugin package to extend `elm-pages`.
-}
import Json.Encode
import LanguageTag exposing (LanguageTag)
import MimeType
import Pages.ImagePath as ImagePath exposing (ImagePath)
import Pages.Internal.String as String
@ -56,6 +57,7 @@ through the `head` function.
type Tag
= Tag Details
| StructuredData Json.Encode.Value
| RootModifier String String
type alias Details =
@ -287,6 +289,38 @@ appleTouchIcon maybeSize image =
|> node "link"
{-| Set the language for a page.
<https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang>
import Head
import LanguageTag
import LanguageTag.Language
LanguageTag.Language.de -- sets the page's language to German
|> LanguageTag.build LanguageTag.emptySubtags
|> Head.rootLanguage
This results pre-rendered HTML with a global lang tag set.
```html
<html lang="no">
...
</html>
```
-}
rootLanguage : LanguageTag -> Tag
rootLanguage languageTag =
languageTag
|> LanguageTag.toString
|> RootModifier "lang"
-- TODO rootDirection for
filterMaybeValues : List ( String, Maybe a ) -> List ( String, a )
filterMaybeValues list =
list
@ -390,6 +424,12 @@ toJson canonicalSiteUrl currentPagePath tag =
, ( "type", Json.Encode.string "json-ld" )
]
RootModifier key value ->
Json.Encode.object
[ ( "type", Json.Encode.string "root" )
, ( "keyValuePair", Json.Encode.list Json.Encode.string [ key, value ] )
]
encodeProperty : String -> String -> ( String, AttributeValue ) -> Json.Encode.Value
encodeProperty canonicalSiteUrl currentPagePath ( name, value ) =