Module: Plum::LiquidFilters

Defined in:
app/services/plum/liquid_filters.rb

Instance Method Summary collapse

Instance Method Details

#image_url(input, size = nil) ⇒ Object



29
30
31
32
33
34
# File 'app/services/plum/liquid_filters.rb', line 29

def image_url(input, size = nil)
  return "" if input.blank?
  return input["url"].to_s unless size && input.is_a?(Hash)

  input[size].presence || input["url"].to_s
end

#markdown(input) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/plum/liquid_filters.rb', line 6

def markdown(input)
  return "" if input.blank?

  text = input.to_s
  return text if text.match?(/<[a-z][\s\S]*>/i)

  renderer = Redcarpet::Render::HTML.new(
    hard_wrap: true,
    link_attributes: { target: "_blank", rel: "noopener" }
  )

  md = Redcarpet::Markdown.new(renderer,
    autolink: true,
    tables: true,
    fenced_code_blocks: true,
    strikethrough: true,
    superscript: true,
    no_intra_emphasis: true
  )

  md.render(text)
end

#table_of_contents(input) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/plum/liquid_filters.rb', line 43

def table_of_contents(input)
  return "" if input.blank?

  headings = Nokogiri::HTML.fragment(input.to_s).css("h2, h3")
  return "" if headings.empty?

  items = headings.map do |heading|
    id = heading["id"].presence || heading.text.parameterize
    label = ERB::Util.html_escape(heading.text)
    %(<li class="toc-#{heading.name}"><a href="##{id}">#{label}</a></li>)
  end

  %(<nav class="table-of-contents" aria-label="On this page"><p>On this page</p><ol>#{items.join}</ol></nav>)
end

#theme_asset_url(input) ⇒ Object



36
37
38
39
40
41
# File 'app/services/plum/liquid_filters.rb', line 36

def theme_asset_url(input)
  builder = @context.registers[:theme_asset_url_builder]
  return "" unless builder

  builder.call(input)
end