Skip to content
This repository was archived by the owner on May 17, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "duckduckgo-template-helpers",
"version": "1.0.12",
"version": "1.1.12",
"description": "Template helpers used by DuckDuckGo",
"main": "template-helpers.js",
"license": "Apache2",
Expand Down
106 changes: 58 additions & 48 deletions template_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
return DDG.commifyNumber(number);
});

/**
/**
* @function concat
*
* **Block Helper**
*
*
* Concatenates all the elements in a collection (array of strings)
*
*
* An optional item separator can be appended to
* each item and an optional conjunction can be
* used for the last item.
*
* Note: Technically, the elements can be of any type, but it is assumed that the block returns a string
*
* @param {string} sep **[optional]** Item separator. Default: `''`
*
*
* @param {string} conj **[optional]** Final separator, preceeds last item. Default: `''`
*
* @returns {string}
Expand All @@ -63,7 +63,7 @@
* - `['a']` returns: `a`
* - `['a', 'b']` returns: `a and b`
* - `['a', 'b', 'c']` returns: `a, b and c`
*
*
*/
Handlebars.registerHelper("concat", function(context, options) {
if (!context) { return ""; }
Expand Down Expand Up @@ -93,21 +93,21 @@

/**
* @function condense
*
*
* Shortens a string
*
*
* An optional maximum string length can be provided if preferred.
*
* @param {number} maxlen **[optional]** Maximum allowed string length. Default: `10`
*
*
* @param {number} fuzz The allowable deviation from the maxlen, used to allow a sentence/word to complete if it is less than fuzz characters longer than the maxlen
*
*
* @param {string truncation **[optional]** The truncation string. Default: `'...'`
*
* Example:
*
*
* `{{condense myString maxlen="135" truncation="..."}}`
*
*
* This will output the value of `myString` up to a maximum of 135 characters
* (not including the length of the truncation string) and then append
* the truncation string to the output
Expand Down Expand Up @@ -182,7 +182,7 @@
* @function durationFormat
*
* Takes a time duration in milliseconds
* and converts it to nice, formatted
* and converts it to nice, formatted
* string duration (i.e. 1:23:10)
*
* usage: "{{durationFormat ms}}"
Expand All @@ -197,18 +197,18 @@
*
* Shortens a string by removing words until string length is <= `limit` and
* appends an ellipsis ('...') to the output
*
*
* Note: It automatically appends any closing tag if one is missing.
*
*
* @param {string} text text to shorten
* @param {number} limit maximum length of shortened string
*
*
* @return {string}
*
* Example:
*
* `{{ellipsis title 50}}`
*
*
*/
Handlebars.registerHelper("ellipsis", function(text, limit, options) {
if (!text) { return ""; }
Expand Down Expand Up @@ -271,7 +271,7 @@

/**
* favicon
*
*
* Find favicon and generate the appropriate markup.
*
*/
Expand All @@ -284,7 +284,7 @@
h = ops.h || '16',
url = DDG.get_favicon_url(sourceUrl),
output = '<img width="'+w+'" height="'+h+'" class="'+className;

// prevent creating an image element with an undefined src tag
if (!url) {
// provide a transparent 1x1 data url to avoid making a server call
Expand All @@ -306,7 +306,7 @@
});

/**
* Takes an array of components (or a single component) and
* Takes an array of components (or a single component) and
* renders the HTML to use them as the subtitle.
*
* @param {array/object/string} components
Expand All @@ -330,7 +330,7 @@
*
* e.g. takes Chicago (Illinois) and converts it to:
* <h1 class="title">Chicago <span class="title__sub">Illinois</span></h1>
*
*
* Will also remove the title from an abstract by taking the link text (parseFirst)
*
*/
Expand Down Expand Up @@ -369,9 +369,9 @@
* @returns {string}
*
* Example:
*
*
* `{{imageProxy imageURL}}`
*
*
* produces: `/iu/?u={{imageURL}}`
*
* This works for direct urls, seems not to work for those with a query string
Expand All @@ -388,18 +388,28 @@
return DDG.getImageProxyURL(url);
});

/**
* @function imageProxyThumbnail
*
* Request thumbnail images in default size + 25%
*/

Handlebars.registerHelper("imageProxyThumbnail", function(url) {
return DDG.getImageProxyURL(url, false, false, 204);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard coding 204 in here and wrapping it as a generic function seems odd. If it's a generic helper function, it should probably be a variable. And if 204 is hard coded, maybe it should just be a function inside the image IA.

Another approach could be added a second size argument to the regular imageProxy helper? Maybe that would be more reusable?

});

/**
* @function include
*
* Loads the specified Handlebars template and applies it with
* the current context
*
*
* Note: There is no recursive cycle detection! **Be careful**.
*
* Example:
*
*
* `{{include ../myTemplate}}`
*
*
* Applies the template `myTemplate` using `this` as the data context
*
* `{{include template wrap="wrapper"}}`
Expand Down Expand Up @@ -436,7 +446,7 @@
* a new object containing the "key" and "value" for each
*
* Example:
*
*
* ```
* {{#keys myObject}}
* {{key}} : {{value}}
Expand All @@ -453,7 +463,7 @@
return out;
});

// tokenize incoming text by wrapping in an l() function
// tokenize incoming text by wrapping in an l() function
Handlebars.registerHelper('l', function() {
return l.apply(window,arguments);
});
Expand All @@ -467,14 +477,14 @@
* @function loop
*
* **Block Helper**
*
*
* Counts from zero to the value of `context` (assuming `context` is a **number**)
* applying the content of the block each time.
*
*
* Note: A maximum of 100 loops is allowed.
*
* Example:
*
*
* ```
* {{#loop star_rating}}
* <img src="{{star}}" class="star"></span>
Expand Down Expand Up @@ -538,30 +548,30 @@
return url;
}
});

// helper that requires moment.js - bails if not available
Handlebars.registerHelper("momentDate", function(obj, options) {
if (!moment) { return ''; }
var ops = options && options.hash || {},
date = moment.utc(obj, "YYYY-MM-DD HH:mm:ss"),
format = ops.format || 'ddd MMM D';

return date.local().format(format);
});

// helper that requires moment.js - bails if not available
Handlebars.registerHelper("momentTime", function(obj) {
if (!moment) { return ''; }
var time = moment.utc(obj, "YYYY-MM-DD HH:mm:ss");

return time.local().format('LT');
});

/**
*
* moreAt
*
* Create an attribution line
* Create an attribution line
*
*/
Handlebars.registerHelper("moreAt", function(meta, name, options) {
Expand Down Expand Up @@ -600,7 +610,7 @@
else if (!meta.sourceIconUrl && meta.sourceUrl && !meta.sourceLogo && meta.sourceIcon !== false) {
meta.sourceIcon=true;
}

// bail if we still don't have a valid URL
if (!meta.sourceUrl) { return; }

Expand Down Expand Up @@ -643,7 +653,7 @@
*
* Delimits a number or string with multiple numbers,
* using commas or given delimiter
*
*
* Note: This supports integers and decimal numbers.
*
* Credit: This function was borrowed from
Expand Down Expand Up @@ -698,9 +708,9 @@
* @param {string} delimiter **[optional]** Format the number with the `numFormat` helper
*
* Example:
*
*
* `{plural star_rating singular="star" plural="stars"}}`
*
*
* Will produce:
* - `{{star_rating}} star` if the value of `star_rating` is `1`, or
* - `{{star_rating}} stars` if `star_rating` > `1`
Expand Down Expand Up @@ -829,24 +839,24 @@

/**
* @function stripHTML
*
*
* Strips HTML tags/elements from text
*
*
* @returns {string}
*
*
* Example:
*
*
* `{{stripHTML stringWithHTML}}`
*/

Handlebars.registerHelper("stripHTML", function(text, options) {
return options.fn(DDG.strip_html(text));
});

/*
Helper for table_detail.handlebars template
iterates through a list (context) and use the value from context as
iterates through a list (context) and use the value from context as
a key to reference a value form the data object.
use: {{#table-each list data}}
Expand Down Expand Up @@ -893,11 +903,11 @@
* Removes leading and trailing spaces from text
*
* @return {string}
*
*
* Example:
*
*
* `{{trim stringWithSpaces}}`
*
*
*/
Handlebars.registerHelper("trim", function(obj) {
if (obj) { return obj.trim(); }
Expand All @@ -915,7 +925,7 @@
});

/**
* For adding a loading animation to
* For adding a loading animation to
* the DOM.
*
* @param {string} color [white|black], optional
Expand Down