jekyll-hive 1.0.0 → 1.1.0

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: 1a498c5b88a46e157b96371105a23234fe02fbdc595ffe332615f92c5d8dbfb4
4
- data.tar.gz: e34b162f75d5225851831bb24239be567ca3e336d58d29d6549d17481b1ad356
3
+ metadata.gz: 529a97b1677713768a1334a0f8efca5123ee363fb012d08c8d1b866f5fc83dc0
4
+ data.tar.gz: c854107108ff8a867091e8156f1236b840acf55f110ac22ae653a89f27645636
5
5
  SHA512:
6
- metadata.gz: 7ba17593792b5d8f17339e20ba4ef1a6b77297ede9ced3925fa25d3b860dd70c78133df802024065b647e436df83ce9f9b07ad065c95f87c19af31fb531deaf7
7
- data.tar.gz: f4529f539627e83f5e0e063cd5ab1892671c51c8756356c0b042cb405a3fc701899238289048fc192a6151458abb749cbe48e66d53153b2fa57d4f56783436ea
6
+ metadata.gz: 5196c21173e790479dd12cf979d80deb26d4243c0bbd6af7c5452baed0e4876f481c895fe4ad168fda6c3b924cee5c1c196af3a2b0bbd91af5c20c02014d6173
7
+ data.tar.gz: 2820d8010e9b966d20b9e4cd89b6efb903978c7afe549c9e4b6faa581b9587563bb26b13cedfaf8fb25c95e290072682038d4f07b96c6c8c99cfdf481f6c3d80
data/README.md CHANGED
@@ -42,6 +42,14 @@ Use the tag as follows in your Jekyll pages, posts and collections:
42
42
  ```
43
43
  This will place the associated content on the page.
44
44
 
45
+ ## Note About Turbolinks
46
+
47
+ If you're using Turbolinks on your Jekyll site, you should consider adding the following line to your main includes, e.g. `_includes/head.html`:
48
+
49
+ ```html
50
+ <script src='https://unpkg.com/steem-content-renderer'></script>
51
+ ```
52
+
45
53
  ## Jekyll Build
46
54
 
47
55
  When building your site with jekyll, you can continue to use the default command:
@@ -56,6 +64,41 @@ If you would like to provide an alternate node:
56
64
  NODE_URL=https://anyx.io jekyll build
57
65
  ```
58
66
 
67
+ ## Jekyll Clean
68
+
69
+ To completely rebuild your site without cache:
70
+
71
+ ```bash
72
+ rm -rf .jekyll-hive-cache && jekyll clean && jekyll build
73
+ ```
74
+
75
+ ## Troubleshooting
76
+
77
+ #### I saw several messages like:
78
+
79
+ ```
80
+ Retrying: stoodkev/how-to-use-hivejs-or-other-modules-referencing-core-node-js-modules-on-react-native (Failed to open TCP connection to api.hive.blog:443 (getaddrinfo: nodename nor servname provided, or not known))
81
+ Retrying: stoodkev/how-to-use-hivejs-or-other-modules-referencing-core-node-js-modules-on-react-native (Failed to open TCP connection to api.openhive.network:443 (getaddrinfo: nodename nor servname provided, or not known))
82
+ .
83
+ .
84
+ .
85
+ Gave up on: stoodkev/how-to-use-hivejs-or-other-modules-referencing-core-node-js-modules-on-react-native
86
+ ```
87
+
88
+ Check your internet connection or provide an alternate node:
89
+
90
+ ```
91
+ NODE_URL=https://anyx.io jekyll build
92
+ ```
93
+
94
+ #### The post has changed on the blockchain, but `jekyll-hive` won't update to the latest post revision.
95
+
96
+ The cached copy is out of date. Try deleting the related post from the cache or remove the entire cache:
97
+
98
+ ```
99
+ rm -rf .jekyll-hive-cache
100
+ ```
101
+
59
102
  ## Contributing
60
103
 
