forked from microsoft/vscode-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.blog.js
133 lines (107 loc) · 4.02 KB
/
gulpfile.blog.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var gulp = require('gulp');
var es = require('event-stream');
var markdownIt = require('markdown-it');
var frontMatter = require('gulp-front-matter');
var rename = require('gulp-rename');
var keybindings = require('./keybindings/doc.keybindings');
var imagemin = require('gulp-imagemin');
var File = require('vinyl');
var common = require('./gulpfile.common');
var slash = require('gulp-slash');
var BLOG_ROOT = 'blogs';
var DEST_ROOT = 'out/vscode-website/src';
var blogs = [];
function mapFileToBlogArticle(file) {
return {
Area: file.data.Area ? file.data.Area.toLowerCase() : null,
Link: slash(file.relative.substr(0, file.relative.lastIndexOf('.'))),
Title: file.data.PageTitle,
NavTitle: file.data.TOCTitle,
MetaDescription: file.data.MetaDescription,
MetaTags: !file.data.MetaTags ? [] : (file.data.MetaTags).join(','),
Sections: [],
Order: file.data.Order,
Content: "",
File: null,
Date: file.data.Date,
Author: file.data.Author,
ShortDescription: file.data.ShortDescription,
MetaSocialImage: file.data.MetaSocialImage
};
}
function renderTemplate(file, article) {
var template = common.swigCompiler('scripts/templates/blog-template.html');
var result = template(article);
file.contents = new Buffer(result, 'utf8');
article.File = file;
return file;
}
gulp.task('copy-blog-images', function () {
console.log('Copying blog images..');
var images = gulp.src([BLOG_ROOT + '/**/images/**/*.{png,PNG,jpg,JPG,svg,SVG}']).pipe(imagemin());
var gifs = gulp.src([BLOG_ROOT + '/**/images/**/*.{gif,GIF}']);
return es.merge([images, gifs])
.pipe(rename(function (path) {
path.basename = path.dirname + '_' + path.basename;
path.dirname = '';
}))
.pipe(gulp.dest(DEST_ROOT + '/dist'));
;})
gulp.task('compile-blog-markdown', function () {
var sources = [
BLOG_ROOT + '/**/**/**/*.md',
'!' + BLOG_ROOT + '/README.md'
];
console.log('Parsing blog MD, applying templates...');
return gulp.src(sources)
.pipe(frontMatter({
property: 'data',
remove: true
}))
.pipe(es.mapSync(function (file) {
var blogArticle = mapFileToBlogArticle(file);
console.log("Compiling Blog: " + blogArticle.Title);
blogArticle = common.compileMarkdown(file, blogArticle);
if (blogArticle.Order) {
blogs.push(blogArticle);
}
return renderTemplate(file, blogArticle);
}))
.pipe(rename({ extname: '.handlebars' }))
.pipe(gulp.dest(DEST_ROOT + '/views/blogs'));
});
gulp.task('compile-blog', ['compile-blog-markdown', 'copy-blog-images'], function () {
console.log('Creating blog index...');
var template = common.swigCompiler('scripts/templates/blog-navigation-template.html');
blogs = blogs.sort(function (a, b) {
return parseFloat(b.Order) - parseFloat(a.Order);
});
var latest = new File({
path: 'latest.handlebars',
contents: blogs[0].File.contents
});
es.readArray([latest])
.pipe(gulp.dest(DEST_ROOT + '/views/blogs'));
var file = new File({
path: 'blogNavigation.handlebars',
contents: new Buffer(template({ articles: blogs }))
});
compileAtomFeed();
return es.readArray([file])
.pipe(gulp.dest(DEST_ROOT + '/views/partials'));
});
function compileAtomFeed() {
console.log('Creating atom feed...');
var feed = common.swigCompiler('scripts/templates/blog-feed.template.xml');
var FEED_LIMIT = 20;
var feedXml = new File({
path: 'feed.xml',
contents: new Buffer(feed({articles: blogs.slice(0, FEED_LIMIT), latest: blogs[0]}))
})
es.readArray([feedXml])
.pipe(gulp.dest(DEST_ROOT + '/public'));
};