Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Skip remote urls, inject scripts that contain html properly #2

Closed
wants to merge 3 commits into from

2 participants

@ashaffer

No description provided.

@gabrielflorit

Thanks for this, but the pull request contains a lot of noise (whitespace modification). Could you reissue a pull request with only the new changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 30 deletions.
  1. +38 −30 index.js
View
68 index.js
@@ -5,49 +5,57 @@ var cheerio = require('cheerio');
var replace = require('gulp-replace');
var fs = require('fs');
var path = require('path');
+var url = require('url');
+
+function isLocal(link) {
+ return link && ! url.parse(link).hostname;
+}
module.exports = function() {
- // create a stream through which each file will pass
- return through.obj(function(file, enc, callback) {
+ // create a stream through which each file will pass
+ return through.obj(function(file, enc, callback) {
- if (file.isNull()) {
- this.push(file); // do nothing if no contents
- return callback();
- }
+ if (file.isNull()) {
+ this.push(file); // do nothing if no contents
+ return callback();
+ }
- if (file.isStream()) {
- this.emit('error', new gutil.PluginError('gulp-smoosher', 'Streaming not supported'));
- return callback();
- }
+ if (file.isStream()) {
+ this.emit('error', new gutil.PluginError('gulp-smoosher', 'Streaming not supported'));
+ return callback();
+ }
- if (file.isBuffer()) {
+ if (file.isBuffer()) {
- var rePattern = /<!-- smoosh -->([\s\S]*?)<!-- endsmoosh -->/g;
+ var rePattern = /<!-- smoosh -->([\s\S]*?)<!-- endsmoosh -->/g;
- var input = String(file.contents);
+ var input = String(file.contents);
- var output = input.replace(rePattern, function(match) {
+ var output = input.replace(rePattern, function(match) {
- var $ = cheerio.load(match);
+ var $ = cheerio.load(match);
- $('link').each(function(index, element) {
- var href = $(element).attr('href');
- $(element).replaceWith('<style>' + fs.readFileSync(path.join(file.base, href), 'utf8') + '</style>');
- });
+ $('link').each(function(index, element) {
+ var href = $(element).attr('href');
+ isLocal(href) && $(element).replaceWith('<style>' + fs.readFileSync(path.join(file.base, href), 'utf8') + '</style>');
+ });
- $('script').each(function(index, element) {
- var src = $(element).attr('src');
- $(element).replaceWith('<script>' + fs.readFileSync(path.join(file.base, src), 'utf8') + '</script>');
- });
+ $('script').each(function(index, element) {
+ var src = $(element).attr('src');
+ if(isLocal(src)) {
+ $(element).attr('src', '')
+ .text(fs.readFileSync(path.join(file.base, src)));
+ }
+ });
- return $.html();
- }).replace(/<!-- smoosh -->\n|<!-- endsmoosh -->\n/g, '');
+ return $.html();
+ }).replace(/<!-- smoosh -->\n|<!-- endsmoosh -->\n/g, '');
- file.contents = new Buffer(output);
+ file.contents = new Buffer(output);
- return callback(null, file);
- }
+ return callback(null, file);
+ }
- });
-};
+ });
+};
Something went wrong with that request. Please try again.