1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.buildSite = undefined;
|
7 |
|
8 | var _showdown = require('showdown');
|
9 |
|
10 | var _glob = require('glob');
|
11 |
|
12 | var _fs = require('fs');
|
13 |
|
14 | var _lodash = require('lodash');
|
15 |
|
16 | var _path = require('path');
|
17 |
|
18 | var _path2 = _interopRequireDefault(_path);
|
19 |
|
20 | var _moment = require('moment');
|
21 |
|
22 | var _moment2 = _interopRequireDefault(_moment);
|
23 |
|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25 |
|
26 |
|
27 |
|
28 | var markdownConverter = new _showdown.Converter({
|
29 | simplifiedAutoLink: true,
|
30 | strikethrough: true,
|
31 | tables: true
|
32 | });
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var buildTemplate = function buildTemplate(templateString, data, step) {
|
38 | step = step || 1;
|
39 | if (step >= 10) {
|
40 | console.error('Reached maximum nesting level', step);
|
41 | return templateString;
|
42 | }
|
43 | var previousResult = templateString;
|
44 | var result = (0, _lodash.template)(templateString)(data);
|
45 | if (result === previousResult) {
|
46 | return result;
|
47 | }
|
48 | return buildTemplate(result, data, ++step);
|
49 | };
|
50 |
|
51 | var isPage = function isPage(entry) {
|
52 | return entry.sys.contentType.sys.id === 'page';
|
53 | };
|
54 | var isPost = function isPost(entry) {
|
55 | return entry.sys.contentType.sys.id === 'post';
|
56 | };
|
57 | var isAuthor = function isAuthor(entry) {
|
58 | return entry.sys.contentType.sys.id === 'author';
|
59 | };
|
60 |
|
61 | var buildPostContent = function buildPostContent(post, locale) {
|
62 | var content = {};
|
63 | Object.keys(post.fields).forEach(function (k) {
|
64 | switch (k) {
|
65 | case 'content':
|
66 | case 'abstract':
|
67 | content[k] = markdownConverter.makeHtml(post.fields[k][locale]);
|
68 | break;
|
69 | case 'hero':
|
70 | content[k] = {
|
71 | title: post.fields[k][locale].fields.title[locale],
|
72 | file: post.fields[k][locale].fields.file[locale]
|
73 | };
|
74 | break;
|
75 | case 'author':
|
76 | content[k] = {
|
77 | name: post.fields[k][locale].fields.name[locale],
|
78 | slug: post.fields[k][locale].fields.slug[locale]
|
79 | };
|
80 | break;
|
81 | default:
|
82 | content[k] = post.fields[k][locale];
|
83 | }
|
84 | });
|
85 | return content;
|
86 | };
|
87 |
|
88 | var buildAuthorContent = function buildAuthorContent(author, locale) {
|
89 | var content = {};
|
90 | Object.keys(author.fields).forEach(function (k) {
|
91 | switch (k) {
|
92 | case 'description':
|
93 | content[k] = markdownConverter.makeHtml(author.fields[k][locale]);
|
94 | break;
|
95 | case 'photo':
|
96 | content[k] = {
|
97 | title: author.fields[k][locale].fields.title[locale],
|
98 | file: author.fields[k][locale].fields.file[locale]
|
99 | };
|
100 | break;
|
101 | default:
|
102 | content[k] = author.fields[k][locale];
|
103 | }
|
104 | });
|
105 | return content;
|
106 | };
|
107 |
|
108 | var buildPageContent = function buildPageContent(page, locale) {
|
109 | var content = {};
|
110 | Object.keys(page.fields).forEach(function (k) {
|
111 | switch (k) {
|
112 | case 'content':
|
113 | content[k] = markdownConverter.makeHtml(page.fields[k][locale]);
|
114 | break;
|
115 | default:
|
116 | content[k] = page.fields[k][locale];
|
117 | }
|
118 | });
|
119 | return content;
|
120 | };
|
121 |
|
122 | var shortDate = function shortDate(date) {
|
123 | return (0, _moment2.default)(date).format('DD.MM.YYYY');
|
124 | };
|
125 | var striptags = function striptags(str) {
|
126 | return str.replace(/<[^>]+>/g, '');
|
127 | };
|
128 |
|
129 |
|
130 | var buildPage = function buildPage(build, config, collections, content, identifier, template, includesFiles) {
|
131 | var includes = {};
|
132 | var page = {
|
133 | url: config.webHost + config.baseHref + identifier + '.html'
|
134 | };
|
135 | var templateData = {
|
136 | build: Object.assign({ identifier: identifier }, build),
|
137 | config: config,
|
138 | content: content,
|
139 | collections: collections,
|
140 | includes: includes,
|
141 | shortDate: shortDate,
|
142 | striptags: striptags,
|
143 | page: page
|
144 | };
|
145 |
|
146 |
|
147 | Object.keys(includesFiles).forEach(function (k) {
|
148 | includes[k] = buildTemplate(includesFiles[k], templateData);
|
149 | });
|
150 |
|
151 |
|
152 | var pageTemplate = buildTemplate((0, _fs.readFileSync)(template, 'utf8'), templateData);
|
153 |
|
154 |
|
155 | (0, _fs.writeFileSync)('build/' + identifier + '.html', pageTemplate);
|
156 |
|
157 |
|
158 | console.log('build/' + identifier + '.html');
|
159 |
|
160 | return templateData;
|
161 | };
|
162 |
|
163 | var buildSite = exports.buildSite = function buildSite(contentFile, templateDir, version, locale) {
|
164 | var environment = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'production';
|
165 |
|
166 | var content = JSON.parse((0, _fs.readFileSync)(contentFile, 'utf-8'));
|
167 |
|
168 |
|
169 | var includesDir = _path2.default.join(templateDir, '/includes/');
|
170 | var includesFiles = {};
|
171 | (0, _glob.sync)(includesDir + '*.html').map(function (f) {
|
172 | includesFiles[f.replace(includesDir, '').replace(/\.html$/, '')] = (0, _fs.readFileSync)(f, 'utf8');
|
173 | });
|
174 |
|
175 | var build = {
|
176 | environment: environment,
|
177 | version: version,
|
178 | time: Date.now(),
|
179 | locale: locale
|
180 | };
|
181 |
|
182 |
|
183 | var config = {};
|
184 | content.entries.filter(function (e) {
|
185 | return e.sys.contentType.sys.id === 'config';
|
186 | }).map(function (c) {
|
187 | var key = c.fields.key[build.locale];
|
188 | config[key] = process.env['CONFIG_' + key.toUpperCase()] || c.fields.value[build.locale];
|
189 | });
|
190 |
|
191 |
|
192 | var collections = {};
|
193 | content.entries.filter(function (e) {
|
194 | return e.sys.contentType.sys.id === 'collection';
|
195 | }).map(function (c) {
|
196 | collections[c.fields.title[build.locale]] = c.fields.items[build.locale].map(function (e) {
|
197 | if (isPage(e)) return buildPageContent(e, build.locale);
|
198 | if (isPost(e)) return buildPostContent(e, build.locale);
|
199 | });
|
200 | });
|
201 |
|
202 |
|
203 | var posts = content.entries.filter(function (e) {
|
204 | return isPost(e);
|
205 | }).map(function (post) {
|
206 | var content = buildPostContent(post, build.locale);
|
207 | return buildPage(build, config, collections, content, content.slug, _path2.default.join(templateDir, '/post.html'), includesFiles);
|
208 | });
|
209 |
|
210 |
|
211 | content.entries.filter(function (e) {
|
212 | return isAuthor(e);
|
213 | }).map(function (page) {
|
214 | var content = buildAuthorContent(page, build.locale);
|
215 | buildPage(build, config, collections, content, content.slug, _path2.default.join(templateDir, '/author.html'), includesFiles);
|
216 | });
|
217 |
|
218 |
|
219 | content.entries.filter(function (e) {
|
220 | return isPage(e);
|
221 | }).map(function (page) {
|
222 | var content = buildPageContent(page, build.locale);
|
223 | buildPage(build, config, collections, content, content.slug, _path2.default.join(templateDir, '/page.html'), includesFiles);
|
224 | });
|
225 |
|
226 |
|
227 | buildPage(build, config, collections, { posts: posts }, 'index', _path2.default.join(templateDir, '/index.html'), includesFiles);
|
228 | }; |
\ | No newline at end of file |