EXTENSIBILITY: add filter api to inject hooks in raw templates

TODO: decide with @eviltrout,
decide if registerUnboundOverride makes more sense
This commit is contained in:
Sam 2015-08-04 18:53:28 +10:00
parent e53bf294ef
commit 6352528988
2 changed files with 30 additions and 1 deletions

View File

@ -1,9 +1,14 @@
import registerUnbound from 'discourse/helpers/register-unbound';
import { runFilters } from 'discourse/lib/filter';
registerUnbound('topic-link', function(topic) {
var title = topic.get('fancyTitle');
var url = topic.linked_post_number ? topic.urlForPostNumber(topic.linked_post_number) : topic.get('lastUnreadUrl');
var extraClass = topic.get('last_read_post_number') === topic.get('highest_post_number') ? " visited" : "";
return new Handlebars.SafeString("<a href='" + url + "' class='title" + extraClass + "'>" + title + "</a>");
var string = "<a href='" + url + "' class='title" + extraClass + "'>" + title + "</a>";
string = runFilters('topic-link', string, topic);
return new Handlebars.SafeString(string);
});

View File

@ -0,0 +1,24 @@
var filters = {};
// use filter API to register a callback from a plugin
const filter = function(name, fn) {
var current = filters[name] = filters[name] || [];
current.push(fn);
};
const runFilters = function(name, val) {
const current = filters[name];
if (current) {
var args = Array.prototype.slice.call(arguments, 1);
for(var i = 0; i < current.length; i++) {
val = current[i].apply(this, args);
}
}
return val;
};
export { runFilters };
export default filter;