Tag description is removing target="_blank"

AI-generated summary

alltiagocom reports that when adding HTML links with target="_blank" in tag descriptions, the attribute is stripped upon page refresh, despite having the site setting “Default other external links in new tab” enabled. This setting doesn’t apply to manually coded HTML links in tag descriptions. pfaffman initially suggests checking that setting, but alltiagocom clarifies it doesn’t resolve the issue. As a workaround, ChatGPT suggested a JavaScript component to dynamically add target="_blank" and rel="noopener noreferrer" to external links in tag descriptions, but alltiagocom is unsure if this is a safe or appropriate solution since they’re not a developer. The core issue is that Discourse’s sanitization removes target="_blank" from user-provided HTML in tag descriptions, even when the global setting for external links is active. A custom JS solution may be necessary to restore the intended behavior, though it requires technical implementation and maintenance.

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.