Skip to content

Commit 79b9939

Browse files
committed
infer title
1 parent ef9715f commit 79b9939

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

Diff for: lib/app/Content.js

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
export default {
22
created () {
33
if (this.$ssrContext) {
4-
const title = getMetadata(this, 'title')
5-
if (title) {
6-
this.$ssrContext.title = title
7-
}
8-
9-
const lang = getMetadata(this, 'lang')
10-
if (lang) {
11-
this.$ssrContext.lang = lang
12-
}
4+
this.$ssrContext.title = getMetadata(this, 'title') || 'VuePress'
5+
this.$ssrContext.lang = getMetadata(this, 'lang') || 'en'
136
}
147
},
158
mounted () {
16-
const title = getMetadata(this, 'title')
17-
if (title) {
18-
document.title = title
19-
}
20-
21-
const lang = getMetadata(this, 'lang')
22-
if (lang) {
23-
document.documentElement.lang = lang
9+
const updateMeta = () => {
10+
document.title = getMetadata(this, 'title') || 'VuePress'
11+
document.documentElement.lang = getMetadata(this, 'lang') || 'en'
2412
}
13+
this.$watch('$page', updateMeta)
14+
updateMeta()
2515
},
2616
render (h) {
2717
const componentName = this.$page.path === '/'
@@ -35,5 +25,5 @@ export default {
3525
}
3626

3727
function getMetadata (vm, key) {
38-
return vm.$page.frontmatter[key] || vm.$page[key]
28+
return vm.$page.frontmatter[key] || vm.$page[key] || vm.$site[key]
3929
}

Diff for: lib/build.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ module.exports = async function build (sourceDir) {
4747
await Promise.all(options.siteData.pages.map(async (page) => {
4848
const context = {
4949
url: page.path,
50-
userHeadTags,
51-
title: 'VuePress',
52-
lang: 'en'
50+
userHeadTags
5351
}
5452

5553
let html

Diff for: lib/prepare.js

+7
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ async function resolveOptions (sourceDir) {
6060
const data = {
6161
path: urlPath
6262
}
63+
64+
// extract yaml frontmatter
6365
const frontmatter = yaml.loadFront(content)
66+
// infer title
67+
const titleMatch = frontmatter.__content.match(/^#+\s+(.*)/)
68+
if (titleMatch) {
69+
data.title = titleMatch[1]
70+
}
6471
delete frontmatter.__content
6572
if (Object.keys(frontmatter).length) {
6673
data.frontmatter = frontmatter

0 commit comments

Comments
 (0)