如何在新 window 中打开 Action Text Trix 附件或链接(目标 _blank)

How to open Action Text Trix attachments or links in new window (target _blank)

如何使我在 Trix (ActionText) 编辑器中添加的 link 在新的 window 中打开 link 并设置为 target="_blank"?

class Post < ApplicationRecord
  has_rich_text :rich_text_content
end

添加 Stimulus 控制器 richtext_controller.js :

import { Controller } from "stimulus"

export default class extends Controller {
  connect() {
    this.element.querySelectorAll('a').forEach(function(link) {
      if (link.host !== window.location.host) {
        link.target = "_blank"
      }
    })
  }
}

像这样添加您的内容:

<div data-controller="richtext">
  <%= post.rich_text_content %>
</div>