Module: FastComments::Jekyll::Util

Defined in:
lib/fastcomments/jekyll/util.rb

Overview

Low-level helpers ported from the fastcomments-11ty util.ts so the emitted client JS is byte-equivalent.

Constant Summary collapse

ESCAPE_FOR_SCRIPT =
{
  "<" => "\\u003C",
  ">" => "\\u003E",
  "&" => "\\u0026",
  "" => "\\u2028",
  "" => "\\u2029"
}.freeze

Class Method Summary collapse

Class Method Details

.escape_attr(value) ⇒ Object

Escape a value for use inside a double-quoted HTML attribute (& first).



25
26
27
28
29
30
31
# File 'lib/fastcomments/jekyll/util.rb', line 25

def escape_attr(value)
  value.to_s
       .gsub("&", "&amp;")
       .gsub('"', "&quot;")
       .gsub("<", "&lt;")
       .gsub(">", "&gt;")
end

.escape_for_script(str) ⇒ Object

Escape the characters that can break out of an inline <script> body.



20
21
22
# File 'lib/fastcomments/jekyll/util.rb', line 20

def escape_for_script(str)
  str.to_s.gsub(/[<>&

]/) { |c| ESCAPE_FOR_SCRIPT[c] }
end

.get_cdn_base(region) ⇒ Object



38
39
40
# File 'lib/fastcomments/jekyll/util.rb', line 38

def get_cdn_base(region)
  region.to_s == "eu" ? "https://cdn-eu.fastcomments.com" : "https://cdn.fastcomments.com"
end

.json_for_script(value) ⇒ Object

JSON-encode a value for safe embedding in an inline <script>.



34
35
36
# File 'lib/fastcomments/jekyll/util.rb', line 34

def json_for_script(value)
  escape_for_script(JSON.generate(value))
end

.make_container_id(prefix) ⇒ Object

Container ids only need to be unique within a page.



54
55
56
# File 'lib/fastcomments/jekyll/util.rb', line 54

def make_container_id(prefix)
  "#{prefix}-#{SecureRandom.alphanumeric(7).downcase}"
end

.sanitize_config(config) ⇒ Object

Drop nil values; keep scalars/arrays/hashes (mirrors the TS sanitizeConfig).



43
44
45
46
47
48
49
50
51
# File 'lib/fastcomments/jekyll/util.rb', line 43

def sanitize_config(config)
  return {} unless config.is_a?(Hash)

  config.each_with_object({}) do |(key, value), out|
    next if value.nil?

    out[key] = value
  end
end