From 7c989fcc682cefcc723fff01c1d3448c038d604c Mon Sep 17 00:00:00 2001 From: Matias Capeletto Date: Thu, 26 Nov 2020 07:03:51 +0100 Subject: [PATCH] feat: add $title and $description computed globals --- docs/guide/global-computed.md | 8 ++++++++ src/client/app/index.ts | 12 ++++++++++++ src/node/markdownToVue.ts | 20 +++++++++++++++++++- types/shared.d.ts | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/guide/global-computed.md b/docs/guide/global-computed.md index 4d2eac0d..80bc190f 100644 --- a/docs/guide/global-computed.md +++ b/docs/guide/global-computed.md @@ -55,6 +55,14 @@ Reference of [\$page](#page).frontmatter. } ``` +## \$title + +Value of the `` label used for the current page. + +## \$description + +The content value of the `<meta name= "description" content= "...">` for the current page. + ## \$themeConfig Refers to `$site.themeConfig`. diff --git a/src/client/app/index.ts b/src/client/app/index.ts index 847e5e70..0bfc2409 100644 --- a/src/client/app/index.ts +++ b/src/client/app/index.ts @@ -92,6 +92,18 @@ export function createApp() { return router.route.data.frontmatter } }, + $title: { + get() { + return router.route.data.title || siteDataByRouteRef.value.title + } + }, + $description: { + get() { + return ( + router.route.data.description || siteDataByRouteRef.value.description + ) + } + }, $themeConfig: { get() { return siteDataByRouteRef.value.themeConfig diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 34594d60..8047a43a 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -3,7 +3,7 @@ import matter from 'gray-matter' import LRUCache from 'lru-cache' import { createMarkdownRenderer, MarkdownOptions } from './markdown/markdown' import { deeplyParseHeader } from './utils/parseHeader' -import { PageData } from '../../types/shared' +import { PageData, HeadConfig } from '../../types/shared' const debug = require('debug')('vitepress:md') const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 }) @@ -41,6 +41,7 @@ export function createMarkdownToVueRenderFn( // inject page data const pageData: PageData = { title: inferTitle(frontmatter, content), + description: inferDescription(frontmatter), frontmatter, headers: data.headers, relativePath: file.replace(/\\/g, '/'), @@ -101,3 +102,20 @@ const inferTitle = (frontmatter: any, content: string) => { } return '' } + +const inferDescription = (frontmatter: Record<string, any>) => { + return getHeadMetaContent(frontmatter.head, 'description') || '' +} + +const getHeadMetaContent = ( + head: HeadConfig[], + name: string +): string | undefined => { + if (!head || !head.length) { + return undefined + } + const meta = head.find(([tag, attrs = {}]) => { + return tag === 'meta' && attrs.name === name && attrs.content + }) + return meta && meta[1].content +} diff --git a/types/shared.d.ts b/types/shared.d.ts index 859176f2..df809e41 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -25,6 +25,7 @@ export type HeadConfig = export interface PageData { title: string + description: string frontmatter: Record<string, any> headers: Header[] relativePath: string