61
104
  1. Fork it ( https://github.com/inertia186/jekyll-hive/fork )
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'cgi'
4
- require 'steem'
4
+ require 'hive'
5
+ require 'open-uri'
6
+ require 'fileutils'
5
7
 
6
8
  Net::OpenTimeout = Class.new(RuntimeError) unless Net.const_defined?(:OpenTimeout)
7
9
  Net::ReadTimeout = Class.new(RuntimeError) unless Net.const_defined?(:ReadTimeout)
@@ -19,8 +21,48 @@ module Jekyll
19
21
 
20
22
  if (tag_contents = determine_arguments(@markup.strip))
21
23
  hive_slug = tag_contents[0]
24
+ attempts = 0
25
+ content = nil
26
+ rebuild = ENV.fetch("JEKYLL_HIVE_REBUILD", 'false') == 'true'
22
27
 
23
- hive_tag(hive_slug)
28
+ loop do
29
+ warn "Attempts for #{hive_slug}: #{attempts}" if attempts > 0
30
+
31
+ if attempts > 4
32
+ warn "Gave up on: #{hive_slug}"
33
+ delete_content_cache(hive_slug)
34
+ break
35
+ end
36
+
37
+ begin
38
+ attempts = attempts + 1
39
+
40
+ if !!rebuild && !!content_cache(hive_slug)
41
+ if content_age(hive_slug) > 9000
42
+ # TODO quickly check if the cache should be cleared
43
+
44
+ warn "Checking for changes: #{hive_slug}"
45
+
46
+ if content_changed?(hive_slug)
47
+ warn "Refreshing: #{hive_slug}"
48
+ delete_content_cache(hive_slug)
49
+ end
50
+ end
51
+
52
+ content_touch(hive_slug)
53
+ end
54
+
55
+ content = content_cache(hive_slug) || hive_tag(hive_slug)
56
+
57
+ break
58
+ rescue => e
59
+ warn "Retrying: #{hive_slug} (#{e})"
60
+ @api = nil
61
+ sleep 3
62
+ end
63
+ end
64
+
65
+ content_cache(hive_slug, content)
24
66
  else
25
67
  raise ArgumentError, <<~ERROR
26
68
  Syntax error in tag 'hive' while parsing the following markup:
@@ -32,20 +74,105 @@ module Jekyll
32
74
  end
33
75
  end
34
76
  private
77
+ def cache_dir_path
78
+ ENV.fetch('JEKYLL_HIVE_CACHE', '.jekyll-hive-cache')
79
+ end
80
+
81
+ def parse_key(hive_slug)
82
+ hive_slug.gsub('/', '-')
83
+ end
84
+
85
+ def cached_file(hive_slug)
86
+ key = parse_key(hive_slug)
87
+
88
+ cache_dir_path + "/#{key}"
89
+ end
90
+
91
+ def content_cache(hive_slug, value = nil)
92
+ key = parse_key(hive_slug)
93
+
94
+ unless !!@content_cache
95
+ @content_cache ||= {}
96
+
97
+ Dir.mkdir cache_dir_path unless Dir.exists? cache_dir_path
98
+
99
+ Dir.glob("#{cache_dir_path}/*") do |filename|
100
+ next if File.directory? filename
101
+
102
+ @content_cache[filename.split('/').last] = File.open(filename).read
103
+ end
104
+ end
105
+
106
+ if !!value && value != ''
107
+ @content_cache[key] ||= value
108
+
109
+ File.open(cached_file(hive_slug), 'wb') do |cache_file|
110
+ cache_file.write(value)
111
+ end
112
+ end
113
+
114
+ if @content_cache[key] == ''
115
+ delete_content_cache(hive_slug)
116
+
117
+ nil
118
+ else
119
+ @content_cache[key]
120
+ end
121
+ end
122
+
123
+ def content_age(hive_slug)
124
+ age = Time.now - File.mtime(cached_file(hive_slug)) rescue 0
125
+ warn "Age for #{hive_slug}: #{age}"
126
+
127
+ age
128
+ end
129
+
130
+ def content_touch(hive_slug)
131
+ FileUtils.touch(cached_file(hive_slug))
132
+ end
133
+
134
+ def delete_content_cache(hive_slug)
135
+ File.delete(cached_file(hive_slug)) rescue false
136
+ end
137
+
35
138
  def determine_arguments(input)
36
139
  return unless input =~ /@?[^\/]+\/[^\/]+/i
37
140
 
38
141
  [input]
39
142
  end
40
143
 
41
- def hive_tag(hive_slug)
144
+ def api
145
+ url = ENV.fetch('NODE_URL', 'https://api.hive.blog,https://api.openhive.network').split(',').sample
146
+
147
+ @api ||= ::Hive::CondenserApi.new(url: url)
148
+ end
149
+
150
+ def parse_slug(hive_slug)
42
151
  hive_slug = hive_slug.split('@').last
43
152
  hive_slug = hive_slug.split('/')
44
153
  author = hive_slug[0]
45
154
  permlink = hive_slug[1..-1].join('/')
46
155
  permlink = permlink.split('?').first
47
156
  permlink = permlink.split('#').first
48
- api = ::Steem::CondenserApi.new(url: ENV.fetch('NODE_URL', 'https://api.openhive.network'))
157
+
158
+ [author, permlink]
159
+ end
160
+
161
+ def content_changed?(hive_slug)
162
+ author, permlink = parse_slug(hive_slug)
163
+ cached_created = File.mtime(cached_file(hive_slug)) rescue Time.now
164
+
165
+ # TODO find a better way to get the `created` timestamp, this is really
166
+ # no faster than just parsing the content.
167
+ api.get_content(author, permlink) do |content|
168
+ created = Time.parse(content.created + 'Z')
169
+
170
+ cached_created > created
171
+ end
172
+ end
173
+
174
+ def hive_tag(hive_slug)
175
+ author, permlink = parse_slug(hive_slug)
49
176
 
50
177
  api.get_content(author, permlink) do |content|
51
178
  body = content.body
@@ -57,24 +184,82 @@ module Jekyll
57
184
 
58
185
  body = body.gsub(/https:\/\/steemitimages.com\/[0-9]+x0\/https:\/\//, 'https://')
59
186
  body = body.gsub(/https:\/\/images.hive.blog\/[0-9]+x0\/https:\/\//, 'https://')
187
+ body = body.gsub(/https:\/\/images.esteem.app\/[0-9]+x0\/https:\/\//, 'https://')
60
188
 
61
- # Although it works on hive.blog and many other markdown interpretors,
62
- # kramdown doesn't like this, so we have to fix it:
63
- #
64
- # <div>
65
- # This *won't* work.
66
- # </div>
67
- #
68
- # See: https://stackoverflow.blog/2008/06/25/three-markdown-gotcha/
69
-
70
- body = body.gsub(/<([^\/].+)>(.+)<\/\1>/m) do
71
- match = Regexp.last_match
72
- html = Kramdown::Document.new(match[2]).to_html
73
-
74
- "<#{match[1]}>#{html.gsub("\n", "<br />")}</#{match[1]}>"
189
+ scrape = URI::open(canonical_url).read
190
+
191
+ if scrape.include? 'rel="canonical"'
192
+ canonical_url = scrape.split('rel="canonical"')[1].split('"')[1]
75
193
  end
76
-
77
- body + <<~DONE
194
+
195
+ "<div id=\"content-#{author}-#{permlink}\">" + body + <<~DONE
196
+ </div>
197
+ <script crossorigin='anonymous' integrity='sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=' src='https://code.jquery.com/jquery-3.5.1.slim.min.js'></script>
198
+ <script src='https://unpkg.com/steem-content-renderer'></script>
199
+ <!-- <script src="https://cdn.jsdelivr.net/npm/hive-content-renderer/dist/hive-content-renderer.min.js"></script> -->
200
+ <script>
201
+ $(document).ready(function() {
202
+ try {
203
+ const renderer = new SteemContentRenderer.DefaultRenderer({
204
+ // const renderer = new HiveContentRenderer({
205
+ baseUrl: "https://hive.blog/",
206
+ breaks: true,
207
+ skipSanitization: false,
208
+ allowInsecureScriptTags: false,
209
+ addNofollowToLinks: true,
210
+ doNotShowImages: false,
211
+ ipfsPrefix: "",
212
+ assetsWidth: 640,
213
+ assetsHeight: 480,
214
+ imageProxyFn: (url) => url,
215
+ usertagUrlFn: (account) => "/@#{author}",
216
+ hashtagUrlFn: (hashtag) => "/#{permlink}",
217
+ isLinkSafeFn: (url) => true,
218
+ });
219
+
220
+ const inputElem = $('#content-#{author}-#{permlink}').html();
221
+ const outputElem = $('#content-#{author}-#{permlink}');
222
+ const output = renderer.render(inputElem);
223
+
224
+ outputElem.html(output);
225
+ } catch(e) {
226
+ console.log(e);
227
+ }
228
+ });
229
+ </script>
230
+ <style>
231
+ #content-#{author}-#{permlink} {
232
+ padding: 0 3rem;
233
+ color: #444444;
234
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
235
+ font-size: 16px;
236
+ line-height: 1.8;
237
+ text-shadow: 0 1px 0 #ffffff;
238
+ padding: 0.5rem;
239
+ }
240
+ #content-#{author}-#{permlink} code {
241
+ background: white;
242
+ }
243
+ #content-#{author}-#{permlink} a {
244
+ border-bottom: 1px solid #444444; color: #444444; text-decoration: none;
245
+ }
246
+ #content-#{author}-#{permlink} a:hover {
247
+ border-bottom: 0;
248
+ }
249
+ #content-#{author}-#{permlink} h1 {
250
+ font-size: 2.2em;
251
+ }
252
+ #content-#{author}-#{permlink} h2, h3, h4, h5 {
253
+ margin-bottom: 0;
254
+ }
255
+ #content-#{author}-#{permlink} header small {
256
+ color: #999;
257
+ font-size: 50%;
258
+ }
259
+ #content-#{author}-#{permlink} img {
260
+ max-width: 100%;
261
+ }
262
+ </style>
78
263
  \n<hr />
79
264
  <p>
80
265
  See: <a href="#{canonical_url}">#{content.title}</a>
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Hive
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-hive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Martin
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2022-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: steem-ruby
14
+ name: hive-ruby
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0.9'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0.9'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -150,7 +150,7 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
- description:
153
+ description:
154
154
  email:
155
155
  - jekyll-hive@martin-studio.com
156
156
  executables: []
@@ -166,7 +166,7 @@ homepage: https://github.com/inertia186/jekyll-hive
166
166
  licenses:
167
167
  - CC0-1.0
168
168
  metadata: {}
169
- post_install_message:
169
+ post_install_message:
170
170
  rdoc_options: []
171
171
  require_paths:
172
172
  - lib
@@ -181,9 +181,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  - !ruby/object:Gem::Version
182
182
  version: '0'
183
183
  requirements: []
184
- rubyforge_project:
185
- rubygems_version: 2.7.10
186
- signing_key:
184
+ rubygems_version: 3.1.4
185
+ signing_key:
187
186
  specification_version: 4
188
187
  summary: Liquid tag for displaying Hive content in Jekyll sites.
189
188
  test_files: []