Skip to content

Instantly share code, notes, and snippets.

@maxmarkus
Created June 18, 2020 09:03
Show Gist options
  • Save maxmarkus/ed02a835729d2048a76eecb0a1fecae3 to your computer and use it in GitHub Desktop.
Save maxmarkus/ed02a835729d2048a76eecb0a1fecae3 to your computer and use it in GitHub Desktop.
Foundation panini helper which extends ifpage with possibility to use wildcards. Extension to panini, the slim flat file generator. https://github.com/foundation/panini
module.exports = function() {
/**
* Handlebars block helper that renders the content inside of it based on the current page.
* @param {string...} pages - One or more pages to check.
* @param (object) options - Handlebars object.
* @example
* {{#ifpage 'index' 'about'}}This must be the index or about page.{{/ifpage}}
* {{#ifpage 'about*' '*ends' '*ind*ex*'}}This must be the index or about page.{{/ifpage}}
*
{{#ifpagewildcard 'over*view'}}<p>over*view</p>{{/ifpagewildcard}}
{{#ifpagewildcard 'ove*iew'}}<p>ove*iew</p>{{/ifpagewildcard}}
{{#ifpagewildcard '*view'}}<p>*view</p>{{/ifpagewildcard}}
{{#ifpagewildcard 'over*'}}<p>over*</p>{{/ifpagewildcard}}
{{#ifpagewildcard '*ervie*'}}<p>*ervie*</p>{{/ifpagewildcard}}
{{#ifpagewildcard '*ervie'}}<p>*ervie NOT SHOWN</p>{{/ifpagewildcard}}
{{#ifpagewildcard 'vervie*'}}<p>vervie* NOT SHOWN</p>{{/ifpagewildcard}}
* @return The content inside the helper if a page matches, or an empty string if not.
*/
const allAsterisks = new RegExp(/\*/, 'g');
var params = Array.prototype.slice.call(arguments);
var pages = params.slice(0, -1);
var options = params[params.length - 1];
var pageName = options.data.root.page;
for (var i in pages) {
const tester = new RegExp('^' + pages[i].replace(allAsterisks, '.*') + '$');
if (tester.test(pageName)) {
return options.fn(this);
}
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment