Add target=blank to links in formatted text via js
Some checks failed
/ Run tests (push) Failing after 15s
/ Run system tests (push) Failing after 14s
/ Build, push and deploy image (push) Has been skipped

This commit is contained in:
david 2024-10-26 20:57:41 +02:00
parent d90f1b86b8
commit 812c192910
5 changed files with 18 additions and 28 deletions

View file

@ -16,9 +16,11 @@ application.register("collapse-chevron-toggler", CollapseChevronTogglerControlle
import HelloController from "./hello_controller"
application.register("hello", HelloController)
import RichTextLinkTargetsController from "./rich_text_link_targets_controller"
application.register("rich-text-link-targets", RichTextLinkTargetsController)
import SetThemeController from "./set_theme_controller"
application.register("set-theme", SetThemeController)
import ThemeSwitcherController from "./theme_switcher_controller"
application.register("theme-switcher", ThemeSwitcherController)

View file

@ -0,0 +1,12 @@
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="rich-text-link-targets"
export default class extends Controller {
connect() {
this.element.querySelectorAll('div.trix-content a').forEach(function(link) {
if (link.host !== window.location.host) {
link.target = "_blank"
}
})
}
}