mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
FEATURE: allow plugins to transform, the transformed post
This allows plugins to amend posts prior to rendering.
This commit is contained in:
@ -17,9 +17,10 @@ import { addDiscoveryQueryParam } from 'discourse/controllers/discovery-sortable
|
|||||||
import { addTagsHtmlCallback } from 'discourse/lib/render-tags';
|
import { addTagsHtmlCallback } from 'discourse/lib/render-tags';
|
||||||
import { addUserMenuGlyph } from 'discourse/widgets/user-menu';
|
import { addUserMenuGlyph } from 'discourse/widgets/user-menu';
|
||||||
import { addPostClassesCallback } from 'discourse/widgets/post';
|
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
|
// 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 {
|
class PluginApi {
|
||||||
constructor(version, container) {
|
constructor(version, container) {
|
||||||
@ -438,6 +439,24 @@ class PluginApi {
|
|||||||
addPostClassesCallback(callback) {
|
addPostClassesCallback(callback) {
|
||||||
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;
|
let _pluginv01;
|
||||||
|
@ -3,6 +3,21 @@ import transformPost from 'discourse/lib/transform-post';
|
|||||||
import { Placeholder } from 'discourse/lib/posts-with-placeholders';
|
import { Placeholder } from 'discourse/lib/posts-with-placeholders';
|
||||||
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
|
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 CLOAKING_ENABLED = !window.inTestEnv;
|
||||||
const DAY = 1000 * 60 * 60 * 24;
|
const DAY = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
@ -96,6 +111,8 @@ export default createWidget('post-stream', {
|
|||||||
transformed.height = _heights[post.id];
|
transformed.height = _heights[post.id];
|
||||||
transformed.cloaked = _cloaked[post.id];
|
transformed.cloaked = _cloaked[post.id];
|
||||||
|
|
||||||
|
postTransformCallbacks(transformed);
|
||||||
|
|
||||||
if (transformed.isSmallAction) {
|
if (transformed.isSmallAction) {
|
||||||
result.push(this.attach('post-small-action', transformed, { model: post }));
|
result.push(this.attach('post-small-action', transformed, { model: post }));
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user