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: frontmatter global and computed global docs #152

Merged
merged 7 commits into from
Nov 26, 2020
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
1 change: 1 addition & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function getGuideSidebar() {
text: 'Advanced',
children: [
{ text: 'Frontmatter', link: '/guide/frontmatter' },
{ text: 'Global Computed', link: '/guide/global-computed' },
{ text: 'Customization', link: '/guide/customization' }
]
}
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ editLink: true
---
```

Between the triple-dashed lines, you can set [predefined variables](#predefined-variables), or even create custom ones of your own. These variables can be used via the <code>\$page.frontmatter</code> variable.
Between the triple-dashed lines, you can set [predefined variables](#predefined-variables), or even create custom ones of your own. These variables can be used via the <code>$frontmatter</code> variable.

Here’s an example of how you could use it in your Markdown file:

Expand All @@ -19,7 +19,7 @@ title: Docs with VitePress
editLink: true
---

# {{ $page.frontmatter.title }}
# {{ $frontmatter.title }}

Guide content
```
Expand Down
73 changes: 73 additions & 0 deletions docs/guide/global-computed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Global Computed

In VitePress, some core [computed properties](https://v3.vuejs.org/guide/computed.html#computed-properties) can be used by the default theme or custom themes. Or directly in Markdown pages using vue, for example using `$frontmatter.title` to access the title defined in the frontmatter section of the page.

## $site

This is the `$site` value of the site you're currently reading:

```js
{
base: '/',
lang: 'en-US',
title: 'VitePress',
description: 'Vite & Vue powered static site generator.',
head: [],
locales: {},
themeConfig: $themeConfig
}
```

## $themeConfig

Refers to `$site.themeConfig`.

```js
{
locales: {},
repo: 'vuejs/vitepress',
docsDir: 'docs',
editLinks: true,
editLinkText: 'Edit this page on GitHub',
lastUpdated: 'Last Updated',
nav: [...],
sidebar: { ... }
}
```

## $page

This is the `$page` value of the page you're currently reading:

```js
{
relativePath: 'guide/global-computed.md',
title: 'Global Computed',
headers: [
{ level: 2, title: '$site', slug: 'site' },
{ level: 2, title: '$page', slug: '$page' },
...
],
frontmatter: $frontmatter,
lastUpdated: 1606297645000
}
```

## $frontmatter

Reference of `$page.frontmatter`.

```js
{
title: 'Docs with VitePress',
editLink: true
}
```

## $title

Value of the `<title>` label used for the current page.

## $description

The content value of the `<meta name= "description" content= "...">` for the current page.
21 changes: 19 additions & 2 deletions src/client/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,31 @@ export function createApp() {
return siteDataByRouteRef.value
}
},
$themeConfig: {
get() {
return siteDataByRouteRef.value.themeConfig
}
},
$page: {
get() {
return router.route.data
}
},
$theme: {
$frontmatter: {
get() {
return siteDataByRouteRef.value.themeConfig
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
)
}
}
})
Expand Down
13 changes: 3 additions & 10 deletions src/client/theme-default/components/NavBarTitle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@
:aria-label="`${$site.title}, back to home`"
>
<img
v-if="$theme.logo"
v-if="$themeConfig.logo"
class="logo"
:src="withBase($theme.logo)"
:src="withBase($themeConfig.logo)"
alt="Logo"
/>
{{ $site.title }}
</a>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
<script setup lang="ts">
import { withBase } from '../utils'

export default defineComponent({
setup() {
return { withBase }
}
})
</script>

<style scoped>
Expand Down
26 changes: 25 additions & 1 deletion src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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, '/'),
Expand Down Expand Up @@ -106,3 +107,26 @@ const inferTitle = (frontmatter: any, content: string) => {
}
return ''
}

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

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
}
5 changes: 3 additions & 2 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export type HeadConfig =
| [string, Record<string, string>, string]

export interface PageData {
relativePath: string
title: string
frontmatter: Record<string, any>
description: string
headers: Header[]
relativePath: string
frontmatter: Record<string, any>
lastUpdated: number
}

Expand Down