Skip to content

Commit 7d50e10

Browse files
authored
Merge pull request #137 from AlbinoDrought/master
Allow `titleTemplate` to take a function
2 parents c28ef2f + 6283e1e commit 7d50e10

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

src/shared/getMetaInfo.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ export default function _getMetaInfo (options = {}) {
8484

8585
// replace title with populated template
8686
if (info.titleTemplate) {
87-
info.title = info.titleTemplate.replace(/%s/g, info.titleChunk)
87+
if (typeof info.titleTemplate === 'function') {
88+
info.title = info.titleTemplate.call(component, info.titleChunk)
89+
} else {
90+
info.title = info.titleTemplate.replace(/%s/g, info.titleChunk)
91+
}
8892
}
8993

9094
// convert base tag to an array so it can be handled the same way

test/getMetaInfo.spec.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,102 @@ describe('getMetaInfo', () => {
6969
__dangerouslyDisableSanitizers: []
7070
})
7171
})
72+
73+
it('properly uses string titleTemplates', () => {
74+
component = new Vue({
75+
metaInfo: {
76+
title: 'Hello',
77+
titleTemplate: '%s World',
78+
meta: [
79+
{ charset: 'utf-8' }
80+
]
81+
}
82+
})
83+
expect(getMetaInfo(component)).to.eql({
84+
title: 'Hello World',
85+
titleChunk: 'Hello',
86+
titleTemplate: '%s World',
87+
htmlAttrs: {},
88+
headAttrs: {},
89+
bodyAttrs: {},
90+
meta: [
91+
{ charset: 'utf-8' }
92+
],
93+
base: [],
94+
link: [],
95+
style: [],
96+
script: [],
97+
noscript: [],
98+
__dangerouslyDisableSanitizers: []
99+
})
100+
})
101+
102+
it('properly uses function titleTemplates', () => {
103+
const titleTemplate = chunk => `${chunk} Function World`
104+
105+
component = new Vue({
106+
metaInfo: {
107+
title: 'Hello',
108+
titleTemplate: titleTemplate,
109+
meta: [
110+
{ charset: 'utf-8' }
111+
]
112+
}
113+
})
114+
expect(getMetaInfo(component)).to.eql({
115+
title: 'Hello Function World',
116+
titleChunk: 'Hello',
117+
titleTemplate: titleTemplate,
118+
htmlAttrs: {},
119+
headAttrs: {},
120+
bodyAttrs: {},
121+
meta: [
122+
{ charset: 'utf-8' }
123+
],
124+
base: [],
125+
link: [],
126+
style: [],
127+
script: [],
128+
noscript: [],
129+
__dangerouslyDisableSanitizers: []
130+
})
131+
})
132+
133+
it('has the proper `this` binding when using function titleTemplates', () => {
134+
const titleTemplate = function (chunk) {
135+
return `${chunk} ${this.helloWorldText}`
136+
}
137+
138+
component = new Vue({
139+
metaInfo: {
140+
title: 'Hello',
141+
titleTemplate: titleTemplate,
142+
meta: [
143+
{ charset: 'utf-8' }
144+
]
145+
},
146+
data () {
147+
return {
148+
helloWorldText: 'Function World'
149+
}
150+
}
151+
})
152+
expect(getMetaInfo(component)).to.eql({
153+
title: 'Hello Function World',
154+
titleChunk: 'Hello',
155+
titleTemplate: titleTemplate,
156+
htmlAttrs: {},
157+
headAttrs: {},
158+
bodyAttrs: {},
159+
meta: [
160+
{ charset: 'utf-8' }
161+
],
162+
base: [],
163+
link: [],
164+
style: [],
165+
script: [],
166+
noscript: [],
167+
__dangerouslyDisableSanitizers: []
168+
})
169+
})
72170
})

0 commit comments

Comments
 (0)