diff --git a/Readme.md b/Readme.md index 91cbe8f..a6b9faa 100644 --- a/Readme.md +++ b/Readme.md @@ -227,6 +227,45 @@ data.json ``` +### `webRoot` built-in context variable + +The `webRoot` field of the context contains the relative path from the source document to +the source root (unless the value is already set in the context options). + +### example + +support/contact/index.html + +```html + + + + + + +

Support Contact Info

+ + + + +``` + +and the result is: + +```html + + + + + + +

Support Contact Info

+ + + + +``` + ### License MIT diff --git a/lib/index.js b/lib/index.js index b2fb1d1..452de7f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,7 +29,15 @@ module.exports = function(opts) { opts.basepath = opts.basepath === '@root' ? process.cwd() : path.resolve(opts.basepath); } + var customWebRoot = !!opts.context.webRoot; + function fileInclude(file, enc, cb) { + if (!customWebRoot) { + // built-in webRoot variable, example usage: + opts.context.webRoot = + path.relative(path.dirname(file.path), file.base).replace(/\\/g, '/') || '.'; + } + if (file.isNull()) { cb(null, file); } else if (file.isStream()) { diff --git a/test/fixtures-webroot-variable/html-head.html b/test/fixtures-webroot-variable/html-head.html new file mode 100644 index 0000000..ab90560 --- /dev/null +++ b/test/fixtures-webroot-variable/html-head.html @@ -0,0 +1,3 @@ + + + diff --git a/test/fixtures-webroot-variable/index.html b/test/fixtures-webroot-variable/index.html new file mode 100644 index 0000000..22d9244 --- /dev/null +++ b/test/fixtures-webroot-variable/index.html @@ -0,0 +1,9 @@ + + + @@include('html-head.html') + +

Page

+ About + @@include('sub/home-link.html') + + diff --git a/test/fixtures-webroot-variable/result.html b/test/fixtures-webroot-variable/result.html new file mode 100644 index 0000000..6bf051d --- /dev/null +++ b/test/fixtures-webroot-variable/result.html @@ -0,0 +1,13 @@ + + + + + + + +

Page

+ About + Home + + + diff --git a/test/fixtures-webroot-variable/sub/home-link.html b/test/fixtures-webroot-variable/sub/home-link.html new file mode 100644 index 0000000..7cb318c --- /dev/null +++ b/test/fixtures-webroot-variable/sub/home-link.html @@ -0,0 +1 @@ +Home diff --git a/test/fixtures-webroot-variable/sub/index.html b/test/fixtures-webroot-variable/sub/index.html new file mode 100644 index 0000000..c2fe275 --- /dev/null +++ b/test/fixtures-webroot-variable/sub/index.html @@ -0,0 +1,9 @@ + + + @@include('../html-head.html') + +

Sub Page

+ About + @@include('home-link.html') + + diff --git a/test/fixtures-webroot-variable/sub/result.html b/test/fixtures-webroot-variable/sub/result.html new file mode 100644 index 0000000..608e076 --- /dev/null +++ b/test/fixtures-webroot-variable/sub/result.html @@ -0,0 +1,13 @@ + + + + + + + +

Sub Page

+ About + Home + + + diff --git a/test/webroot-variable.js b/test/webroot-variable.js new file mode 100644 index 0000000..fb375d4 --- /dev/null +++ b/test/webroot-variable.js @@ -0,0 +1,46 @@ +'use strict'; + +const fileIncludePlugin = require('..'); +const gutil = require('gulp-util'); +const should = require('should'); +const fs = require('fs'); + +describe('## built-in webRoot variable', () => { + + it('# regular usage and includes', done => { + var result = fs.readFileSync('test/fixtures-webroot-variable/result.html', 'utf8'); + var path = 'test/fixtures-webroot-variable/index.html'; + var file = new gutil.File({ path: path, contents: fs.createReadStream(path) }); + + var stream = fileIncludePlugin(); + stream.on('data', newFile => { + should.exist(newFile); + should.exist(newFile.contents); + + String(newFile.contents).should.equal(result); + done(); + }); + + stream.write(file); + stream.end(); + }); + + it('# nested folder', done => { + var result = fs.readFileSync('test/fixtures-webroot-variable/sub/result.html', 'utf8'); + var path = 'test/fixtures-webroot-variable/sub/index.html'; + var file = new gutil.File({ path: path, contents: fs.createReadStream(path) }); + + var stream = fileIncludePlugin(); + stream.on('data', newFile => { + should.exist(newFile); + should.exist(newFile.contents); + + String(newFile.contents).should.equal(result); + done(); + }); + + stream.write(file); + stream.end(); + }); + +});