Module: Jekyll::Filters::Arrays

Defined in:
lib/jekyll/filters/arrays.rb

Instance Method Summary collapse

Instance Method Details

#concat_or_append(input, concatenable) ⇒ Object

Join arrays or append item to array

Examples:

{% assign things = site.posts | where: "layout", "thing" %}
{% assign being = site.posts | find: "layout", "being" %}
{% assign dread = things | concat_or_append: being %}

Parameters:

  • input (Array)
  • concatenable (Array, any)


51
52
53
54
55
56
57
58
59
# File 'lib/jekyll/filters/arrays.rb', line 51

def concat_or_append(input, concatenable)
  input = [] unless array? input

  if concatenable.is_a? Array
    input + concatenable
  else
    input.dup << concatenable
  end
end

#flatten(input, level = nil) ⇒ Array

Flattens nested arrays

Examples:

{{ site.posts | map: 'categories' | flatten: 1 }}

Parameters:

  • input (any)
  • level (Integer) (defaults to: nil)

Returns:

  • (Array)


38
39
40
# File 'lib/jekyll/filters/arrays.rb', line 38

def flatten(input, level = nil)
  [input].flatten(level)
end

#infinite_next(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>

Finds the next posts in a collection, starting from current post and restarts from the beginning to always return posts.

Examples:

{{ page | infinite_next: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)

    The current post (page)

  • posts (Array)

    An array of posts (ie. results from where filter)

  • amount (Integer) (defaults to: 1)

    Amount of posts next to current, max is posts size

Returns:

  • (Array<Jekyll::Document,Drop>)

    Array of posts



70
71
72
73
74
75
76
77
# File 'lib/jekyll/filters/arrays.rb', line 70

def infinite_next(input, posts, amount = 1)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts.rotate(index).slice(1, amount) if index
end

#infinite_prev(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>

Finds the previous posts in a collection, starting from current post and restarts from the beginning to always return posts.

Examples:

{{ page | infinite_next: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)

    The current post (page)

  • posts (Array<Jekyll::Document,Drop>)

    An array of posts (ie. results from where filter)

  • amount (Integer) (defaults to: 1)

    Amount of posts previous to current, max is posts size

Returns:

  • (Array<Jekyll::Document,Drop>)

    Array of posts



88
89
90
91
92
93
94
95
# File 'lib/jekyll/filters/arrays.rb', line 88

def infinite_prev(input, posts, amount = 1)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts.rotate(index).reverse.slice(0, amount).reverse if index
end

#next(input, posts) ⇒ Drop?

Return next post from an array or nothing if post is last.

Examples:

{{ page | next: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)
  • posts (Array<Jekyll::Document,Drop>)

Returns:

  • (Drop, nil)


104
105
106
107
108
109
110
111
# File 'lib/jekyll/filters/arrays.rb', line 104

def next(input, posts)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts[index + 1] if index
end

#prev(input, posts) ⇒ Drop? Also known as: previous

Return previous post from an array, or nothing if post is the first item.

Examples:

{{ page | prev: site.posts }}
{{ page | previous: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)
  • array (Array)

Returns:

  • (Drop, nil)


122
123
124
125
126
127
128
129
# File 'lib/jekyll/filters/arrays.rb', line 122

def prev(input, posts)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts[index - 1] if index&.positive?
end

#sample(input, amount = 1) ⇒ Array

Returns one or several random items from an Array.

Examples:

{{ site.posts | sample }}
{{ site.posts | sample: 3 }}

Parameters:

  • input (Array)
  • amount (Integer) (defaults to: 1)

Returns:

  • (Array)


14
15
16
17
18
# File 'lib/jekyll/filters/arrays.rb', line 14

def sample(input, amount = 1)
  input = [] unless array? input

  input.sample(amount)
end

#shuffle(input) ⇒ Array

Returns list of random items from an Array.

Examples:

{{ site.posts | shuffle }}

Parameters:

  • input (Array)

Returns:

  • (Array)


26
27
28
29
# File 'lib/jekyll/filters/arrays.rb', line 26

def shuffle(input)
  input = [] unless array? input
  input.shuffle
end