mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 12:42:57 +08:00
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:
parent
e53bf294ef
commit
6352528988
|
@ -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);
|
||||
});
|
||||
|
|
24
app/assets/javascripts/discourse/lib/filter.js.es6
Normal file
24
app/assets/javascripts/discourse/lib/filter.js.es6
Normal 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;
|
Loading…
Reference in New Issue
Block a user