html/social-card.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
854ffdf86b42e43b3811f561d3ca8544db768420
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

// A reusable article page with safe social metadata, built with std:html.
//
// The page is assembled from ordinary Protos functions and structured nodes;
// no raw HTML is concatenated. The URLs are inert example data.

Html: import("std:html")
Arrays: import("std:collections/array")

socialHead: (article) => {
    Html.head(
        Html.meta(%{"charset": "utf-8"}),
        Html.title(article.title),
        Html.meta(%{"name": "description"; "content": article.summary}),
        Html.link(%{"rel": "canonical"; "href": article.url}),
        Html.meta(%{"property": "og:type"; "content": "article"}),
        Html.meta(%{"property": "og:site_name"; "content": article.site}),
        Html.meta(%{"property": "og:title"; "content": article.title}),
        Html.meta(%{"property": "og:description"; "content": article.summary}),
        Html.meta(%{"property": "og:url"; "content": article.url}),
        Html.meta(%{"property": "og:image"; "content": article.image.url}),
        Html.meta(%{"property": "og:image:alt"; "content": article.image.alt}),
        Html.meta(%{"property": "og:image:width"; "content": article.image.width}),
        Html.meta(%{"property": "og:image:height"; "content": article.image.height}),
        Html.meta(%{"name": "twitter:card"; "content": "summary_large_image"}),
        Html.meta(%{"name": "twitter:title"; "content": article.title}),
        Html.meta(%{"name": "twitter:description"; "content": article.summary}),
        Html.meta(%{"name": "twitter:image"; "content": article.image.url}),
        Html.meta(%{"name": "twitter:image:alt"; "content": article.image.alt})
    )
}

articlePage: (article) => {
    Html.fragment(
        Html.doctype(),
        Html.html(%{"lang": "en"},
            socialHead(article),
            Html.body(
                Html.h1(article.title),
                ...Arrays.map(article.paragraphs, (text) => { Html.p(text) }),
                Html.ul(...Arrays.map(article.links, (link) => {
                    Html.li(Html.a(%{"href": link.href}, link.label))
                }))
            )
        )
    )
}

article: {
    site: "Example Notes"
    title: "Objects <without> classes"
    summary: "How delegation replaces inheritance & keeps objects small."
    url: "https://www.example.com/notes/objects"
    image: {
        url: "https://www.example.com/notes/objects.png"
        alt: "Two objects connected by a delegation arrow"
        width: "1200"
        height: "630"
    }
    paragraphs: Array(
        "Every object can serve as a prototype.",
        "Untrusted text such as <script> stays text."
    )
    links: Array(
        { href: "/notes"; label: "All notes" },
        { href: "https://www.example.com/about"; label: "About" }
    )
}

print(Html.render(articlePage(article)))