-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathhtml_rouge.rb
More file actions
87 lines (77 loc) · 3.18 KB
/
html_rouge.rb
File metadata and controls
87 lines (77 loc) · 3.18 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require "rouge/plugins/redcarpet"
module Redcarpet
module Render
class HTMLRouge < HTML
include Rouge::Plugins::Redcarpet
# Rouge requires the hint language to be lower case, by overriding this
# method we can allow the hint language to be specified with other casings
# eg. `Ada` instead of `ada`
def block_code(code, language)
super(code, language.to_s.downcase)
end
def image(link, title, alt_text)
# Check if the URL is an image and process accordingly
if %r{\Ahttps?://}.match?(link)
modified_url = MediaStore.find_by(original_url: link)&.output_url || link
title_attr = title ? %( title="#{title}") : ""
alt_text_attr = alt_text ? %( alt="#{alt_text}") : ""
%(<img src="#{modified_url}"#{title_attr}#{alt_text_attr}/>)
else
title_attr = title ? %( title="#{title}") : ""
alt_text_attr = alt_text ? %( alt="#{alt_text}") : ""
%(<img src="#{link}"#{title_attr}#{alt_text_attr}/>)
end
end
def link(link, _title, content) # rubocop:disable Metrics/PerceivedComplexity
# Regex to capture src, alt, and title attributes from an img tag
if content&.include?("<img") && (doc = Nokogiri::HTML(content))
image_url = doc.at_css("img")["src"]
alt_text = doc.at_css("img")["alt"] || nil
title = doc.at_css("img")["title"] || nil
modified_content = image(image_url, title, alt_text) # Call your image method with title and alt text
%(<a href="#{link}">#{modified_content}</a>)
else
# Proceed with normal link rendering if no image is detected
return if %r{<a\s.+/a>}.match?(content)
link_attributes = ""
@options[:link_attributes]&.each do |attribute, value|
link_attributes += %( #{attribute}="#{value}")
end
if (%r{https?://\S+}.match? link) || link.nil?
%(<a href="#{link}"#{link_attributes}>#{content}</a>)
elsif %r{\w+:}.match?(link)
# Handle links with protocols (mailto:, tel:, ftp:, etc.)
%(<a href="#{link}"#{link_attributes}>#{content}</a>)
elsif /\.{1}/.match? link
%(<a href="//#{link}"#{link_attributes}>#{content}</a>)
elsif link.start_with?("#")
%(<a href="#{link}"#{link_attributes}>#{content}</a>)
else
%(<a href="#{app_protocol}#{app_domain}#{link}"#{link_attributes}>#{content}</a>)
end
end
end
def header(title, header_number)
anchor_link = slugify(title)
<<~HEREDOC
<h#{header_number}>
<a name="#{anchor_link}" href="##{anchor_link}" class="anchor">
</a>
#{title}
</h#{header_number}>
HEREDOC
end
private
def app_protocol
ApplicationConfig["APP_PROTOCOL"]
end
def app_domain
Settings::General.app_domain
end
def slugify(string)
stripped_string = ActionView::Base.full_sanitizer.sanitize string
stripped_string.downcase.gsub(EmojiRegex::RGIEmoji, "").strip.gsub(/[[:punct:]]/u, "").gsub(/\s+/, "-")
end
end
end
end