jekyll-linked-posts 0.1.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6fd76736e5b6a0ad623396a4fdcbae2ecade1d238c543d230f9838db8676d0f
4
- data.tar.gz: 9574fc8c1000bdb034ec270d512b63e3d08c0c4f85239dd8a70ad68417251255
3
+ metadata.gz: 062cad3991af05112873e7c17f8cf892c64a8d14d657062a09375bce163e939a
4
+ data.tar.gz: c0ed5ea2f74e4a9820d93c1a167ce4a6b5f58d8241da7743626f5d6547eecf13
5
5
  SHA512:
6
- metadata.gz: 1460920ac472f1c6507315ab62f5bb8b257bdb2e9e14bc783a662bcc9fc7b259d255b32cefcf300946f8e8900d1e54a8297e3b07622273c1e28ec60d8a6937cf
7
- data.tar.gz: 1807a345bcaeaf0603bf6da6c9a7a7865cd185497a304a48923a3fb255fab72dd1c0fd2e89a9eaca0f8d1bc737a3c38a6b23b409cc91757d75959a02732e7124
6
+ metadata.gz: 5993651482aa7722650f42c1d91af57eb1dad1c8be5b32418055b4d7d116164ba7e1609aa46c2ba8461ec6ca33374bb2936330f906506b56d11c35bc1e87ffcb
7
+ data.tar.gz: ddb553827ed04c755a0851baf3311c6b2713451b11e38a6371491fef3317d7f3c7e1986ff296a24d29c1a225da5a62daca37ced79b278ae63df35f1b789cf38f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.2.0 - 2020-09-03
4
+
5
+ * Breadcrumbs!
6
+
3
7
  ## v0.1.0 - 2020-05-20
4
8
 
5
9
  * First release!
data/README.md CHANGED
@@ -3,6 +3,9 @@
3
3
  This plugin finds the actual posts on front matter fields by UUID, for
4
4
  comfortable access on Liquid templates.
5
5
 
