jekyll-linked-posts 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -0
  3. data/lib/jekyll/linked_posts.rb +45 -16
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b90e7a22303bc20289f133ee54a2f59a60948728a9beb56ed6a4ac163f8d3678
4
- data.tar.gz: c22383603722fc768d7b02c80be83edab9032d86cc8ab86c1169f43a6255da10
3
+ metadata.gz: 39edda049f0e0f58c262ce78dbfdb628dcbe8a817ea09500106ee3b3997cd776
4
+ data.tar.gz: f8a917ff13833ab7ab122b565e3c1da5f93f8ce65fa323d7110449fc2bac6877
5
5
  SHA512:
6
- metadata.gz: aaa69aa67d59681692f3b359f2884b2a3949d7a1fa5caff19e768d8daac72d1b8871ba514c2c8f9d9b975134870cc5f9fc2c798825e6976afd3dd70f19d91f0f
7
- data.tar.gz: f4650ff35e5233daa11cdaa5b6a24ab8bcc13665c612274e41afaf3adffd8d8bd206bef1537f7b7c9752822ea854c2e5142bda4a3e11e4fe25a8e31d9d27cbe2
6
+ metadata.gz: 72367aa83c205d0e70422006e90375d6f6bb520f480950583544e19b6fffed1336578efa629e2cf352e915c094ed5b3c51ea2f4dae38e8582563ac810ae0475a
7
+ data.tar.gz: 0dd64960259f80cc7db62bf49668d7932aee32b9669d0c248a90bfff47dc2979f1da64a412eabf1c2da06e006607e47d835a7bfd802b1e68a51c52b759b0e14c
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
 
@@ -113,6 +116,17 @@ a breadcrumb like this:
113
116
  </nav>
114
117
  ```
115
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
+
116
130
  ## Contributing
117
131
 
118
132
  Bug reports and pull requests are welcome on 0xacab.org at
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jekyll
4
- # Link posts by UUIDs
4
+ # Link posts by a field value, ideally an UUID
5
5
  class LinkedPosts
6
+ # Default linked fields
6
7
  FIELDS = %w[lang related].freeze
8
+ # Value types allowed to index by
9
+ INDEXABLE_TYPES = [String, Integer].freeze
7
10
 
8
11
  attr_reader :site
9
12
 
@@ -19,22 +22,22 @@ module Jekyll
19
22
  def read
20
23
  indexed_documents.values.each do |doc|
21
24
  fields.each do |field|
22
- next unless doc.data.key? field
25
+ next if (values = doc.data[field]).nil?
23
26
 
24
27
  # XXX: this lacks... elegance
25
28
  doc.data[field] =
26
- case doc.data[field]
27
- when String then find(doc.data[field])
29
+ case values
30
+ when String then find(values)
28
31
  when Array
29
- doc.data[field].map do |doc|
32
+ values.map do |doc|
30
33
  find doc
31
34
  end.compact
32
35
  when Hash
33
- doc.data[field].map do |f, uuid|
34
- [f, find(uuid)]
35
- end.to_h
36
+ values.map do |f, value|
37
+ [f, v] if (v = find value)
38
+ end.compact.to_h
36
39
  else
37
- Jekyll.logger.warn "Couldn't link posts on #{doc.path}"
40
+ Jekyll.logger.warn "Couldn't link posts on #{doc.relative_path}"
38
41
  nil
39
42
  end
40
43
  end
@@ -43,18 +46,44 @@ module Jekyll
43
46
 
44
47
  private
45
48
 
46
- # Obtain docs from the whole site, indexing them by uuid
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.
47
59
  #
48
60
  # @return [Hash]
49
61
  def indexed_documents
50
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
+
51
80
  docs.tap do |d|
52
- d[doc.data['uuid']] = doc
81
+ d[index_value] = doc
53
82
  end
54
83
  end
55
84
  end
56
85
 
57
- # 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
58
87
  # as an Array called 'fields' on _config.yml for the plugin
59
88
  # configuration:
60
89
  #
@@ -67,12 +96,12 @@ module Jekyll
67
96
  @fields ||= site.config.fetch('linked_fields', FIELDS).compact.uniq
68
97
  end
69
98
 
70
- # Find a post with the given UUID and log alert if missing.
99
+ # Find a post with the given value and log alert if missing.
71
100
  #
72
101
  # @return [Jekyll::Document,Nil]
73
- def find(uuid)
74
- indexed_documents[uuid].tap do |i|
75
- Jekyll.logger.warn "#{uuid} couldn't be found" if i.nil?
102
+ def find(value)
103
+ indexed_documents[value].tap do |i|
104
+ Jekyll.logger.warn "#{value} couldn't be found" if i.nil?
76
105
  end
77
106
  end
78
107
  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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-07 00:00:00.000000000 Z
11
+ date: 2021-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll