Trying to replace / auto-format strings in user posts

Hello, I am new to Discourse and have my site up and am trying to get a critical piece of functionality I need for a major portion of the community figured out.

Users have software that can export magic strings that represent some game state and I want to render that state nicely as a table that gets generated from the string (at page load time).

Ideally, I give users a template when posting or they put their magic string inside a named class or something and the string just gets rendered (decorated?) at page load time.

Can I do this? I am currently thinking I should write a script that parses the strings for all elements/classes of that type but am struggling on how/when to get the script to modify the displayed (cooked?) post.


1 Like

Sure, it should be possible. You have an API to decorate the cooked post.
For example: decorateCookedElement

Can you share a visual example of exactly what you’re trying to achieve?

1 Like

Maybe you can create your own style

1 Like

All right! It seems that decorateCookedElement is what I needed (per @Arkshine note).

I also found this post that has some nice examples to filter on elements of interest:
How do we fire scripts after topic HTML is rendered in DOM? - dev - Discourse Meta

In particular, I found this very helpful:

$.fn.doSomething = function() {
  const targetElement = $(this).children("[data-theme-test]").length;
  if (!targetElement) return;

  // do your work
  return this;

api.decorateCooked($elem => $elem.doSomething(), { onlyStream: true });

Thank you!


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.