diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml new file mode 100644 index 00000000..f46281ca --- /dev/null +++ b/.github/workflows/create-release.yml @@ -0,0 +1,34 @@ +name: Create release + +on: + push: + tags: + - 'v*' + +jobs: + create-release: + name: Create GitHub Release + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Get semver number + id: get_semver + env: + TAG_NAME: ${{ github.ref }} + run: echo "::set-output name=num::${TAG_NAME:11}" + + - name: Create release on GitHub API + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: "v${{ steps.get_semver.outputs.num }}" + body: | + Version ${{ steps.get_semver.outputs.num }} + draft: false + prerelease: false diff --git a/README.md b/README.md index 933d106f..265ebfed 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,11 @@ Therefore, we do not recommend using this shortcut. output inside scriptlet tags. - `async` When `true`, EJS will use an async function for rendering. (Depends on async/await support in the JS runtime. + - `includer` Custom function to handle EJS includes, receives `(originalPath, parsedPath)` + parameters, where `originalPath` is the path in include as-is and `parsedPath` is the + previously resolved path. Should return an object `{ filename, template }`, + you may return only one of the properties, where `filename` is the final parsed path and `template` + is the included content. This project uses [JSDoc](http://usejsdoc.org/). For the full public API documentation, clone the repository and run `npm run doc`. This will run JSDoc diff --git a/jakefile.js b/jakefile.js index 9b7c70ee..0eebf2e0 100644 --- a/jakefile.js +++ b/jakefile.js @@ -64,7 +64,8 @@ publishTask('ejs', ['build'], function () { 'ejs.js', 'ejs.min.js', 'lib/**', - 'bin/**' + 'bin/**', + 'usage.txt' ]); }); diff --git a/lib/ejs.js b/lib/ejs.js index bb4e1bfb..104aadad 100755 --- a/lib/ejs.js +++ b/lib/ejs.js @@ -179,7 +179,7 @@ function getIncludePath(path, options) { if (!includePath && Array.isArray(views)) { includePath = resolvePaths(path, views); } - if (!includePath) { + if (!includePath && typeof options.includer !== 'function') { throw new Error('Could not find the include file "' + options.escapeFunction(path) + '"'); } @@ -307,6 +307,17 @@ function fileLoader(filePath){ function includeFile(path, options) { var opts = utils.shallowCopy({}, options); opts.filename = getIncludePath(path, opts); + if (typeof options.includer === 'function') { + var includerResult = options.includer(path, opts.filename); + if (includerResult) { + if (includerResult.filename) { + opts.filename = includerResult.filename; + } + if (includerResult.template) { + return handleCache(opts, includerResult.template); + } + } + } return handleCache(opts); } @@ -515,6 +526,7 @@ function Template(text, opts) { options.cache = opts.cache || false; options.rmWhitespace = opts.rmWhitespace; options.root = opts.root; + options.includer = opts.includer; options.outputFunctionName = opts.outputFunctionName; options.localsName = opts.localsName || exports.localsName || _DEFAULT_LOCALS_NAME; options.views = opts.views; diff --git a/package-lock.json b/package-lock.json index 94e7cc2f..af51801f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ejs", - "version": "3.1.0", + "version": "3.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 1f7fc996..5d140238 100644 --- a/package.json +++ b/package.json @@ -6,13 +6,15 @@ "engine", "ejs" ], - "version": "3.1.2", + "version": "3.1.3", "author": "Matthew Eernisse (http://fleegix.org)", "license": "Apache-2.0", "bin": { "ejs": "./bin/cli.js" }, "main": "./lib/ejs.js", + "jsdelivr": "ejs.min.js", + "unpkg": "ejs.min.js", "repository": { "type": "git", "url": "git://github.com/mde/ejs.git" diff --git a/test/ejs.js b/test/ejs.js index 5a7e7d4e..5360499d 100755 --- a/test/ejs.js +++ b/test/ejs.js @@ -999,6 +999,36 @@ suite('include()', function () { fixture('include.html')); }); + test('include ejs with custom includer function', function () { + var file = 'test/fixtures/include-root.ejs'; + var inc = function (original, prev) { + if (original.charAt(0) === '/') { + return { + filename: path.join(__dirname, 'fixtures', prev) + }; + } else { + return prev; + } + }; + assert.equal(ejs.render(fixture('include-root.ejs'), {pets: users}, {filename: file, delimiter: '@', includer: inc}), + fixture('include.html')); + }); + + test('include ejs with includer returning template', function () { + var file = 'test/fixtures/include-root.ejs'; + var inc = function (original, prev) { + if (prev === '/include.ejs') { + return { + template: '

Hello template!

\n' + }; + } else { + return prev; + } + }; + assert.equal(ejs.render(fixture('include-root.ejs'), {pets: users}, {filename: file, delimiter: '@', includer: inc}), + fixture('hello-template.html')); + }); + test('work when nested', function () { var file = 'test/fixtures/menu.ejs'; assert.equal(ejs.render(fixture('menu.ejs'), {pets: users}, {filename: file}), diff --git a/test/fixtures/hello-template.html b/test/fixtures/hello-template.html new file mode 100644 index 00000000..c0de8a52 --- /dev/null +++ b/test/fixtures/hello-template.html @@ -0,0 +1 @@ +

Hello template!