al_comments 0.1.0 → 0.1.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/CHANGELOG.md +15 -0
  3. data/lib/al_comments.rb +127 -34
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8582e0b0a66b49c71cafbf872b834a2154e5221f8dc1520aac1b34b0e3fbbf8
4
- data.tar.gz: 0fb8762fba4a9e91594be452e2584ff47726d9c41b5aeea8b91ccce4bf9f52a5
3
+ metadata.gz: b21be4d1edaba6ece5ead7b8a4a367d44f8931ba71e789923d5420a3edc88958
4
+ data.tar.gz: 378e2a1016b2e1775665ab3ff6ade1bf21fd4cc9c37ba24d31efcba5f7b56ae1
5
5
  SHA512:
6
- metadata.gz: 8cd9477e85aecc152ac1ef75dd40d6663de2ff54835d41b435128893752f1d45586481a443ed07898adc66813207747b365ee0e73021b591a5067c916097ad0f
7
- data.tar.gz: 9d66f7bfd9f311fa5dfe9811a65329b3817f4ab7d6c6184352bae96e5fe5c05db95b4b5c06e775e90fe92b5d4aa92fb3b682b241fa4a2f03374317daa84ca956
6
+ metadata.gz: 458b7d6f979287163ccdc1ee7a59be3d8989aace5cf526bb6826809e4a0fbacba2e9255f8796d4045eb703dc9a7982e4454109b30f377171b93da38b2def58d1
7
+ data.tar.gz: 3eb9b4fc9c64fc6414d5411635503a37ee8fff72edcc8d9e26498f4eb5959874f5d435fc450fcbabb9b7503cda215e772f579aef559af6a678d94c959a8948a8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.2 - 2026-02-07
4
+ - Aligned giscus activation behavior with al-folio main branch expectations:
5
+ - Removed fallback from `site.repository` / `site.github.repository_nwo` to avoid partially configured giscus injection.
6
+ - Requires `repo`, `repo_id`, `category`, and `category_id` before loading giscus client script.
7
+ - Shows theme-side warning with missing required keys when configuration is incomplete.
8
+ - Expanded tests for incomplete giscus config handling.
9
+
10
+ ## 0.1.1 - 2026-02-07
11
+ - Improved config compatibility for comment rendering:
12
+ - Supports both string and symbol keys for nested config values.
13
+ - Supports `disqus.shortname` in addition to `disqus_shortname`.
14
+ - Supports `giscus.repo` aliases and fallback from `repository` / `github.repository_nwo`.
15
+ - Accepts common truthy flag values (`1`, `yes`, `on`) for `*_comments`.
16
+ - Added automated tests for Giscus/Disqus rendering paths and config fallbacks.
17
+
3
18
  ## 0.1.0 - 2026-02-07
4
19
  - Initial gem release.
5
20
  - Added standalone Giscus/Disqus comment rendering tag for Jekyll/al-folio sites.
data/lib/al_comments.rb CHANGED
@@ -9,12 +9,13 @@ module AlComments
9
9
 
10
10
  page = context.registers[:page] || context['page'] || {}
11
11
  output = []
12
+ disqus_shortname = resolve_disqus_shortname(site)
12
13
 
13
- if site.config['disqus_shortname'] && truthy?(page['disqus_comments'])
14
- output << disqus_html(site, page)
14
+ if disqus_shortname && truthy?(fetch_key(page, 'disqus_comments'))
15
+ output << disqus_html(site, page, disqus_shortname)
15
16
  end
16
17
 
17
- if truthy?(page['giscus_comments'])
18
+ if truthy?(fetch_key(page, 'giscus_comments'))
18
19
  output << giscus_html(site, page)
19
20
  end
20
21
 
@@ -24,20 +25,108 @@ module AlComments
24
25
  private
25
26
 
26
27
  def truthy?(value)
27
- value == true || value.to_s == 'true'
28
+ return false if value.nil? || value == false
29
+ return true if value == true
30
+ return true if value.is_a?(Numeric) && value != 0
31
+
32
+ normalized = value.to_s.strip.downcase
33
+ %w[true 1 yes y on].include?(normalized)
28
34
  end
29
35
 
30
36
  def post_layout?(page)
