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 2 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 && rejectHeadDescription(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 rejectHeadDescription(head: HeadConfig[]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move these functions to src/shared so we don't duplicate the implementation across node and client?

Also, thinking maybe filterHeadDescription could be more clear?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree that needs to be moved to shared, but I tried to create a @shared/head file for these helpers before sending the PR but got compilation errors that I did not know how to fix.

About filterHeadDescription, at least for me, it sounds that you are going to get only the head description (instead of filtering it out). I associate it with the positive check condition like filterApples === filter( _ , isApple ). I used reject as in https://lodash.com/docs/4.17.15#reject. But not an issue if that is changed 👍🏼

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried again to move rejectHeadDescription to shared/config.ts, importing it as ../shared/config from node and /@shared/config from client. This is the error I do not know how to resolve:

λ yarn docs-dev
yarn run v1.22.10
$ node ./bin/vitepress dev docs
vitepress v0.9.0
vite v1.0.0-rc.13
internal/modules/cjs/loader.js:1032
  throw err;
  ^

Error: Cannot find module '../dist/node'
Require stack:
- C:\Labs\vitepress\bin\vitepress.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
    at Function.Module._load (internal/modules/cjs/loader.js:898:27)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at Object.<anonymous> (C:\Labs\vitepress\bin\vitepress.js:16:3)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Labs\\vitepress\\bin\\vitepress.js' ]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

FWIW, I think the structure of shared code in Vitepress may change quite a bit once Vitepress is migrated to Vite 2 and this kind of helpers would end up in a rollup plugin shared by both dev and build processes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late check in! Do you have any update on this one? @matias-capeletto 👀 Well Vite 2 here, and maybe we could tackle rollup plugin approach, though I think that's out of this PR's scope.

So maybe renaming method name and lets merge this...?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think if this is going to move to a plugin, the redundancy will be shortlived anyways.
I renamed reject to filterOut, I hope that is clearer.
I also merged master into the branch and everything still looks good.

return head.filter((h: HeadConfig) => !isMetaDescription(h))
}
20 changes: 17 additions & 3 deletions src/node/build/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function renderPage(
pageServerJsFileName
))
const pageData = JSON.parse(__pageData)

const frontmatterHead = pageData.frontmatter.head
const assetPath = `${siteData.base}_assets/`
const preloadLinks = [
// resolve imports for index.js + page.md.js and inject script tags for
Expand All @@ -63,11 +63,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="${assetPath}${cssChunk.fileName}">
${preloadLinks}
${renderHead(siteData.head)}
${renderHead(pageData.frontmatter.head)}
${renderHead(frontmatterHead && rejectHeadDescription(frontmatterHead))}
</head>
<body>
<div id="app">${content}</div>
Expand Down Expand Up @@ -119,3 +121,15 @@ function renderAttrs(attrs: Record<string, string>): string {
})
.join('')
}

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

function rejectHeadDescription(head: HeadConfig[]) {
return head.filter((h: HeadConfig) => !isMetaDescription(h))
}
10 changes: 5 additions & 5 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function createMarkdownToVueRenderFn(
const pageData: PageData = {
title: inferTitle(frontmatter, content),
description: inferDescription(frontmatter),
frontmatter,
frontmatter: frontmatter,
headers: data.headers,
relativePath: file.replace(/\\/g, '/'),
lastUpdated
Expand Down Expand Up @@ -109,11 +109,11 @@ 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