fix: frontmatter description duplication (#194) (#170)

Co-authored-by: Kia King Ishii <kia.king.08@gmail.com>
pull/224/head
Matias Capeletto 4 years ago committed by GitHub
parent c67c5d0a77
commit 338e8453d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,6 +28,8 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
const pageData = route.data const pageData = route.data
const siteData = siteDataByRouteRef.value const siteData = siteDataByRouteRef.value
const pageTitle = pageData && pageData.title const pageTitle = pageData && pageData.title
const pageDescription = pageData && pageData.description
const frontmatterHead = pageData && pageData.frontmatter.head
document.title = (pageTitle ? pageTitle + ` | ` : ``) + siteData.title document.title = (pageTitle ? pageTitle + ` | ` : ``) + siteData.title
updateHeadTags([ updateHeadTags([
['meta', { charset: 'utf-8' }], ['meta', { charset: 'utf-8' }],
@ -42,11 +44,11 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
'meta', 'meta',
{ {
name: 'description', name: 'description',
content: siteData.description content: pageDescription || siteData.description
} }
], ],
...siteData.head, ...siteData.head,
...((pageData && pageData.frontmatter.head) || []) ...((frontmatterHead && filterOutHeadDescription(frontmatterHead)) || [])
]) ])
}) })
} }
@ -61,3 +63,15 @@ function createHeadElement([tag, attrs, innerHTML]: HeadConfig) {
} }
return el return el
} }
function isMetaDescription(headConfig: HeadConfig) {
return (
headConfig[0] === 'meta' &&
headConfig[1] &&
headConfig[1].name === 'description'
)
}
function filterOutHeadDescription(head: HeadConfig[]) {
return head.filter((h) => !isMetaDescription(h))
}

@ -38,6 +38,7 @@ export async function renderPage(
pageServerJsFileName pageServerJsFileName
)) ))
const pageData = JSON.parse(__pageData) const pageData = JSON.parse(__pageData)
const frontmatterHead = pageData.frontmatter.head
const preloadLinks = [ const preloadLinks = [
// resolve imports for index.js + page.md.js and inject script tags for // resolve imports for index.js + page.md.js and inject script tags for
@ -61,11 +62,13 @@ export async function renderPage(
<title> <title>
${pageData.title ? pageData.title + ` | ` : ``}${siteData.title} ${pageData.title ? pageData.title + ` | ` : ``}${siteData.title}
</title> </title>
<meta name="description" content="${siteData.description}"> <meta name="description" content="${
pageData.description || siteData.description
}">
<link rel="stylesheet" href="${siteData.base}${cssChunk.fileName}"> <link rel="stylesheet" href="${siteData.base}${cssChunk.fileName}">
${preloadLinks} ${preloadLinks}
${renderHead(siteData.head)} ${renderHead(siteData.head)}
${renderHead(pageData.frontmatter.head)} ${renderHead(frontmatterHead && filterOutHeadDescription(frontmatterHead))}
</head> </head>
<body> <body>
<div id="app">${content}</div> <div id="app">${content}</div>
@ -127,3 +130,15 @@ function renderAttrs(attrs: Record<string, string>): string {
}) })
.join('') .join('')
} }
function isMetaDescription(headConfig: HeadConfig) {
return (
headConfig[0] === 'meta' &&
headConfig[1] &&
headConfig[1].name === 'description'
)
}
function filterOutHeadDescription(head: HeadConfig[]) {
return head.filter((h) => !isMetaDescription(h))
}

@ -112,11 +112,13 @@ const inferTitle = (frontmatter: any, content: string) => {
} }
const inferDescription = (frontmatter: Record<string, any>) => { const inferDescription = (frontmatter: Record<string, any>) => {
if (!frontmatter.head) { const { description, head } = frontmatter
return ''
if (description !== undefined) {
return description
} }
return getHeadMetaContent(frontmatter.head, 'description') || '' return (head && getHeadMetaContent(head, 'description')) || ''
} }
const getHeadMetaContent = ( const getHeadMetaContent = (

Loading…
Cancel
Save