forked from ashaffer/gulp-inline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
239 lines (208 loc) · 5.41 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* Imports
*/
var through = require('through2')
var cheerio = require('cheerio')
var gulp = require('gulp')
var url = require('url')
var path = require('path')
var fs = require('fs')
/**
* Inlinable type map
*/
var typeMap = {
css: {
tag: 'link',
template: function (contents, el) {
var attribute = el.attr('media')
attribute = attribute ? ' media="' + attribute + '" ' : ''
return '<style' + attribute + '>\n' + String(contents) + '\n</style>'
},
filter: function (el) {
return el.attr('rel') === 'stylesheet' && isLocal(el.attr('href'))
},
getSrc: function (el) {
return el.attr('href')
}
},
js: {
tag: 'script',
template: function (contents, el) {
var str = stringifyAttrs(without(el[0].attribs, 'src'))
return '<script ' + str + '>\n' + String(contents) + '\n</script>'
},
filter: function (el) {
return isLocal(el.attr('src'))
},
getSrc: function (el) {
return el.attr('src')
}
},
img: {
tag: 'img',
template: function (contents, el) {
el.attr('src', 'data:image/unknown;base64,' + contents.toString('base64'))
return cheerio.html(el)
},
filter: function (el) {
var src = el.attr('src')
return !/\.svg$/.test(src)
},
getSrc: function (el) {
return el.attr('src')
}
},
svg: {
tag: ['img', 'svg'],
template: function (contents, el) {
var tag = el[0].tagName,
$ = cheerio.load(String(contents), {decodeEntities: false})
switch (tag) {
case 'img':
return cheerio.html($('svg').attr(without(el.attr(), 'src')))
break
case 'svg':
return cheerio.html($('svg').removeAttr('id').attr(without(el.attr(), 'src')))
break
}
},
filter: function (el) {
var tag = el[0].tagName
switch (tag) {
case 'img':
var src = el.attr('src')
return /\.svg$/.test(src) && isLocal(src)
break
case 'svg':
var children = el.children(),
child = children.first(),
src = child.attr('xlink:href')
// Return TRUE if element contains only one child element and
// that child element is a <use> element with xlink:href
// attribute which refers to some random ID (which according to
// HTML5 spec should contain at least one character and shouldn't
// contain spaces) in external SVG file
return children.length === 1
&& child[0].tagName === 'use'
&& /\.svg#\S+$/.test(src)
break
}
},
getSrc: function (el) {
var tag = el[0].tagName
switch (tag) {
case 'img':
return el.attr('src')
case 'svg':
// Return "/foo.svg" out of "/foo.svg#SomeIdentifier"
return el.children().first().attr('xlink:href').match(/[^#]+/)[0]
break
}
}
}
}
function inject ($, process, base, cb, opts, relative, ignoredFiles) {
var items = []
if (!process) {
process = noop
}
// Normalize tags
var tags = opts.tag instanceof Array ? opts.tag : [opts.tag]
tags.forEach(function(tag) {
$(tag).each(function (idx, el) {
el = $(el)
if (opts.filter(el)) {
items.push(el)
}
})
})
if (items.length) {
var done = after(items.length, cb)
items.forEach(function (el) {
var src = opts.getSrc(el) || ''
var file = path.join(src[0] === '/' ? base : relative, src)
if (fs.existsSync(file) && ignoredFiles.indexOf(src) === -1) {
gulp.src(file)
.pipe(process())
.pipe(replace(el, opts.template))
.pipe(through.obj(function (file, enc, cb) {
cb()
}, done))
} else {
done()
}
})
} else {
cb()
}
}
/**
* Inline plugin
*/
function inline (opts) {
opts = opts || {}
opts.base = opts.base || ''
opts.ignore = opts.ignore || []
opts.disabledTypes = opts.disabledTypes || []
return through.obj(function (file, enc, cb) {
var self = this
var $ = cheerio.load(String(file.contents), {decodeEntities: false})
var typeKeys = Object.getOwnPropertyNames(typeMap)
var done = after(typeKeys.length, function () {
file.contents = new Buffer($.html())
self.push(file)
cb()
})
typeKeys.forEach(function (type) {
if (opts.disabledTypes.indexOf(type) === -1) {
inject($, opts[type], opts.base, done, typeMap[type], path.dirname(file.path), opts.ignore)
} else {
done()
}
})
})
}
/**
* Utilities
*/
function replace (el, tmpl) {
return through.obj(function (file, enc, cb) {
el.replaceWith(tmpl(file.contents, el))
this.push(file)
cb()
})
}
function noop () {
return through.obj(function (file, enc, cb) {
this.push(file)
cb()
})
}
function after (n, cb) {
var i = 0
return function () {
i++
if (i === n) cb.apply(this, arguments)
}
}
function isLocal (href) {
return href && href.slice(0, 2) !== '//' && !url.parse(href).hostname
}
function without (o, keys) {
keys = [].concat(keys)
return Object.keys(o).reduce(function (memo, key) {
if (keys.indexOf(key) === -1) {
memo[key] = o[key]
}
return memo
}, {})
}
function stringifyAttrs (attrs) {
return Object.keys(attrs).map(function (key) {
return [key, attrs[key]].join('=')
}).join(' ')
}
/**
* Exports
*/
module.exports = inline