This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdato.config.js
142 lines (133 loc) · 4.99 KB
/
dato.config.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const htmlTag = require('html-tag');
const fs = require('fs');
const lodash = require('lodash');
// This function helps transforming structures like:
//
// [{ tagName: 'meta', attributes: { name: 'description', content: 'foobar' } }]
//
// into proper HTML tags:
//
// <meta name="description" content="foobar" />
const toHtml = (tags) =>
tags
.map(({tagName, attributes, content}) =>
htmlTag(tagName, attributes, content),
)
.join('');
// Arguments that will receive the mapping function:
//
// * dato: lets you easily access any content stored in your DatoCMS
// administrative area;
//
// * root: represents the root of your project, and exposes commands to
// easily create local files/directories;
//
// * i18n: allows to switch the current locale to get back content in
// alternative locales from the first argument.
//
// Read all the details here:
// https://github.com/datocms/js-datocms-client/blob/master/docs/dato-cli.md
module.exports = (dato, root, i18n) => {
// Add to the existing Hugo config files some properties coming from data
// stored on DatoCMS
['config.dev.toml', 'config.prod.toml'].forEach((file) => {
root.addToDataFile(file, 'toml', {
title: dato.site.globalSeo.siteName,
languageCode: i18n.locale,
});
});
const content = {
name: dato.site.globalSeo.siteName,
language: dato.site.locales[0],
intro: dato.home.introText,
copyright: dato.home.copyright,
// iterate over all the `social_profile` item types
socialProfiles: dato.socialProfiles.map((profile) => {
return {
type: profile.profileType.toLowerCase().replace(/ +/, '-'),
url: profile.url,
};
}),
faviconMetaTags: toHtml(dato.site.faviconMetaTags),
seoMetaTags: toHtml(dato.home.seoMetaTags),
};
// Create a YAML data file to store global data about the site
root.createDataFile('data/settings.yml', 'yaml', content);
// Create a markdown file with content coming from the `about_page` item
// type stored in DatoCMS
root.createPost(`content/about/about.md`, 'yaml', {
frontmatter: {
title: dato.aboutPage.title,
subtitle: dato.aboutPage.subtitle,
photo: dato.aboutPage.photo.url({
w: 800,
fm: 'jpg',
auto: 'compress',
}),
seoMetaTags: toHtml(dato.aboutPage.seoMetaTags),
},
content: dato.aboutPage.bio,
});
root.createPost(`content/imprint/_index.md`, 'yaml', {
frontmatter: {
title: dato.imprintPage.title,
seoMetaTags: toHtml(dato.imprintPage.seoMetaTags),
},
content: dato.imprintPage.text,
});
root.createPost(`content/blog/_index.md`, 'yaml', {
frontmatter: {
title: dato.blogPage.title,
seoMetaTags: toHtml(dato.blogPage.seoMetaTags),
},
content: dato.blogPage.text,
});
// Create a `category` directory
root.directory('content/categories', (categoriesDir) => {
// ...and for each of the category stored online...
dato.categories.forEach((category, index) => {
// ...create a markdown file with all the metadata in the frontmatter
categoriesDir.directory(category.slug, (categorieDir) => {
categorieDir.createPost('_index.md', 'yaml', {
frontmatter: {
title: category.name,
slug: category.slug,
},
});
});
});
});
// Create a `post` directory (or empty it if already exists)...
root.directory('content/post', (dir) => {
// ...and for each of the post stored online...
dato.posts.forEach((post, index) => {
// ...create a markdown file with all the metadata in the frontmatter
dir.createPost(`${post.slug}.md`, 'yaml', {
frontmatter: {
//postall: post.toMap(),
title: post.title,
description: post.description,
seoMetaTags: toHtml(post.seoMetaTags),
publishedDate: post.createdAt,
heroVideo: post.heroVideo,
heroImage: post.heroImage.url({
h: 765,
auto: 'compress',
}),
credits: post.credits.toMap(),
listImage: post.heroImage.url({
crop: 'top, right',
}),
featured: post.featured,
content: post.content.toMap(),
category: post.category.slug,
weight: index,
categoryData: {
title: post.category.name,
slug: post.category.slug,
},
},
});
});
});
};