31
- page['layout'].to_s == 'post'
37
+ fetch_key(page, 'layout').to_s == 'post'
38
+ end
39
+
40
+ def fetch_key(hash, *keys)
41
+ return nil unless hash.respond_to?(:key?)
42
+
43
+ keys.each do |key|
44
+ return hash[key] if hash.key?(key)
45
+
46
+ string_key = key.to_s
47
+ return hash[string_key] if hash.key?(string_key)
48
+
49
+ symbol_key = key.respond_to?(:to_sym) ? key.to_sym : nil
50
+ return hash[symbol_key] if symbol_key && hash.key?(symbol_key)
51
+ end
52
+
53
+ nil
32
54
  end
33
55
 
34
- def disqus_html(site, page)
56
+ def value_blank?(value)
57
+ value.nil? || value.to_s.strip.empty?
58
+ end
59
+
60
+ def resolve_disqus_shortname(site)
61
+ direct = fetch_key(site.config, 'disqus_shortname')
62
+ return direct.to_s unless value_blank?(direct)
63
+
64
+ disqus = fetch_key(site.config, 'disqus')
65
+ return nil unless disqus.is_a?(Hash)
66
+
67
+ nested = fetch_key(disqus, 'shortname')
68
+ return nil if value_blank?(nested)
69
+
70
+ nested.to_s
71
+ end
72
+
73
+ def resolve_giscus_config(site)
74
+ giscus = fetch_key(site.config, 'giscus')
75
+ return giscus if giscus.is_a?(Hash)
76
+
77
+ comments = fetch_key(site.config, 'comments')
78
+ return {} unless comments.is_a?(Hash)
79
+
80
+ nested = fetch_key(comments, 'giscus')
81
+ nested.is_a?(Hash) ? nested : {}
82
+ end
83
+
84
+ def resolve_giscus_repo(_site, giscus)
85
+ repo = fetch_key(giscus, 'repo', 'repository', 'repo_name')
86
+ return '' if value_blank?(repo)
87
+
88
+ repo.to_s
89
+ end
90
+
91
+ def missing_giscus_fields(giscus, repo)
92
+ required = {
93
+ 'repo' => repo,
94
+ 'repo_id' => fetch_key(giscus, 'repo_id'),
95
+ 'category' => fetch_key(giscus, 'category'),
96
+ 'category_id' => fetch_key(giscus, 'category_id')
97
+ }
98
+
99
+ required.each_with_object([]) do |(name, value), missing|
100
+ missing << name if value_blank?(value)
101
+ end
102
+ end
103
+
104
+ def giscus_warning_html(style, spacer, missing_fields)
105
+ details = if missing_fields.empty?
106
+ ''
107
+ else
108
+ "<p>Missing required keys: <code>#{missing_fields.join(', ')}</code>.</p>"
109
+ end
110
+
111
+ warning = <<~HTML
112
+ <blockquote class="block-danger">
113
+ <h5>giscus comments misconfigured</h5>
114
+ <p>Please follow instructions at <a href="http://giscus.app">http://giscus.app</a> and update your giscus configuration.</p>
115
+ #{details}
116
+ </blockquote>
117
+ HTML
118
+
119
+ %(<div id="giscus_thread"#{style}>#{spacer}\n#{warning}</div>)
120
+ end
121
+
122
+ def disqus_html(site, page, disqus_shortname)
123
+ max_width = fetch_key(site.config, 'max_width')
35
124
  <<~HTML
36
- <div id="disqus_thread" style="max-width: #{site.config['max_width']}; margin: 0 auto;">
125
+ <div id="disqus_thread" style="max-width: #{max_width}; margin: 0 auto;">
37
126
  <script type="text/javascript">
38
- var disqus_shortname = #{site.config['disqus_shortname'].to_json};
39
- var disqus_identifier = #{page['id'].to_s.to_json};
40
- var disqus_title = #{page['title'].to_s.to_json};
127
+ var disqus_shortname = #{disqus_shortname.to_json};
128
+ var disqus_identifier = #{fetch_key(page, 'id').to_s.to_json};
129
+ var disqus_title = #{fetch_key(page, 'title').to_s.to_json};
41
130
  (function() {
42
131
  var dsq = document.createElement('script');
43
132
  dsq.type = 'text/javascript';
@@ -55,17 +144,15 @@ module AlComments
55
144
  end
56
145
 
57
146
  def giscus_html(site, page)
58
- giscus = site.config['giscus'] || {}
59
- style = post_layout?(page) ? " style=\"max-width: #{site.config['max_width']}; margin: 0 auto;\"" : ''
147
+ giscus = resolve_giscus_config(site)
148
+ repo = resolve_giscus_repo(site, giscus)
149
+ max_width = fetch_key(site.config, 'max_width')
150
+ style = post_layout?(page) ? " style=\"max-width: #{max_width}; margin: 0 auto;\"" : ''
60
151
  spacer = post_layout?(page) ? "\n <br>" : ''
152
+ missing_fields = missing_giscus_fields(giscus, repo)
61
153
 
62
- if giscus['repo'].to_s.strip.empty?
63
- warning = <<~MARKDOWN
64
- > ##### giscus comments misconfigured
65
- > Please follow instructions at [http://giscus.app](http://giscus.app) and update your giscus configuration.
66
- {: .block-danger }
67
- MARKDOWN
68
- return %(<div id="giscus_thread"#{style}>#{spacer}\n#{Jekyll::Converters::Markdown.new(site.config).convert(warning)}</div>)
154
+ unless missing_fields.empty?
155
+ return giscus_warning_html(style, spacer, missing_fields)
69
156
  end
70
157
 
71
158
  <<~HTML
@@ -79,17 +166,17 @@ module AlComments
79
166
  var giscusTheme = determineGiscusTheme();
80
167
  var attrs = {
81
168
  src: "https://giscus.app/client.js",
82
- "data-repo": #{giscus['repo'].to_s.to_json},
83
- "data-repo-id": #{giscus['repo_id'].to_s.to_json},
84
- "data-category": #{giscus['category'].to_s.to_json},
85
- "data-category-id": #{giscus['category_id'].to_s.to_json},
86
- "data-mapping": #{giscus['mapping'].to_s.to_json},
87
- "data-strict": #{giscus['strict'].to_s.to_json},
88
- "data-reactions-enabled": #{giscus['reactions_enabled'].to_s.to_json},
89
- "data-emit-metadata": #{giscus['emit_metadata'].to_s.to_json},
90
- "data-input-position": #{giscus['input_position'].to_s.to_json},
169
+ "data-repo": #{repo.to_s.to_json},
170
+ "data-repo-id": #{fetch_key(giscus, 'repo_id').to_s.to_json},
171
+ "data-category": #{fetch_key(giscus, 'category').to_s.to_json},
172
+ "data-category-id": #{fetch_key(giscus, 'category_id').to_s.to_json},
173
+ "data-mapping": #{fetch_key(giscus, 'mapping').to_s.to_json},
174
+ "data-strict": #{fetch_key(giscus, 'strict').to_s.to_json},
175
+ "data-reactions-enabled": #{fetch_key(giscus, 'reactions_enabled').to_s.to_json},
176
+ "data-emit-metadata": #{fetch_key(giscus, 'emit_metadata').to_s.to_json},
177
+ "data-input-position": #{fetch_key(giscus, 'input_position').to_s.to_json},
91
178
  "data-theme": giscusTheme,
92
- "data-lang": #{giscus['lang'].to_s.to_json},
179
+ "data-lang": #{fetch_key(giscus, 'lang').to_s.to_json},
93
180
  crossorigin: "anonymous",
94
181
  async: true
95
182
  };
@@ -109,16 +196,22 @@ module AlComments
109
196
  end
110
197
 
111
198
  def theme_detection(site)
112
- if site.config['enable_darkmode']
199
+ giscus = resolve_giscus_config(site)
200
+ dark_theme = fetch_key(giscus, 'dark_theme').to_s
201
+ light_theme = fetch_key(giscus, 'light_theme').to_s
202
+ dark_theme = 'dark' if value_blank?(dark_theme)
203
+ light_theme = 'light' if value_blank?(light_theme)
204
+
205
+ if fetch_key(site.config, 'enable_darkmode')
113
206
  <<~JS
114
207
  var theme = localStorage.getItem("theme") || document.documentElement.getAttribute("data-theme") || "system";
115
- if (theme === "dark") return #{site.config.dig('giscus', 'dark_theme').to_s.to_json};
116
- if (theme === "light") return #{site.config.dig('giscus', 'light_theme').to_s.to_json};
208
+ if (theme === "dark") return #{dark_theme.to_json};
209
+ if (theme === "light") return #{light_theme.to_json};
117
210
  var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
118
- return prefersDark ? #{site.config.dig('giscus', 'dark_theme').to_s.to_json} : #{site.config.dig('giscus', 'light_theme').to_s.to_json};
211
+ return prefersDark ? #{dark_theme.to_json} : #{light_theme.to_json};
119
212
  JS
120
213
  else
121
- %(return #{site.config.dig('giscus', 'light_theme').to_s.to_json};)
214
+ %(return #{light_theme.to_json};)
122
215
  end
123
216
  end
124
217
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: al_comments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - al-org