-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdatoMetaTags.js
More file actions
35 lines (32 loc) · 1020 Bytes
/
datoMetaTags.js
File metadata and controls
35 lines (32 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const htmlTag = require('html-tag')
const { modifyNodes } = require('reshape-plugin-util')
module.exports = function datoMetaTags () {
return function datoMetaTagsPlugin (tree, ctx) {
if (ctx) {
Object.assign(ctx.runtime, { htmlTag })
}
return modifyNodes(
tree,
(node) => node.name === 'dato-meta-tags',
(node) => {
if (!(node.attrs && node.attrs.record)) {
throw new ctx.PluginError({
message: 'dato-meta-tags tag has no "record" attribute',
plugin: 'spike-datocms',
location: node.location
})
}
return {
type: 'code',
content: `(function() {
const record = locals.${node.attrs.record[0].content};
if (!record || !record.seoMetaTags) { return null; }
return record.seoMetaTags.map(tag => (
__runtime.htmlTag(tag.tagName, tag.attributes || {}, tag.content)
)).join('');
})()`
}
}
)
}
}