jekyll-linked-posts 0.2.0 → 0.4.2

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 +66 -30
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 567b5bf4cd5fbc9ed6b521ed1a76d6b609ac4bdd0d5e61b01f963ffe81ec6229
4
- data.tar.gz: 9fbca911800e53b4259be09b74d82c8d96e8e59c555a276b774310bf4b90cab1
3
+ metadata.gz: 0c792272b2c2f7fe9e8b9464f35b5743458229ecba36f3e1bed1e471418ee052
4
+ data.tar.gz: d863dd78fd66ff0572d29a5cda8326e92632acf33b68b5793dd62a55d1e76a17
5
5
  SHA512:
6
- metadata.gz: 0d52bf528f5ad92507d44c698321e95cdfc7b1bd6506b5c618f6f4aafea9e97d2fdf76b56b0288653e4eeb1611b5541a19f22f4c776cafd78b82143d24cc46ae
7
- data.tar.gz: f1ecddd44fcc19576bbc58382df7371587ab3bcd60bb4c5b3ff74fc26c0a7af48d574324714633a24f13bf5310359ea76f27780c17121d8dd11997a4dea55ef9
6
+ metadata.gz: ce5a1eb85f8976e80635391bd49fd5131d2f733571355cef6fc2de6e082002477f36351465adcaebfa7e40e747f2587a29b5f7e17061369f055fca3c040d3a24
7
+ data.tar.gz: a8a56dcff3ad0ce0d1850e0e0254e0b8bb3a809953ae825959910aa51ddc177972ee8b7e694557fc9f07002e7b0fb7f0dacdd61ef306e27b9f5c4da34d915e12
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,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.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.2.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-03 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
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  - !ruby/object:Gem::Version
72
72
  version: '0'
73
73
  requirements: []
74
- rubygems_version: 3.0.3
74
+ rubygems_version: 3.1.2
75
75
  signing_key:
76
76
  specification_version: 4
77
77
  summary: Adds linked posts to Jekyll