Skip to content

Commit

Permalink
Merge pull request #823 from willrowe/hotfix/function/source/namespac…
Browse files Browse the repository at this point in the history
…e-support

Add namespace support to `source` function
  • Loading branch information
willrowe authored Sep 2, 2022
2 parents 38f9aac + 3d6fed7 commit 03c47bc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "docs/wiki"]
path = docs/wiki
url = git://github.com/justjohn/twig.js.wiki.git
url = https://github.com/twigjs/twig.js.wiki.git
8 changes: 7 additions & 1 deletion src/twig.functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,17 @@ module.exports = function (Twig) {
* @returns {string}
*/
source(name, ignoreMissing) {
const state = this;
const {namespaces} = state.template.options;
let templateSource;
let templateFound = false;
const isNodeEnvironment = typeof module !== 'undefined' && typeof module.exports !== 'undefined' && typeof window === 'undefined';
let loader;
const path = name;
let path = name;

if (namespaces && typeof namespaces === 'object') {
path = Twig.path.expandNamespace(namespaces, path);
}

// If we are running in a node.js environment, set the loader to 'fs'.
if (isNodeEnvironment) {
Expand Down
44 changes: 20 additions & 24 deletions src/twig.path.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ module.exports = function (Twig) {
*/
Twig.path = {};

/**
* @param {Twig.Template} template
* @param {string} path
*/
Twig.path.expandNamespace = function (namespaces, path) {
const namespaceIdentifiers = Object.keys(namespaces);
const pattern = new RegExp(`^(?:@(${namespaceIdentifiers.join('|')})/|(${namespaceIdentifiers.join('|')})::)`);

return path.replace(pattern, (wholeMatch, atNamespace, colonNamespace) => {
const namespaceIdentifier = (atNamespace === undefined ? colonNamespace : atNamespace);

return `${namespaces[namespaceIdentifier]}/`;
});
};

/**
* Generate the canonical version of a url based on the given base path and file path and in
* the previously registered namespaces.
Expand All @@ -19,36 +34,17 @@ module.exports = function (Twig) {
* @return {string} The canonical version of the path
*/
Twig.path.parsePath = function (template, _file) {
let k = null;
const {namespaces} = template.options;
let file = _file || '';
const file = _file || '';
const hasNamespaces = namespaces && typeof namespaces === 'object';

if (hasNamespaces) {
for (k in namespaces) {
if (!file.includes(k)) {
continue;
}

// Check if keyed namespace exists at path's start
const colon = new RegExp('^' + k + '::');
const atSign = new RegExp('^@' + k + '/');
// Add slash to the end of path
const namespacePath = namespaces[k].replace(/([^/])$/, '$1/');
let path = (hasNamespaces ? Twig.path.expandNamespace(namespaces, file) : file);

if (colon.test(file)) {
file = file.replace(colon, namespacePath);
return file;
}

if (atSign.test(file)) {
file = file.replace(atSign, namespacePath);
return file;
}
}
if (path === file) {
path = Twig.path.relativePath(template, file);
}

return Twig.path.relativePath(template, file);
return path;
};

/**
Expand Down
1 change: 1 addition & 0 deletions test/templates/functions/source/namespaces.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ test }}
9 changes: 9 additions & 0 deletions test/test.functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ describe('Twig.js Functions ->', function () {
it('should allow loading relative paths', function () {
twig({data: '{{ source("test/templates/simple.twig") }}'}).render().should.equal('Twig.js!');
});

it('should allow loading paths with namespaces', function () {
twig({
'data': "{{ source('test::namespaces.twig') }}",
'namespaces': {
'test': 'test/templates/functions/source',
},
}).render().trim().should.equal('{{ test }}');
});
});
});
});

0 comments on commit 03c47bc

Please sign in to comment.