-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.js
121 lines (106 loc) · 3.78 KB
/
gulpfile.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
/*
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
'use strict';
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const del = require('del');
const fs = require('fs');
const hljs = require('highlight.js');
const markdownIt = require('markdown-it')({
html: true,
highlight: (code, lang) => {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, code).value;
} catch (__) { console.log(__) }
} else {
try {
return hljs.highlightAuto(code).value;
} catch (__) { console.log(__) }
}
return ''; // use external default escaping
}
});
const markdownItAttrs = require('markdown-it-attrs');
const merge = require('merge-stream');
const runSequence = require('run-sequence');
const toc = require('toc');
markdownIt.use(markdownItAttrs);
// keep markdownIt from escaping template markup.
markdownIt.normalizeLink = function(link) { return link; }
markdownIt.validateLink = function(link) { return true; }
function convertMarkdownToHtml(templateName) {
return $.grayMatter(function(file) { // pull out front matter data.
const data = file.data;
data.file = file;
data.title = data.title || '';
data.subtitle = data.subtitle || '';
let content = file.content;
// Inline code snippets before running through markdown for syntax highlighting.
content = content.replace(/<!--\s*include_file\s*([^\s]*)\s*-->/g,
(match, src) => fs.readFileSync(`app/${src}`));
// Markdown -> HTML.
content = markdownIt.render(content);
// If there is a table of contents, toc-ify it. Otherwise, wrap the
// original markdown content anyway, so that we can style it.
if (content.match(/<!--\s*toc\s*-->/gi)) {
data.content = toc.process(content, {
TOC: '<h3 id="table-of-contents">Table of Contents</h3><%= toc %>',
tocMax: 3,
anchor: function(header, attrs) {
// if we have an ID attribute, use that, otherwise
// use the default slug
var id = attrs.match(/(?:^|\s+)id="([^"]*)"/)
return id ? id[1] : toc.anchor(header);
}
});
} else {
data.content = content;
}
const tmpl = fs.readFileSync(templateName);
const renderTemplate = $.util.template(tmpl);
return renderTemplate(data);
});
}
gulp.task('md:blog', Object.assign(function() {
return gulp.src([
'app/blog/*.md',
], {base: 'app/'})
.pipe(convertMarkdownToHtml('templates/base-blog.html'))
.pipe($.rename({extname: '.html'}))
.pipe(gulp.dest('dist'));
}, {
description: 'Blog markdown -> HTML conversion. Syntax highlight and TOC generation',
}));
gulp.task('copy', Object.assign(function() {
const app = gulp.src([
'app/**/*',
'!app/**/*.md',
'app.yaml',
'redirects.yaml',
'server.py'
], {nodir: true})
.pipe(gulp.dest('dist'));
const templates = gulp.src([
'templates/*.html'
])
.pipe(gulp.dest('dist/templates'));
return merge(app, templates);
}, {
description: 'Copy site files (polyfills, templates, etc.) to dist/',
}));
gulp.task('clean', Object.assign(function() {
return del('dist');
}, {
description: 'Remove built files',
}));
// Default task. Build the dest dir.
gulp.task('default', Object.assign(gulp.series('clean', 'copy', 'md:blog'), {
description: 'Build site',
}));