6
+ From v0.4.0 you don't need UUIDs and can use any field name you want
7
+ and this plugin will link string or integer values.
8
+
6
9
  We use them for [translations](https://rubygems.org/gems/jekyll-locales)
7
10
  and manually provided related posts.
8
11
 
@@ -90,6 +93,40 @@ them with other fields, re-add them to the configuration.
90
93
 
91
94
  It also works between collections!
92
95
 
96
+ ## Breadcrumbs
97
+
98
+ If you're linking articles by the same field, you can create
99
+ a breadcrumb like this:
100
+
101
+ ```html
102
+ {%- assign breadcrumbs = page | breadcrumbs: 'previous' -%}
103
+
104
+ <nav aria-label="Breadcrumbs">
105
+ <ol class="breadcrumb">
106
+ {% for crumb in breadcrumbs %}
107
+ {% if forloop.last %}
108
+ <li class="breadcrumb-item active" aria-current="page">{{ crumb.title }}</li>
109
+ {% else %}
110
+ <li class="breadcrumb-item">
111
+ <a href="{{ crumb.url }}">{{ crumb.title }}</a>
112
+ </li>
113
+ {% endif %}
114
+ {% endfor %}
115
+ </ol>
116
+ </nav>
117
+ ```
118
+
119
+ ## Link by any field name
120
+
121
+ If you don't want to use UUIDs, you can configure this plugin to link by
122
+ any field name as long as their values are strings or integers.
123
+
124
+ Add to your `_config.yml`:
125
+
126
+ ```yaml
127
+ indexed_field: title
128
+ ```
129
+
93
130
  ## Contributing
94
131
 
95
132
  Bug reports and pull requests are welcome on 0xacab.org at
@@ -98,6 +135,9 @@ intended to be a safe, welcoming space for collaboration, and
98
135
  contributors are expected to adhere to the [Sutty code of
99
136
  conduct](https://sutty.nl/en/code-of-conduct/).
100
137
 
138
+ If you like our plugins, [please consider
139
+ donating](https://donaciones.sutty.nl/en/)!
140
+
101
141
  ## License
102
142
 
103
143
  The gem is available as free software under the terms of the GPL3
@@ -1,4 +1,5 @@
1
1
  require_relative 'jekyll/linked_posts'
2
+ require_relative 'jekyll/filters/breadcrumbs'
2
3
 
3
4
  # Process the site replacing UUIDs for posts in certain front matter fields
4
5
  Jekyll::Hooks.register :site, :post_read do |site|
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Filters
5
+ module Breadcrumbs
6
+ # The breadcrumbs filter returns an Array of Jekyll::Documents
7
+ # that are linked recursively by the same field.
8
+ #
9
+ # @param [Jekyll::Document]
10
+ # @param [String]
11
+ # @return [Array]
12
+ def breadcrumbs(input, field)
13
+ return unless input.respond_to? :[]
14
+
15
+ crumbs = [ input ]
16
+ prev = input
17
+
18
+ while prev&.public_send(:[], field)
19
+ crumbs << (prev = prev[field])
20
+ end
21
+
22
+ crumbs.reverse
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ Liquid::Template.register_filter(Jekyll::Filters::Breadcrumbs)
@@ -1,7 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
- # Link posts by UUIDs
4
+ # Link posts by a field value, ideally an UUID
3
5
  class LinkedPosts
6
+ # Default linked fields
4
7
  FIELDS = %w[lang related].freeze
8
+ # Value types allowed to index by
9
+ INDEXABLE_TYPES = [String, Integer].freeze
5
10
 
6
11
  attr_reader :site
7
12
 
@@ -15,58 +20,89 @@ module Jekyll
15
20
  # If it doesn't find anything it'll show a warning and replace the
16
21
  # value for nil.
17
22
  def read
18
- docs.each do |doc|
23
+ site.documents.values.each do |doc|
19
24
  fields.each do |field|
20
- next unless doc.data.key? field
25
+ next if (values = doc.data[field]).nil?
21
26
 
22
27
  # XXX: this lacks... elegance
23
28
  doc.data[field] =
24
- case doc.data[field]
25
- when String then
26
- find doc.data[field]
27
- when Array then
28
- doc.data[field].map do |doc|
29
- find doc
30
- end.compact
31
- when Hash
32
- doc.data[field].map do |f, uuid|
33
- [f, find(uuid)]
34
- end.to_h
35
- else
36
- Jekyll.logger.warn "Couldn't link posts on #{doc.path}"
37
- nil
38
- end
29
+ case values
30
+ when String then find(values)
31
+ when Array
32
+ values.map do |doc|
33
+ find doc
34
+ end.compact
35
+ when Hash
36
+ values.map do |f, value|
37
+ [f, v] if (v = find value)
38
+ end.compact.to_h
39
+ else
40
+ Jekyll.logger.warn "Couldn't link posts on #{doc.relative_path}"
41
+ nil
39
42
  end
43
+ end
40
44
  end
41
45
  end
42
46
 
43
47
  private
44
48
 
45
- # Obtain docs from the whole site
46
- def docs
47
- @docs ||= site.collections.map(&:last).map(&:docs).flatten
49
+ # The field to index posts by
50
+ #
51
+ # @return [String] by default uuid
52
+ def indexed_field
53
+ @indexed_field ||= site.config.fetch('indexed_field', 'uuid')
54
+ end
55
+
56
+ # Obtain docs from the whole site, indexing them by field value and
57
+ # warning if docs have missing field, duplicate values or invalid
58
+ # types.
59
+ #
60
+ # @return [Hash]
61
+ def indexed_documents
62
+ @indexed_documents ||= site.documents.reduce({}) do |docs, doc|
63
+ index_value = doc.data[indexed_field]
64
+
65
+ unless index_value
66
+ Jekyll.logger.warn "#{doc.relative_path} is missing field #{indexed_field}"
67
+ next docs
68
+ end
69
+
70
+ unless INDEXABLE_TYPES.include? index_value.class
71
+ Jekyll.logger.warn "#{doc.relative_path} value for #{indexed_field} is #{index_value.class.downcase} instead of string or integer"
72
+ next docs
73
+ end
74
+
75
+ if (dup = docs[index_value])
76
+ Jekyll.logger.warn "#{doc.relative_path} shares the same indexing value as #{dup.relative_path}: #{index_value}"
77
+ next docs
78
+ end
79
+
80
+ docs.tap do |d|
81
+ d[index_value] = doc
82
+ end
83
+ end
48
84
  end
49
85
 
50
- # Search for UUIDs in these front matter fields. You can add more
86
+ # Search for posts in these front matter fields. You can add more
51
87
  # as an Array called 'fields' on _config.yml for the plugin
52
88
  # configuration:
53
89
  #
54
90
  # linked_fields:
55
91
  # - related
56
92
  # - lang
93
+ #
94
+ # @return [Array]
57
95
  def fields
58
96
  @fields ||= site.config.fetch('linked_fields', FIELDS).compact.uniq
59
97
  end
60
98
 
61
- # Find a post with the given UUID
62
- def find(uuid)
63
- r = docs.lazy.find do |doc|
64
- doc.data['uuid'] == uuid
99
+ # Find a post with the given value and log alert if missing.
100
+ #
101
+ # @return [Jekyll::Document,Nil]
102
+ def find(value)
103
+ indexed_documents[value].tap do |i|
104
+ Jekyll.logger.warn "#{value} couldn't be found" if i.nil?
65
105
  end
66
-
67
- Jekyll.logger.warn "#{uuid} couldn't be found" if r.nil?
68
-
69
- r
70
106
  end
71
107
  end
72
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-linked-posts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-20 00:00:00.000000000 Z
11
+ date: 2021-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -38,6 +38,7 @@ files:
38
38
  - LICENSE.txt
39
39
  - README.md
40
40
  - lib/jekyll-linked-posts.rb
41
+ - lib/jekyll/filters/breadcrumbs.rb
41
42
  - lib/jekyll/linked_posts.rb
42
43
  homepage: https://0xacab.org/sutty/jekyll/jekyll-linked-posts
43
44
  licenses:
@@ -48,7 +49,7 @@ metadata:
48
49
  source_code_uri: https://0xacab.org/sutty/jekyll/jekyll-linked-posts
49
50
  changelog_uri: https://0xacab.org/sutty/jekyll/jekyll-linked-posts/-/blob/master/CHANGELOG.md
50
51
  documentation_uri: https://rubydoc.info/gems/jekyll-linked-posts
51
- post_install_message:
52
+ post_install_message:
52
53
  rdoc_options:
53
54
  - "--title"
54
55
  - jekyll-linked-posts - Adds linked posts to Jekyll
@@ -70,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
71
  - !ruby/object:Gem::Version
71
72
  version: '0'
72
73
  requirements: []
73
- rubygems_version: 3.0.3
74
- signing_key:
74
+ rubygems_version: 3.1.2
75
+ signing_key:
75
76
  specification_version: 4
76
77
  summary: Adds linked posts to Jekyll
77
78
  test_files: []