# Interaction among theme component, theme javascript, and "previews"

**URL:** https://meta.discourse.org/t/interaction-among-theme-component-theme-javascript-and-previews/190303
**Category:** Development
**Created:** [May 13, 2021, 12:36pm UTC](https://meta.discourse.org/t/interaction-among-theme-component-theme-javascript-and-previews/190303 "2021-05-13T12:36:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![craigconstantine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/craigconstantine/32/511195_2.png) [@craigconstantine](https://meta.discourse.org/u/craigconstantine)
#### Post date: [May 13, 2021, 12:36pm UTC](https://meta.discourse.org/t/interaction-among-theme-component-theme-javascript-and-previews/190303/1 "2021-05-13T12:36:50Z")

</div>

# j/s

I have some custom j/s in my theme. (It’s my solution to, [Interactive SVG using \<object\>? - #9 by craigconstantine](https://meta.discourse.org/t/interactive-svg-using-object/173747/9) ) Eventually, I’ll move the j/s to a Git repo, and install it as a proper Theme component. For now, I simply have the j/s pasted into my Theme’s header. I suppose, this problem might go away if I installed this j/s as a proper component, but it doesn’t smell that way to me.

My j/s modifies the DOM before it’s sent to the browser…

```
api.decorateCooked(
    $elem => $elem.children('.cooked div[data-custom="umbdv"]').umbdv(),
    { id: 'umbdv' }
);

```

# In action…

It selects appropriate `<div data-custom="…`, and then adds new elements. For example, in the following screenshot, everything below the arrows, is inserted by the j/s. (You can see it in action here, [https://forum.moversmindset.com/t/list-of-movers-mindset-podcast-episodes/1160](https://forum.moversmindset.com/t/list-of-movers-mindset-podcast-episodes/1160) )

 ![Screen Shot 2021-05-13 at 08.03.55](https://global.discourse-cdn.com/meta/original/3X/c/1/c1c5df54f11de093e9da7a632a337c46a92ada5a.png)

# What I’m seeing…

I’m using the Topic List Previews component ( [GitHub - merefield/discourse-tc-topic-list-previews: Enriches the content and layout of topic lists · GitHub](https://github.com/merefield/discourse-topic-list-previews-theme.git) ) and my j/s doesn’t seem to get called.

In this screenshot, the `/vmm/gibberish…` string is the bare `<div data-custom="…` contents showing through because my j/s never gets to it via `api.decorateCooked(…`.

 ![Screen Shot 2021-05-13 at 08.02.55](https://global.discourse-cdn.com/meta/original/3X/0/7/0778f57601966451195b1ecde67443cf9cab75d1.png)

The same thing happens in emails that get sent out…

 ![e58e96d4c014c7676c4e2844db707b7831c61e09](https://global.discourse-cdn.com/meta/original/3X/0/f/0f74e8458d2f9d8ee0602cfda197657ee2aa5050.png)

# What I’d like…

I don’t actually want it do the full replacement. Long story. I’d just like to expand my j/s so it can put some static string there, rather than the bare gibberish from my `<div data-custom="…` block.

Is there some other `api.` method that I can hook into?

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [May 13, 2021, 1:35pm UTC](https://meta.discourse.org/t/interaction-among-theme-component-theme-javascript-and-previews/190303/2 "2021-05-13T13:35:27Z")

</div>

Unfortunately there isn’t a javascript API for modifying the ‘excerpts’ of topics in the topic list. Theme components also do not have any access to server-side rendered content (i.e. emails) for security reasons. If you wanted to modify emails, you would have to use a plugin.

However, I wonder if we can solve this in a different way. Right now you are pasting something like this in the composer:

```html
<div data-custom="umbdv">/vmm/longstringhere</div>

```

Instead, you could do something like this:

```html
<div data-custom="umbdv" data-theme-longstring="/vmm/longstringhere">
  fallback content for emails/excerpts
</div>

```

Or if you’re feeling super fancy, you could use [the generic bbcode wrapper](https://meta.discourse.org/t/generic-bbcode-wrapper-for-theme-components/123516) to do it

```plaintext
[wrap=umbdv longstring="/vmm/longstringhere"]
Fallback content
[/wrap]

```

That will automagically generate a div like

```plaintext
<div class="d-wrap" data-wrap="umbdv" data-longstring="/vmm/longstringhere" dir="ltr">
<p dir="ltr">Fallback content</p>
</div>

```

Then you will need to tweak your theme component code so that it deletes the fallback content, and replaces it with whatever you want to show.

---

<div class="post-metadata">

### Author: ![craigconstantine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/craigconstantine/32/511195_2.png) [@craigconstantine](https://meta.discourse.org/u/craigconstantine)
#### Post date: [May 13, 2021, 1:53pm UTC](https://meta.discourse.org/t/interaction-among-theme-component-theme-javascript-and-previews/190303/3 "2021-05-13T13:53:26Z")

</div>

…oooooooooooh, that would do it. I’ll tinker and report back. 🙂

---

<div class="post-metadata">

### Author: ![craigconstantine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/craigconstantine/32/511195_2.png) [@craigconstantine](https://meta.discourse.org/u/craigconstantine)
#### Post date: [May 14, 2021, 12:02pm UTC](https://meta.discourse.org/t/interaction-among-theme-component-theme-javascript-and-previews/190303/4 "2021-05-14T12:02:58Z")

</div>

…yeah, works purrfectly @david, thanks!

 ![Screen Shot 2021-05-14 at 08.01.26](https://global.discourse-cdn.com/meta/original/3X/4/5/45589ffcfeef086fbac4a817a02fe173b34a8929.png)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 14, 2021, 4:37pm UTC](https://meta.discourse.org/t/interaction-among-theme-component-theme-javascript-and-previews/190303/5 "2021-05-14T16:37:19Z")

</div>

Sometimes the ❤ just isn’t enough. This is a great answer, and I’m really close to understanding javascript and CSS well enough to `d-wrap` my head around it. 🍻
