From fbaad21413e813352e979d677f6643af1f5d5774 Mon Sep 17 00:00:00 2001 From: Dem Pilafian Date: Tue, 28 Mar 2017 14:26:11 -0700 Subject: [PATCH 1/3] Create built-in webRoot variable Provides a way to create relative URLs in a template. Example usage: Note: Value is safely set (will not override value if already set in "context" option) --- lib/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/index.js b/lib/index.js index b2fb1d1..c198280 100644 --- a/lib/index.js +++ b/lib/index.js @@ -66,6 +66,9 @@ module.exports = function(opts) { var filebase = opts.basepath === '@file' ? path.dirname(file.path) : opts.basepath; var currentFilename = path.resolve(file.base, file.path); + // built-in webRoot variable, example usage: + opts.context.webRoot = opts.context.webRoot || path.relative(currentFilename, file.base); + data = extend(true, {}, opts.context, data || {}); data.content = text; From 6c8de10e07ec210d54625504c18fdd1c4ac70349 Mon Sep 17 00:00:00 2001 From: Dem Pilafian Date: Thu, 30 Mar 2017 22:18:57 -0700 Subject: [PATCH 2/3] Instructions for webRoot feature --- Readme.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) 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 From 2debbc9431509b2be7e238a2411d8c96c8fd4fba Mon Sep 17 00:00:00 2001 From: Dem Pilafian Date: Thu, 30 Mar 2017 22:23:37 -0700 Subject: [PATCH 3/3] Test cases for webRoot --- lib/index.js | 11 +++-- test/fixtures-webroot-variable/html-head.html | 3 ++ test/fixtures-webroot-variable/index.html | 9 ++++ test/fixtures-webroot-variable/result.html | 13 ++++++ .../sub/home-link.html | 1 + test/fixtures-webroot-variable/sub/index.html | 9 ++++ .../fixtures-webroot-variable/sub/result.html | 13 ++++++ test/webroot-variable.js | 46 +++++++++++++++++++ 8 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 test/fixtures-webroot-variable/html-head.html create mode 100644 test/fixtures-webroot-variable/index.html create mode 100644 test/fixtures-webroot-variable/result.html create mode 100644 test/fixtures-webroot-variable/sub/home-link.html create mode 100644 test/fixtures-webroot-variable/sub/index.html create mode 100644 test/fixtures-webroot-variable/sub/result.html create mode 100644 test/webroot-variable.js diff --git a/lib/index.js b/lib/index.js index c198280..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()) { @@ -66,9 +74,6 @@ module.exports = function(opts) { var filebase = opts.basepath === '@file' ? path.dirname(file.path) : opts.basepath; var currentFilename = path.resolve(file.base, file.path); - // built-in webRoot variable, example usage: - opts.context.webRoot = opts.context.webRoot || path.relative(currentFilename, file.base); - data = extend(true, {}, opts.context, data || {}); data.content = text; 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(); + }); + +});