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

Nic*_*vre 3 ruby-on-rails trix stimulusjs actiontext

如何使我在 Trix (ActionText) 编辑器中添加的链接在新窗口中使用 target="_blank" 打开链接?

class Post < ApplicationRecord
  has_rich_text :rich_text_content
end
Run Code Online (Sandbox Code Playgroud)

Nic*_*vre 5

添加刺激控制器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"
      }
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

像这样添加您的内容:

<div data-controller="richtext">
  <%= post.rich_text_content %>
</div>
Run Code Online (Sandbox Code Playgroud)