-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgulpfile.js
More file actions
82 lines (72 loc) · 1.99 KB
/
gulpfile.js
File metadata and controls
82 lines (72 loc) · 1.99 KB
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
var gulp = require('gulp');
var del = require('del');
var markdown = require('metalsmith-markdown');
var layouts = require('metalsmith-layouts');
var contentful = require('contentful');
var fs = require('fs');
/**
* PLEASE NOTE!
* Don't use a parent directory ("..") as we do it
* here for simplicity (you can clone the repo and
* tutorial works with the current version).
*
* When using this library as a package installed
* with npm, you have to require "gulp-metalsmith":
*
* const metalsmith = require('gulp-metalsmith');
*/
var metalsmith = require('..');
gulp.task('default', ['watch']);
gulp.task('clean', function () {
del('build/**');
});
gulp.task('watch', function () {
gulp.watch('src/**', ['metalsmith-json']);
});
gulp.task('metalsmith', ['clean'], function () {
return gulp.src('src/**')
.pipe(metalsmith({
use: [
markdown(),
layouts({engine: 'swig'})
]
}))
.pipe(gulp.dest('build'));
});
gulp.task('metalsmith-json', ['clean'], function () {
return gulp.src('src/**')
.pipe(metalsmith({
ignore: 'src/*.md',
json: true,
use: [
markdown(),
layouts({engine: 'swig'})
]
}))
.pipe(gulp.dest('build'));
});
gulp.task('contentful', function () {
// Both space ID and access token can be found in the "APIs" section
var client = contentful.createClient({
space: '49ewbt9gbyem',
accessToken: '4ffd55500ba0dbae29b4eefb033298f1e9f09df91d65cfb02eb480f5b33775c3'
});
client.getEntries({
content_type: 'article',
order: 'fields.order'
}).then(function (entries) {
var pages = preparePages(entries);
fs.writeFileSync('src/pages.json', JSON.stringify(pages, null, 2));
});
});
function preparePages(entries) {
return entries.items.reduce(function (acc, item) {
acc[item.fields.slug.replace(/\.html$/, '.md')] = {
title: item.fields.title,
order: item.fields.order,
layout: 'basic.swig',
contents: item.fields.content
};
return acc;
}, {});
}