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

fix: frontmatter description duplication (#194) #170

Merged
merged 6 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions src/client/app/composables/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
const pageData = route.data
const siteData = siteDataByRouteRef.value
const pageTitle = pageData && pageData.title
const pageDescription = pageData && pageData.description
const frontmatterHead = pageData && pageData.frontmatter.head
document.title = (pageTitle ? pageTitle + ` | ` : ``) + siteData.title
updateHeadTags([
['meta', { charset: 'utf-8' }],
Expand All @@ -42,11 +44,11 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
'meta',
{
name: 'description',
content: siteData.description
content: pageDescription || siteData.description
}
],
...siteData.head,
...((pageData && pageData.frontmatter.head) || [])
...((frontmatterHead && filterOutHeadDescription(frontmatterHead)) || [])
])
})
}
Expand All @@ -61,3 +63,15 @@ function createHeadElement([tag, attrs, innerHTML]: HeadConfig) {
}
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))
}
19 changes: 17 additions & 2 deletions src/node/build/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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 @@ -61,11 +62,13 @@ export async function renderPage(
<title>
${pageData.title ? pageData.title + ` | ` : ``}${siteData.title}
</title>
<meta name="description" content="${siteData.description}">
<meta name="description" content="${
pageData.description || siteData.description
}">
<link rel="stylesheet" href="${siteData.base}${cssChunk.fileName}">
${preloadLinks}
${renderHead(siteData.head)}
${renderHead(pageData.frontmatter.head)}
${renderHead(frontmatterHead && filterOutHeadDescription(frontmatterHead))}
</head>
<body>
<div id="app">${content}</div>
Expand Down Expand Up @@ -127,3 +130,15 @@ function renderAttrs(attrs: Record<string, string>): string {
})
.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))
}
8 changes: 5 additions & 3 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ const inferTitle = (frontmatter: any, content: string) => {
}

const inferDescription = (frontmatter: Record<string, any>) => {
if (!frontmatter.head) {
return ''
const { description, head } = frontmatter

if (description !== undefined) {
return description
}

return getHeadMetaContent(frontmatter.head, 'description') || ''
return (head && getHeadMetaContent(head, 'description')) || ''
}

const getHeadMetaContent = (
Expand Down