Tag description is removing target="_blank"

On some of my tags’ descriptions I want to add a link like this:

<a href="https://store.sgiant.me" target="_blank">Click here</a> to visit the official Sir Giant store.

When I first save it, it works and it opens in a new window.
When I refresh the page, it doesn’t work anymore and when I check the code, it becomes:

<a href="https://store.sgiant.me">Click here</a> to visit the official Sir Giant store.

I already have that option to open external links in a new tab set to Yes, but this seems to not affect this particular case where I’m adding html code.

ChatGPT suggested the creation of a component with the JS tab containing:

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  function processTagLinks() {
    document.querySelectorAll('.tag-title-contents a[href^="http"]').forEach(link => {
      if (!link.href.includes(window.location.hostname) && !link.hasAttribute('data-processed')) {
        link.setAttribute('target', '_blank');
        link.setAttribute('rel', 'noopener noreferrer');
        link.setAttribute('data-processed', 'true');
      }
    });
  }

  // Process immediately
  processTagLinks();

  // Watch for DOM changes
  const observer = new MutationObserver(() => {
    processTagLinks();
  });

  observer.observe(document.body, {
    childList: true,
    subtree: true
  });

  // Also run on page changes
  api.onPageChange(() => {
    setTimeout(processTagLinks, 100);
  });
});

But I’m no developer and so I don’t know if that’s a good solution or not?

Maybe you just want to adjust Default other external links in new tab

But that’s what I mean by I already have that option to open external links in a new tab set to Yes

image

This doesn’t affect the raw HTML I’m using in the description. That’s why I had to find an alternative.

1 Like