-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathpreview_helper.rb
More file actions
53 lines (38 loc) · 1.45 KB
/
preview_helper.rb
File metadata and controls
53 lines (38 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true
module PreviewHelper
AVAILABLE_PRISM_LANGUAGES = %w[ruby erb haml]
FALLBACK_LANGUAGE = "ruby"
def preview_source
return if @render_args.nil?
render "preview_source"
end
def prism_css_source_url
serve_static_preview_assets? ? asset_path("prism.css", skip_pipeline: true) : "https://cdn.jsdelivr.net/npm/prismjs@1.28.0/themes/prism.min.css"
end
def prism_js_source_url
serve_static_preview_assets? ? asset_path("prism.min.js", skip_pipeline: true) : "https://cdn.jsdelivr.net/npm/prismjs@1.28.0/prism.min.js"
end
def find_template_data_for_preview_source(lookup_context:, template_identifier:)
template = lookup_context.find_template(template_identifier)
{
source: template.source,
prism_language_name: prism_language_name_by_template(template: template)
}
end
private
def prism_language_name_by_template(template:)
language = template.identifier.split(".").last
return FALLBACK_LANGUAGE unless AVAILABLE_PRISM_LANGUAGES.include? language
language
end
# :nocov:
def prism_language_name_by_template_path(template_file_path:)
language = template_file_path.gsub(".html", "").split(".").last
return FALLBACK_LANGUAGE unless AVAILABLE_PRISM_LANGUAGES.include? language
language
end
# :nocov:
def serve_static_preview_assets?
Rails.application.config.view_component.show_previews && Rails.application.config.public_file_server.enabled
end
end