Class: Jekyll::ClientSearch::RelatedTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/client_search/related_tag.rb

Overview

Liquid tag that renders the related-articles container, sort control, and runtime scripts in one line. Drop into any post layout:

{% related_articles %}
{% related_articles sort:date %}
{% related_articles no_scripts %}

When related.enabled is false the tag renders nothing, so it is safe to leave in a layout even when the feature is off.

Constant Summary collapse

SYNTAX =
/\A\s*(sort:(\w+))?\s*(no_scripts)?\s*\z/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ RelatedTag

Returns a new instance of RelatedTag.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jekyll/client_search/related_tag.rb', line 17

def initialize(tag_name, markup, tokens)
  super
  @markup = markup.to_s
  unless (match = @markup.match(SYNTAX))
    raise Liquid::SyntaxError,
          "related_articles: invalid syntax. Use {% related_articles %}, " \
          "{% related_articles sort:date %}, or {% related_articles no_scripts %}"
  end

  @sort = match[2] if match[2]
  @include_scripts = match[3].nil?
end

Instance Method Details

#render(context) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll/client_search/related_tag.rb', line 30

def render(context)
  site = context.registers[:site]
  config = site&.config&.fetch("client_search", {})
  return "" unless related_enabled?(config)

  asset_prefix = asset_prefix(site)
  sort_attr = @sort ? " data-related-sort=\"#{@sort}\"" : ""
  scripts = build_scripts(asset_prefix)
  build_html(sort_attr, scripts)
end