Skip to content

Add {{path}} parsing to 'start-' and 'endtag' #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,41 @@ And in your `./src/index.html`:
</html>
```

### Injecting files contents based on file path

In order to inject files based on file path you have to provide custom `starttag` which includes `{{path}}`. Additionally, in order to inject file contents include `transform` function, that will return file contents as string. You also have to omit `{read: false}` option of `gulp.src` in this case. Path can be either absolute, or relative in which case you should set [`options.relative`] to true. Example below shows how to inject contents of html partials into `index.html`:

***Code:***

```javascript
gulp.src('./src/index.html')
.pipe(inject(gulp.src(['./src/partials/head/*.html']), {
starttag: '<!-- inject:{{path}} -->',
relative: true,
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
<!DOCTYPE html>
<html>
<head>
<title>My index</title>
<!-- inject:path/to/your/file.ext -->
<!-- contents of html partials will be injected here -->
<!-- endinject -->
</head>
<body>
</body>
</html>
```

## API

### inject(sources, options)
Expand Down Expand Up @@ -621,7 +656,7 @@ When `true` all tags without corresponding files will be emptied.
**Purpose:**

Used to dynamically set starting placeholder tag depending on file extensions.
In the provided string, or the string returned from the given function, the string `{{ext}}` is replaced with the source file extension name, e.g. "css", "js" or "html". `{{name}}` will be replaced by [`options.name`](#optionsname).
In the provided string, or the string returned from the given function, the string `{{ext}}` is replaced with the source file extension name, e.g. "css", "js" or "html". `{{name}}` will be replaced by [`options.name`](#optionsname). `{{path}}` will be replaced by path to source file (when used together with [`options.relative`] it will allow relative path to source file.

##### Default:

Expand All @@ -646,7 +681,7 @@ A function dependent on target file type and source file type that returns:
**Purpose:**

Used to dynamically set ending placeholder tag depending on file extensions.
In the provided string, or the string returned from the given function, the string `{{ext}}` is replaced with the source file extension name, e.g. "css", "js" or "html". `{{name}}` will be replaced by [`options.name`](#optionsname).
In the provided string, or the string returned from the given function, the string `{{ext}}` is replaced with the source file extension name, e.g. "css", "js" or "html". `{{name}}` will be replaced by [`options.name`](#optionsname). `{{path}}` will be replaced by path to source file.

##### Default:

Expand Down
1 change: 1 addition & 0 deletions src/inject/expected/customTagsWithPath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><!-- /fixtures/partial.html: --><div>Partial body</div><!-- :/fixtures/partial.html --><h1>Hello world</h1>
1 change: 1 addition & 0 deletions src/inject/fixtures/partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Partial body</div>
1 change: 1 addition & 0 deletions src/inject/fixtures/templateTagsWithPath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><!-- /fixtures/partial.html: --><!-- :/fixtures/partial.html --><h1>Hello world</h1>
12 changes: 7 additions & 5 deletions src/inject/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function getNewContent(target, collection, opt) {
}
var content = String(target.contents);
var targetExt = extname(target.path);
var files = prepareFiles(collection, targetExt, opt);
var files = prepareFiles(collection, targetExt, opt, target);
var filesPerTags = groupArray(files, 'tagKey');
var startAndEndTags = Object.keys(filesPerTags);
var matches = [];
Expand Down Expand Up @@ -219,11 +219,12 @@ function getLeadingWhitespace(str) {
return str.match(LEADING_WHITESPACE_REGEXP)[0];
}

function prepareFiles(files, targetExt, opt) {
function prepareFiles(files, targetExt, opt, target) {
return files.map(function (file) {
var ext = extname(file.path);
var startTag = getTagRegExp(opt.tags.start(targetExt, ext, opt.starttag), ext, opt);
var endTag = getTagRegExp(opt.tags.end(targetExt, ext, opt.endtag), ext, opt);
var filePath = getFilepath(file, target, opt);
var startTag = getTagRegExp(opt.tags.start(targetExt, ext, opt.starttag), ext, opt, filePath);
var endTag = getTagRegExp(opt.tags.end(targetExt, ext, opt.endtag), ext, opt, filePath);
var tagKey = String(startTag) + String(endTag);
return {
file,
Expand All @@ -235,10 +236,11 @@ function prepareFiles(files, targetExt, opt) {
});
}

function getTagRegExp(tag, sourceExt, opt) {
function getTagRegExp(tag, sourceExt, opt, sourcePath) {
tag = makeWhiteSpaceOptional(escapeStringRegexp(tag));
tag = replaceVariables(tag, {
name: opt.name,
path: sourcePath,
ext: sourceExt === '{{ANY}}' ? '.+' : sourceExt
});
return new RegExp(tag, 'ig');
Expand Down
19 changes: 19 additions & 0 deletions src/inject/inject_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ describe('gulp-inject', function () {
streamShouldContain(stream, ['customTagsWithExt.html'], done);
});

it('should replace {{path}} in starttag and endtag with current file path if specified', function (done) {
var target = src(['templateTagsWithPath.html'], {read: true});
var sources = src([
'template.html',
'partial.html',
'template2.html'
], {read: true});

var stream = target.pipe(inject(sources, {
starttag: '<!-- {{path}}: -->',
endtag: '<!-- :{{path}} -->',
transform: function (filePath, file) {
return file.contents.toString('utf8');
}
}));

streamShouldContain(stream, ['customTagsWithPath.html'], done);
});

it('should replace existing data within start and end tag', function (done) {
var target = src(['templateWithExistingData.html'], {read: true});
var sources = src([
Expand Down