diff --git a/README.md b/README.md index 5c61733..661851c 100644 --- a/README.md +++ b/README.md @@ -524,6 +524,43 @@ And in your `./src/index.html`: ``` +### Injecting files contents with dynamic name + +If you want to inject files with just name and ext pattern, add the option [`options.dynamicName`](#optionsdynamicname) with true. + +***Code:*** + +```javascript +gulp.src('./src/index.html') + .pipe(inject(gulp.src(['./src/img/*.svg']), { + starttag: '', + transform: function (filePath, file) { + // return file contents as string + return file.contents.toString('utf8') + } + })) + .pipe(gulp.dest('./dest')); +``` + +And in your `./src/index.html`: + +```html + + + + My index + + + + + + + + + + +``` + ## API ### inject(sources, options) @@ -579,6 +616,13 @@ Default: `"inject"` Used in the default [start](#optionsstarttag) and [end](#optionsendtag) tags below. +#### options.dynamicName +Type: `Boolean` + +Default: `false` + + +If set to `true`, the `{{name}}` of your starttag will be dynamicly replaced with the name oy the file you want to inject. #### options.starttag diff --git a/src/inject/index.js b/src/inject/index.js index 9b440fb..4e26269 100644 --- a/src/inject/index.js +++ b/src/inject/index.js @@ -164,7 +164,11 @@ function getNewContent (target, collection, opt) { var targetExt = extname(target.path); var filesPerTags = groupBy(collection, function (file) { - var ext = extname(file.path); + var ext = extname(file.path), + name = path.basename(file.path, path.extname(file.path)); + if(typeof opt.dynamicName !== 'undefined' && opt.dynamicName) { + opt.tags.name = name; + } var startTag = opt.tags.start(targetExt, ext, opt.starttag); var endTag = opt.tags.end(targetExt, ext, opt.endtag); var tag = startTag + endTag;