Skip to content

Commit

Permalink
Use best_description for non ogp pages (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysk24ok committed May 5, 2023
1 parent adc1560 commit d0a5ab9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
11 changes: 1 addition & 10 deletions lib/jekyll-linkpreview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,14 @@ def from_page(page)
Properties.new({
'title' => page.best_title,
'url' => page.url,
'description' => get_description(page),
'description' => page.best_description,
'domain' => page.host,
}, @@template_file)
end

def from_hash(hash)
Properties.new(hash, @@template_file)
end

private
def get_description(page)
if !page.parsed.xpath('//p[normalize-space()]').empty? then
return page.parsed.xpath('//p[normalize-space()]').map(&:text).first[0..180] + "..."
else
return "..."
end
end
end

class LinkpreviewTag < Liquid::Tag
Expand Down
11 changes: 9 additions & 2 deletions spec/jekyll-linkpreview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,15 @@ def template_dir
describe 'description' do
where(:html, :expected) do
[
["<html><body><p>#{description}</p></body></html>", "#{description}..."], # p tag exists
["<html></html>", '...'], # no p tag
# standard meta description
["<html><head><meta name=\"description\" content=\"#{description}\"></head></html>", description],
# og:description
# TODO: _generate_html cannot be used here for some reason.
["<html><head><meta property=\"og:description\" content=\"#{description}\"></head></html>", description],
# twitter:description
["<html><head><meta name=\"twitter:description\" content=\"#{description}\"></head></html>", description],
# no description
["<html></html>", nil],
]
end

Expand Down

0 comments on commit d0a5ab9

Please sign in to comment.