Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Render titles for social sharing and improve home page sharing #263

Merged
merged 1 commit into from
May 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions src/node/build/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export async function renderPage(
pageServerJsFileName
))
const pageData = JSON.parse(__pageData)
const frontmatterHead = pageData.frontmatter.head

const preloadLinks = [
// resolve imports for index.js + page.md.js and inject script tags for
Expand All @@ -57,22 +56,30 @@ export async function renderPage(
? `<link rel="stylesheet" href="${siteData.base}${cssChunk.fileName}">`
: ''

const title: string =
pageData.title && pageData.title !== 'Home'
? `${pageData.title} | ${siteData.title}`
: siteData.title

const head = addSocialTags(
title,
...siteData.head,
...filterOutHeadDescription(pageData.frontmatter.head)
)

const html = `
<!DOCTYPE html>
<html lang="${siteData.lang}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>
${pageData.title ? pageData.title + ` | ` : ``}${siteData.title}
</title>
<title>${title}</title>
<meta name="description" content="${
pageData.description || siteData.description
}">
${stylesheetLink}
${preloadLinks}
${renderHead(siteData.head)}
${renderHead(frontmatterHead && filterOutHeadDescription(frontmatterHead))}
${renderHead(head)}
</head>
<body>
<div id="app">${content}</div>
Expand Down Expand Up @@ -112,9 +119,6 @@ function resolvePageImports(
}

function renderHead(head: HeadConfig[]) {
if (!head || !head.length) {
return ''
}
return head
.map(([tag, attrs = {}, innerHTML = '']) => {
const openTag = `<${tag}${renderAttrs(attrs)}>`
Expand All @@ -136,13 +140,27 @@ function renderAttrs(attrs: Record<string, string>): string {
}

function isMetaDescription(headConfig: HeadConfig) {
return (
headConfig[0] === 'meta' &&
headConfig[1] &&
headConfig[1].name === 'description'
)
const [type, attrs] = headConfig
return type === 'meta' && attrs?.name === 'description'
}

function filterOutHeadDescription(head: HeadConfig[] | undefined) {
return head ? head.filter((h) => !isMetaDescription(h)) : []
}

function hasTag(head: HeadConfig[], tag: HeadConfig) {
const [tagType, tagAttrs] = tag
const [attr, value] = Object.entries(tagAttrs)[0] // First key
return head.some(([type, attrs]) => type === tagType && attrs[attr] === value)
}

function filterOutHeadDescription(head: HeadConfig[]) {
return head.filter((h) => !isMetaDescription(h))
function addSocialTags(title: string, ...head: HeadConfig[]) {
const tags: HeadConfig[] = [
['meta', { name: 'twitter:title', content: title }],
['meta', { property: 'og:title', content: title }]
]
tags.filter((tagAttrs) => {
if (!hasTag(head, tagAttrs)) head.push(tagAttrs)
})
return head
}