How to decorate a post before it's cooked?

I’m writing a theme component and would like to decorate/change the posts just before they’re cooked. How can I get ahold of the uncooked post/text (for the post that’s about to be cooked/shown)?

I read the “Developer’s guide to Discourse Themes” and found that I could modify the cooked post by using the pluginAPI methods decorateCooked/decorateCookedElement and addPostTransformCallback, but didn’t find how to get it before it was cooked.

I also looked at the controllers and components, but without any luck.

2 Likes

you need to use this function:

https://github.com/discourse/discourse/blob/2123561125242a3c048958a56bd8bebe83cd577f/app/assets/javascripts/discourse/app/lib/plugin-api.js#L972
which will overwrite this:
https://github.com/discourse/discourse/blob/2123561125242a3c048958a56bd8bebe83cd577f/app/assets/javascripts/discourse/app/models/composer.js#L850
so you can change the post before its saved.
peace out. :smiley:

1 Like

Thanks for your reply! But I don’t want to save the text modifications, I want to change it after it’s saved but before it’s shown (and before it’s cooked).

The reason is that I want the original text to pop-up if the user edits the post. I suspect that if I would use composerBeforeSave, the changed text would appear when editing (?).

1 Like

if you want to do this as a theme this will be your only choice. userinput (markdown)-> server side cooking in ruby → (html) retrieval by other users and possible decoration. This is the promise that will be called before hitting the save/reply button. This will not show up in drafts, but you are right it would show up if you were to edit the post. if you want to get around this you need to undo the changes that you do on save when the composer opens/switches to the post you modified before. I could go into further detail or you could just read the code and do your own experiments.
cheers! :smiley:
EDIT: I just thought about this. theoretically you could also recook on the client side. But I think thats a bad idea for performance and other reasons. Or just do a plugin: Plugin Tutorial #1 - How to manipulate the text in the composer?

Will read up on plugins! But probably I will just modify the cooked post instead (exactly what I said I didn’t want to :smile:).

Thanks again!