jekyll-linkpreview 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: e553cbd1585c7303612cb32a21651039f3ebba2f
4
- data.tar.gz: 58bf7022cf2eb09e8661292b5143bd8b8750d201
3
+ metadata.gz: 597a9685dd537f7fddef541e75b596cdcda260cc
4
+ data.tar.gz: f4a9201088c0881410bf7e87d3963f04e9942de5
5
5
  SHA512:
6
- metadata.gz: d41da5f3352c0fee0e9fcdb5c5d91f8b3f75d3943d2fe00f41593ed00b78f0d94e55d8d5bb8474022a815dcbf532622b6afc8a8df62d84bfde0cb0e39ae2075e
7
- data.tar.gz: e6a23a9bb9c3170780aadd984af4977034055eaa4d53fc6ef008ce4bb92818057877894cb1bd43d2927b04e09c389d36af1572f6e054151272b74851d2b171aa
6
+ metadata.gz: e9affa10e185284257a77d68d44736f8c3f0f0d9a324aa4578fdb7fc454504dad883f69b0b2875ef5cdbd0766768709dc0c81a140b219c88bd607714cd1bbe9e
7
+ data.tar.gz: bd20c259e0becb55c2976f384f1e220b835e53778b7edc1a0382e37ccf49e00d6eb0aac38b77a453d9281f3cb11802925ac5095b1aa3a9d14c61e072afecf7db
data/README.md CHANGED
@@ -2,7 +2,20 @@
2
2
 
