2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-09-18 17:25:04 +00:00
|
|
|
module KnowledgeBaseRichTextHelper
|
2020-02-19 17:07:52 +00:00
|
|
|
def prepare_rich_text(input)
|
|
|
|
prepare_rich_text_videos(prepare_rich_text_links(input))
|
|
|
|
end
|
|
|
|
|
2019-09-18 17:25:04 +00:00
|
|
|
def prepare_rich_text_links(input)
|
|
|
|
scrubber = Loofah::Scrubber.new do |node|
|
|
|
|
next if node.name != 'a'
|
|
|
|
next if !node.key? 'data-target-type'
|
|
|
|
|
|
|
|
case node['data-target-type']
|
|
|
|
when 'knowledge-base-answer'
|
|
|
|
if (translation = KnowledgeBase::Answer::Translation.find_by(id: node['data-target-id']))
|
|
|
|
path = help_answer_path(translation.answer.category.translation_preferred(translation.kb_locale),
|
|
|
|
translation,
|
|
|
|
locale: translation.kb_locale.system_locale.locale)
|
|
|
|
|
|
|
|
node['href'] = custom_path_if_needed path, translation.kb_locale.knowledge_base
|
|
|
|
else
|
|
|
|
node['href'] = '#'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-07 06:30:20 +00:00
|
|
|
Loofah.scrub_fragment(input, scrubber).to_s.html_safe # rubocop:disable Rails/OutputSafety
|
2019-09-18 17:25:04 +00:00
|
|
|
|
|
|
|
end
|
2020-02-19 17:07:52 +00:00
|
|
|
|
|
|
|
def prepare_rich_text_videos(input)
|
2021-05-12 11:37:44 +00:00
|
|
|
input.gsub(%r{\((\s*)widget:(\s*)video\W([\s\S])+?\)}) do |match|
|
2020-02-19 17:07:52 +00:00
|
|
|
settings = match
|
|
|
|
.slice(1...-1)
|
|
|
|
.split(',')
|
2022-01-03 07:21:16 +00:00
|
|
|
.to_h { |pair| pair.split(':').map(&:strip) }
|
2020-02-19 17:07:52 +00:00
|
|
|
.symbolize_keys
|
|
|
|
|
|
|
|
url = case settings[:provider]
|
|
|
|
when 'youtube'
|
2020-03-06 08:27:42 +00:00
|
|
|
"https://www.youtube.com/embed/#{settings[:id]}"
|
2020-02-19 17:07:52 +00:00
|
|
|
when 'vimeo'
|
|
|
|
"https://player.vimeo.com/video/#{settings[:id]}"
|
|
|
|
end
|
|
|
|
|
2020-08-18 13:01:18 +00:00
|
|
|
return match if !url
|
2020-02-19 17:07:52 +00:00
|
|
|
|
2020-11-23 17:12:53 +00:00
|
|
|
"<div class='videoWrapper'><iframe allowfullscreen id='#{settings[:provider]}#{settings[:id]}' type='text/html' src='#{url}' frameborder='0'></iframe></div>"
|
2020-02-19 17:07:52 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-18 17:25:04 +00:00
|
|
|
end
|