FEATURE: allow plugins to transform, the transformed post

This allows plugins to amend posts prior to rendering.
This commit is contained in:
Sam 2017-03-07 17:12:31 -05:00
parent 8d80a5d97e
commit 0c03ccb01e
2 changed files with 37 additions and 1 deletions

View File

@ -17,9 +17,10 @@ import { addDiscoveryQueryParam } from 'discourse/controllers/discovery-sortable
import { addTagsHtmlCallback } from 'discourse/lib/render-tags';
import { addUserMenuGlyph } from 'discourse/widgets/user-menu';
import { addPostClassesCallback } from 'discourse/widgets/post';
import { addPostTransformCallback } from 'discourse/widgets/post-stream';
// If you add any methods to the API ensure you bump up this number
const PLUGIN_API_VERSION = '0.8.4';
const PLUGIN_API_VERSION = '0.8.5';
class PluginApi {
constructor(version, container) {
@ -438,6 +439,24 @@ class PluginApi {
addPostClassesCallback(callback) {
addPostClassesCallback(callback);
}
/**
*
* Adds a callback to be executed on the "transformed" post that is passed to the post
* widget.
*
* This allows you to apply transformations on the actual post that is about to be rendered.
*
* Example:
*
* addPostTransformCallback((t)=>{
* // post number 7 is overrated, don't show it ever
* if (t.post_number === 7) { t.cooked = ""; }
* })
*/
addPostTransformCallback(callback) {
addPostTransformCallback(callback);
}
}
let _pluginv01;

View File

@ -3,6 +3,21 @@ import transformPost from 'discourse/lib/transform-post';
import { Placeholder } from 'discourse/lib/posts-with-placeholders';
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
let transformCallbacks = null;
function postTransformCallbacks(transformed) {
if (transformCallbacks === null) {
return;
}
for(let i=0; i < transformCallbacks.length; i++) {
transformCallbacks[i].call(this, transformed);
}
}
export function addPostTransformCallback(callback){
transformCallbacks = transformCallbacks || [];
transformCallbacks.push(callback);
};
const CLOAKING_ENABLED = !window.inTestEnv;
const DAY = 1000 * 60 * 60 * 24;
@ -96,6 +111,8 @@ export default createWidget('post-stream', {
transformed.height = _heights[post.id];
transformed.cloaked = _cloaked[post.id];
postTransformCallbacks(transformed);
if (transformed.isSmallAction) {
result.push(this.attach('post-small-action', transformed, { model: post }));
} else {