3
3
  Jekyll plugin to generate link preview by `{% linkpreview %}` tag. The plugin fetches [Open Graph protocols](http://ogp.me/) of the designated page to generate preview. The og properties are saved as JSON for caching and it is used when rebuilding the site.
4
4
 
5
- For example, `{% linkpreview https://github.com %}` tag generates following HTML when you run `jekyll build`,
5
+ You can pass url directly to the tag,
6
+
7
+ ```
8
+ {% linkpreview https://github.com %}
9
+ ```
10
+
11
+ or, can pass a url variable.
12
+
13
+ ```
14
+ {% assign github_toppage = 'https://github.com'%}
15
+ {% linkpreview github_toppage %}
16
+ ```
17
+
18
+ The tag above generates following HTML when you run `jekyll build`,
6
19
 
7
20
  ```html
8
21
  <div class="jekyll-linkpreview-wrapper">
@@ -38,7 +51,7 @@ See https://jekyllrb.com/docs/plugins/installation/ .
38
51
 
39
52
  ## Usage
40
53
 
41
- 1. Create `_cache` directory in the same hierarchy as `_site` directory.
54
+ 1. Create `_cache` directory where you run `jekyll build` or `jekyll serve`.
42
55
 
43
56
  1. Embed [linkpreview.css](assets/css/linkpreview.css) into your Website.
44
57
 
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Linkpreview
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -6,31 +6,82 @@ require "jekyll-linkpreview/version"
6
6
 
7
7
  module Jekyll
8
8
  module Linkpreview
9
+ class OpenGraphProperties
10
+ def get(url)
11
+ og_properties = fetch(url)
12
+ og_url = get_og_property(og_properties, 'og:url')
13
+ domain = extract_domain(og_url)
14
+ image_url = get_og_property(og_properties, 'og:image')
15
+ {
16
+ 'title' => get_og_property(og_properties, 'og:title'),
17
+ 'url' => og_url,
18
+ 'image' => convert_to_absolute_url(image_url, domain),
19
+ 'description' => get_og_property(og_properties, 'og:description'),
20
+ 'domain' => domain
21
+ }
22
+ end
23
+
24
+ private
25
+ def get_og_property(properties, key)
26
+ if !properties.key? key then
27
+ return nil
28
+ end
29
+ properties[key][0]
30
+ end
31
+
32
+ private
33
+ def fetch(url)
34
+ MetaInspector.new(url).meta_tags['property']
35
+ end
36
+
37
+ private
38
+ def convert_to_absolute_url(url, domain)
39
+ if url.nil? then
40
+ return nil
41
+ end
42
+ # root relative url
43
+ if url[0] == '/' then
44
+ return "//#{domain}#{url}"
45
+ end
46
+ url
47
+ end
48
+
49
+ private
50
+ def extract_domain(url)
51
+ if url.nil? then
52
+ return nil
53
+ end
54
+ url.match(%r{(http|https)://([^/]+).*})[-1]
55
+ end
56
+ end
57
+
9
58
  class LinkpreviewTag < Liquid::Tag
59
+ @@cache_dir = '_cache'
60
+
10
61
  def initialize(tag_name, markup, parse_context)
11
62
  super
12
63
  @markup = markup.rstrip()
13
- @cache_filepath = "_cache/%s.json" % Digest::MD5.hexdigest(@markup)
64
+ @og_properties = OpenGraphProperties.new
14
65
  end
15
66
 
16
67
  def render(context)
17
- properties = get_properties()
68
+ url = get_url_from(context)
69
+ properties = get_properties(url)
18
70
  title = properties['title']
19
- url = properties['url']
20
71
  image = properties['image']
21
72
  description = properties['description']
22
73
  domain = properties['domain']
23
- if title.nil? || url.nil? || image.nil? then
74
+ if title.nil? || image.nil? || description.nil? || domain.nil? then
24
75
  html = <<-EOS
25
76
  <div class="jekyll-linkpreview-wrapper">
26
- <p><a href="#{@markup}" target="_blank">#{@markup}</a></p>
77
+ <p><a href="#{url}" target="_blank">#{url}</a></p>
27
78
  </div>
28
79
  EOS
29
80
  return html
30
81
  end
31
82
  html = <<-EOS
32
83
  <div class="jekyll-linkpreview-wrapper">
33
- <p><a href="#{@markup}" target="_blank">#{@markup}</a></p>
84
+ <p><a href="#{url}" target="_blank">#{url}</a></p>
34
85
  <div class="jekyll-linkpreview-wrapper-inner">
35
86
  <div class="jekyll-linkpreview-content">
36
87
  <div class="jekyll-linkpreview-image">
@@ -46,7 +97,7 @@ module Jekyll
46
97
  </div>
47
98
  </div>
48
99
  <div class="jekyll-linkpreview-footer">
49
- <a href="#{url}" target="_blank">#{domain}</a>
100
+ <a href="//#{domain}" target="_blank">#{domain}</a>
50
101
  </div>
51
102
  </div>
52
103
  </div>
@@ -54,53 +105,34 @@ module Jekyll
54
105
  html
55
106
  end
56
107
 
57
- def get_properties()
58
- if File.exist?(@cache_filepath) then
59
- return load_cache_file
60
- else
61
- og_properties = fetch_og_properties()
62
- url = get_og_property(og_properties, 'og:url')
63
- properties = {
64
- 'title' => get_og_property(og_properties, 'og:title'),
65
- 'url' => url,
66
- 'image' => get_og_property(og_properties, 'og:image'),
67
- 'description' => get_og_property(og_properties, 'og:description'),
68
- 'domain' => extract_domain(url)
69
- }
70
- save_cache_file(properties)
71
- return properties
108
+ def get_properties(url)
109
+ cache_filepath = "#{@@cache_dir}/%s.json" % Digest::MD5.hexdigest(url)
110
+ if File.exist?(cache_filepath) then
111
+ return load_cache_file(cache_filepath)
72
112
  end
73
- end
74
-
75
- private
76
- def fetch_og_properties()
77
- MetaInspector.new(@markup).meta_tags['property']
78
- end
79
-
80
- private
81
- def get_og_property(properties, key)
82
- if !properties.key? key then
83
- return nil
113
+ properties = @og_properties.get(url)
114
+ if Dir.exists?(@@cache_dir) then
115
+ save_cache_file(cache_filepath, properties)
116
+ else
117
+ # TODO: This message will be shown at all linkprevew tag
118
+ warn "'#{@@cache_dir}' directory does not exist. Create it for caching."
84
119
  end
85
- properties[key][0]
120
+ properties
86
121
  end
87
122
 
88
123
  private
89
- def extract_domain(url)
90
- if url.nil? then
91
- return nil
92
- end
93
- url.match(%r{(http|https)://([^/]+).*})[-1]
124
+ def get_url_from(context)
125
+ context.scopes[0].key?(@markup) ? context.scopes[0][@markup] : @markup
94
126
  end
95
127
 
96
128
  private
97
- def load_cache_file()
98
- JSON.parse(File.open(@cache_filepath).read)
129
+ def load_cache_file(filepath)
130
+ JSON.parse(File.open(filepath).read)
99
131
  end
100
132
 
101
- private
102
- def save_cache_file(properties)
103
- File.open(@cache_filepath, 'w').write(JSON.generate(properties))
133
+ protected
134
+ def save_cache_file(filepath, properties)
135
+ File.open(filepath, 'w').write(JSON.generate(properties))
104
136
  end
105
137
  end
106
138
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-linkpreview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yusuke Nishioka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-04 00:00:00.000000000 Z
11
+ date: 2019-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  requirements: []
122
122
  rubyforge_project:
123
- rubygems_version: 2.6.13
123
+ rubygems_version: 2.5.2.3
124
124
  signing_key:
125
125
  specification_version: 4
126
126
  summary: Jekyll tag plugin to generate link preview