我试图在Rails 6上以WYSIWYG Trix和渲染内容显示带有ActionText的嵌入式视频。但是ActionText渲染器过滤了所有原始的html代码,并迫使我使用JS在渲染的内容中显示iframe,这在Trix中不起作用。
我按照Basecamp的一名开发人员https://github.com/rails/actiontext/issues/37#issuecomment-451627370给出的指示进行操作。第1步到第3步有效,但是当ActionText渲染我的局部代码时,它会过滤iframe。
创建WYSIYWG的表单
= form_for(article, url: url, method: method) do |a|
= a.label :content
= a.rich_text_area :content, data: { controller: "articles", target: "articles.field", embeds_path: editorial_publication_embeds_path(@publication, format: :json) }
= a.submit submit_text, class:"btn full"
Stimulus控制器添加了嵌入功能(迫切需要重构)
import { Controller } from "stimulus";
import Trix from "trix";
$.ajaxSetup({
headers: {
"X-CSRF-Token": $('meta[name="csrf-token"]').attr("content"),
},
});
export default class extends Controller {
static targets = ["field"];
connect() {
this.editor = this.fieldTarget.editor;
const buttonHTML =
'<button type="button" class="trix-button" data-trix-attribute="embed" data-trix-action="embed" title="Embed" tabindex="-1">Media</button>';
const buttonGroup = this.fieldTarget.toolbarElement.querySelector(
".trix-button-group--block-tools"
);
const dialogHml = `<div class="trix-dialog trix-dialog--link" data-trix-dialog="embed" data-trix-dialog-attribute="embed">
<div class="trix-dialog__link-fields">
<input type="text" name="embed" class="trix-input trix-input--dialog" placeholder="Paste your video or sound url" aria-label="embed code" required="" data-trix-input="" disabled="disabled">
<div class="trix-button-group">
<input type="button" class="trix-button trix-button--dialog" data-trix-custom="add-embed" value="Add">
</div>
</div>
</div>`;
const dialogGroup = this.fieldTarget.toolbarElement.querySelector(
".trix-dialogs"
);
buttonGroup.insertAdjacentHTML("beforeend", buttonHTML);
dialogGroup.insertAdjacentHTML("beforeend", dialogHml);
document
.querySelector('[data-trix-action="embed"]')
.addEventListener("click", event => {
const dialog = document.querySelector('[data-trix-dialog="embed"]');
const embedInput = document.querySelector('[name="embed"]');
if (event.target.classList.contains("trix-active")) {
event.target.classList.remove("trix-active");
dialog.classList.remove("trix-active");
delete dialog.dataset.trixActive;
embedInput.setAttribute("disabled", "disabled");
} else {
event.target.classList.add("trix-active");
dialog.classList.add("trix-active");
dialog.dataset.trixActive = "";
embedInput.removeAttribute("disabled");
embedInput.focus();
}
});
document
.querySelector('[data-trix-custom="add-embed"]')
.addEventListener("click", event => {
const content = document.querySelector('[name="embed"]').value;
if (content) {
$.ajax({
method: "POST",
url: document.querySelector("[data-embeds-path]").dataset
.embedsPath,
data: {
embed: {
content,
},
},
success: ({ content, sgid }) => {
const attachment = new Trix.Attachment({
content,
sgid,
});
this.editor.insertAttachment(attachment);
this.editor.insertLineBreak();
},
});
}
});
}
}
嵌入模型
class Embed < ApplicationRecord
include ActionText::Attachable
validates :content, presence: true
after_validation :fetch_oembed_data
def to_partial_path
"editorial/embeds/embed"
end
def fetch_oembed_data
url =
case content
when /youtube/
"https://www.youtube.com/oembed?url=#{content}&format=json"
when /soundcloud/
"https://soundcloud.com/oembed?url=#{content}&format=json"
when /twitter/
"https://publish.twitter.com/oembed?url=#{content}"
end
res = RestClient.get url
json = JSON.parse(res.body, object_class: OpenStruct)
self.height = json.height
self.author_url = json.author_url
self.thumbnail_url = json.thumbnail_url
self.width = json.width
self.author_name = json.author_name
self.thumbnail_height = json.thumbnail_height
self.title = json.title
self.version = json.version
self.provider_url = json.provider_url
self.thumbnail_width = json.thumbnail_width
self.embed_type = json.type
self.provider_name = json.provider_name
self.html = json.html
end
end
创建嵌入的控制器
def create
@embed = Embed.create!(params.require(:embed).permit(:content))
respond_to do |format|
format.json
end
end
jbuilder视图响应ajax调用以创建Embed
json.extract! @embed, :content
json.sgid @embed.attachable_sgid
json.content render(partial: "editorial/embeds/embed", locals: { embed: @embed }, formats: [:html])
HTML嵌入部分(精简版)
.youtube-embed.embed
.content
= image_tag(embed.thumbnail_url) if embed.thumbnail_url.present?
p = "Embed from #{embed.provider_name} (#{embed.content})"
p.embed-html = embed.html
最后,当显示带有嵌入内容的文章内容时,显示iframe的JS代码
$(document).ready(() => {
$(".embed").each(function(i, embed) {
const $embed = $(embed);
const p = $embed
.find(".content")
.replaceWith($embed.find(".embed-html").text());
});
});
如果我将Embed部分更改为
== embed.html
它在所见即所得中正确显示,但在渲染的视图中不能正确显示。
答案 0 :(得分:1)
需要在allowed_tags中添加iframe,在application.rb
中添加如下代码:
config.to_prepare do
ActionText::ContentHelper.allowed_tags << "iframe"
end
答案 1 :(得分:0)
您似乎需要将生成iframe的脚本列入白名单。
您可以在显示页面上进行快速测试,为内容提供商添加相关的JS(我正在测试Instagram附件,因此添加了 web: gunicorn deploy: app
)。
在ActionText视图中将所有<script async src="//www.instagram.com/embed.js"></script>
标签列入白名单是不明智的,但是您可以管理自己加载的脚本。
答案 2 :(得分:0)
我无法测试您的复杂示例,但是如果您想放入ActionText的HTML,将会很有帮助。
请尝试以下操作:
<%= raw your_action_text_object.to_plain_text %>