# Trying to replace / auto-format strings in user posts

**URL:** https://meta.discourse.org/t/trying-to-replace-auto-format-strings-in-user-posts/285326
**Category:** Development
**Created:** [November 13, 2023, 3:32pm UTC](https://meta.discourse.org/t/trying-to-replace-auto-format-strings-in-user-posts/285326 "2023-11-13T15:32:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hemes](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hemes/32/340413_2.png) [@hemes](https://meta.discourse.org/u/hemes)
#### Post date: [November 13, 2023, 3:32pm UTC](https://meta.discourse.org/t/trying-to-replace-auto-format-strings-in-user-posts/285326/1 "2023-11-13T15:32:21Z")

</div>

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.

Thanks,  
Brett

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [November 13, 2023, 4:48pm UTC](https://meta.discourse.org/t/trying-to-replace-auto-format-strings-in-user-posts/285326/2 "2023-11-13T16:48:36Z")

</div>

Sure, it should be possible. You have an API to decorate the cooked post.  
For example: [decorateCookedElement](https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/plugin-api.js#L379)

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

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [November 13, 2023, 4:50pm UTC](https://meta.discourse.org/t/trying-to-replace-auto-format-strings-in-user-posts/285326/3 "2023-11-13T16:50:55Z")

</div>

Maybe you can create your own style

> [@Customize posts' contents with your own styles](https://meta.discourse.org/t/customize-posts-contents-with-your-own-styles/257738):
>
> Requirementsinformation_source To be able to use these tips and tricks, you need to be an administrator of either a self-hosted Discourse instance or a [Discourse-hosted plan](https://discourse.org/pricing) higher than Basic. Introduction Discourse supports several methods to format and customize a post’s contents. You can find the list here: But sometimes, you’ll want something more specific, for example, a link that looks like a button. [Green button] This is the kind of modification we’ll learn here. The logic I’l…

---

<div class="post-metadata">

### Author: ![hemes](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hemes/32/340413_2.png) [@hemes](https://meta.discourse.org/u/hemes)
#### Post date: [November 13, 2023, 5:18pm UTC](https://meta.discourse.org/t/trying-to-replace-auto-format-strings-in-user-posts/285326/4 "2023-11-13T17:18:51Z")

</div>

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](https://meta.discourse.org/t/how-do-we-fire-scripts-after-topic-html-is-rendered-in-dom/114701)

In particular, I found this very helpful:

```plaintext
$.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!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [December 13, 2023, 5:19pm UTC](https://meta.discourse.org/t/trying-to-replace-auto-format-strings-in-user-posts/285326/5 "2023-12-13T17:19:08Z")

</div>

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