Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support asset hash #14

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/rubygems.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: Publish to RubyGems
on:
push:
tags:
- v[1-9].[0-9]+.[0-9]+
release:
types: [published]
jobs:
publish:
runs-on: ubuntu-latest
Expand All @@ -23,8 +22,9 @@ jobs:
- name: Push gem
run: |
VERSION=$(bundle exec rake print_version)
if [ "refs/tags/v${VERSION}" != $GITHUB_REF ]; then
echo "Ref ${GITHUB_REF} not match"
TAG_NAME=${{ github.event.release.tag_name }}
if [ "v${VERSION}" != $TAG_NAME ]; then
echo "Ref ${TAG_NAME} not match with v${VERSION}"
exit 1
fi
gem push "pkg/middleman-ogp-${VERSION}.gem"
- run: gem push "pkg/middleman-ogp-${VERSION}.gem"
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ activate :ogp do |ogp|
#
ogp.namespaces = {
fb: data.ogp.fb,
# from data/ogp/fb.yml
og: data.ogp.og
# from data/ogp/og.yml
og:
# from data/ogp/fb.yml
data
.ogp
.og
}
ogp.base_url = 'http://mysite.tld/'
# from data/ogp/og.yml

ogp.base_url = 'http://mysite.tld'
end
```

Expand All @@ -29,8 +33,7 @@ Example:

```yaml
image:
"": http://mydomain.tld/path/to/fbimage.png
secure_url: https://secure.mydomain.tld/path/to/fbimage.png
"": path/to/fbimage.png
type: image/png
width: 400
height: 300
Expand Down Expand Up @@ -65,8 +68,7 @@ ogp:
og:
description: "This is my fixture Middleman site."
image:
"": http://mydomain.tld/path/to/fbimage.png
secure_url: https://secure.mydomain.tld/path/to/fbimage.png
"": path/to/fbimage.png
type: image/png
width: 400
height: 300
Expand All @@ -78,8 +80,7 @@ ogp:
fb:
description: "This is my fixture Middleman site."
image:
"": http://mydomain.tld/path/to/fbimage.png
secure_url: https://secure.mydomain.tld/path/to/fbimage.png
"": path/to/fbimage.png
type: image/png
width: 400
height: 300
Expand All @@ -103,11 +104,16 @@ activate :ogp do |ogp|
#
ogp.namespaces = {
fb: data.ogp.fb,
# from data/ogp/fb.yml
og: data.ogp.og
# from data/ogp/og.yml
og:
# from data/ogp/fb.yml
data
.ogp
.og
}
# from data/ogp/og.yml

ogp.blog = true
ogp.base_url = 'http://mysite.tld'
end
```

Expand Down
4 changes: 1 addition & 3 deletions features/blog.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Feature: Middleman Blog support
Then I should see '<meta content="2018-04-12T04:00:00Z" property="article:expiration_time" />'
Then I should see '<meta content="blog" property="article:tag" />'
Then I should see '<meta content="http://myblog.foo.tld/2014/04/12/my-test.html" property="og:url" />'
Then I should see '<meta content="http://myblog.foo.tld/images/logo/sample-d588934c.png" property="og:image" />'
Then I should see '<meta content="Fixture page" property="og:title" />'

Scenario: multi author article page
Expand All @@ -38,6 +39,3 @@ Scenario: multi author article page
Then I should see '<meta content="male" property="article:author:gender" />'
Then I should see '<meta content="http://myblog.foo.tld/2019/07/18/multi-author-test.html" property="og:url" />'
Then I should see '<meta content="Multi author fixture page" property="og:title" />'



4 changes: 2 additions & 2 deletions features/helper.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Feature: OpenGraph Protocol Tags with the "ogp_tags" helper method
When I go to "/"
Then I should see '<meta content="5678" property="fb:app_id" />'
Then I should see '<meta content="My Description" property="og:description" />'
Then I should see '<meta content="https://images.mydomain.tld/path/to/og-site-image.png" property="og:image" />'
Then I should see '<meta content="https://my.assethosting.tld/images/logo/sample2-23c27556.png" property="og:image" />'
Then I should see '<meta content="image/png" property="og:image:type" />'
Then I should see '<meta content="400" property="og:image:width" />'
Then I should see '<meta content="300" property="og:image:height" />'
Expand All @@ -20,7 +20,7 @@ Feature: OpenGraph Protocol Tags with the "ogp_tags" helper method
When I go to "/page.html"
Then I should see '<meta content="1234" property="fb:app_id" />'
Then I should see '<meta content="This is my fixture Middleman article." property="og:description" />'
Then I should see '<meta content="https://images.mydomain.tld/path/to/og-article-image.png" property="og:image" />'
Then I should see '<meta content="https://my.assethosting.tld/images/logo/sample-d588934c.png" property="og:image" />'
Then I should see '<meta content="image/png" property="og:image:type" />'
Then I should see '<meta content="400" property="og:image:width" />'
Then I should see '<meta content="300" property="og:image:height" />'
Expand Down
9 changes: 4 additions & 5 deletions fixtures/test-app/config.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# frozen_string_literal: true

activate :asset_hash
activate :ogp do |ogp|
#
# register namespace with default options
#
ogp.namespaces = {
fb: data.ogp.fb,
# from data/ogp/fb.yml
og: data.ogp.og
# from data/ogp/og.yml
}
ogp.base_url = 'http://myshop.foo.tld/'
ogp.image_base_url = 'https://images.mydomain.tld/path/'
ogp.base_url = 'http://myshop.foo.tld'
end

activate :asset_host, host: 'https://my.assethosting.tld'
2 changes: 1 addition & 1 deletion fixtures/test-app/data/ogp/og.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
description: 'This is my fixture Middleman site for OpenGraph.'
image:
'': to/og-site-image.png
'': logo/sample2.png
type: image/png
width: 400
height: 300
Expand Down
Binary file added fixtures/test-app/source/images/logo/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/test-app/source/images/logo/sample2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion fixtures/test-app/source/page.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ogp:
og:
description: 'This is my fixture Middleman article for OpenGraph.'
image:
'': to/og-article-image.png
'': logo/sample.png
type: image/png
width: 400
height: 300
Expand Down
3 changes: 2 additions & 1 deletion fixtures/test-blog/config.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

activate :asset_hash
activate :blog do |blog|
end

Expand All @@ -8,6 +9,6 @@
fb: data.ogp.fb,
og: data.ogp.og
}
ogp.base_url = 'http://myblog.foo.tld/'
ogp.blog = true
ogp.base_url = 'http://myblog.foo.tld'
end
6 changes: 5 additions & 1 deletion fixtures/test-blog/source/2014-04-12-my-test.html.slim
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
title: Fixture page
description: This is a ficture page
description: This is a fixture page
tags: ruby, middleman, blog
date: 2014-04-12 04:00
ogp:
og:
image:
"": logo/sample.png
author:
first_name: Test
last_name: Author
Expand Down
Binary file added fixtures/test-blog/source/images/logo/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 20 additions & 9 deletions lib/middleman-ogp/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ class OGPExtension < Extension
option :blog, false, 'Middleman Blog support'
option :auto, %w[title url description], 'Properties to automatically fill from page data.'
option :base_url, nil, 'Base URL to generate permalink for og:url'
option :image_base_url, nil, 'Base URL to generate og:image'

def after_configuration
Middleman::OGP::Helper.namespaces = options[:namespaces] || {}
Middleman::OGP::Helper.blog = options[:blog]
Middleman::OGP::Helper.auto = options[:auto]
Middleman::OGP::Helper.base_url = options[:base_url]
Middleman::OGP::Helper.image_base_url = options[:image_base_url]
end

#
helpers do # rubocop:disable Metrics/BlockLength
def ogp_tags(&block) # rubocop:disable all
Middleman::OGP::Helper.app = @app
Middleman::OGP::Helper.current_resource = current_resource

opts = current_resource.data['ogp'] || {}
is_blog_article = Middleman::OGP::Helper.blog && respond_to?(:is_blog_article?) && is_blog_article?
if is_blog_article
Expand Down Expand Up @@ -85,9 +86,8 @@ def ogp_tags(&block) # rubocop:disable all
opts[:og][:description] = yield_content(:description)
end
end
if Middleman::OGP::Helper.auto.include?('url') &&
Middleman::OGP::Helper.base_url
opts[:og][:url] = URI.join(Middleman::OGP::Helper.base_url, current_resource.url)
if Middleman::OGP::Helper.auto.include?('url') && Middleman::OGP::Helper.base_url
opts[:og][:url] = Middleman::OGP::Helper.base_url + current_resource.url
end

Middleman::OGP::Helper.ogp_tags(opts) do |name, value|
Expand All @@ -108,7 +108,8 @@ module Helper
mattr_accessor :blog
mattr_accessor :auto
mattr_accessor :base_url
mattr_accessor :image_base_url
mattr_accessor :app
mattr_accessor :current_resource

def self.ogp_tags(opts = {}, &block) # rubocop:disable Metrics/MethodLength
opts ||= {}
Expand Down Expand Up @@ -161,13 +162,23 @@ def self.og_tag(key, obj = nil, prefix = 'og', &block) # rubocop:disable Metrics
else
name = [prefix].concat(key).join(':')
value = obj.to_s
if Middleman::OGP::Helper.image_base_url && name == 'og:image' && !%r{^https?://}.match(value)
value = URI.join(Middleman::OGP::Helper.image_base_url, value)
end
value = full_image_url(value) if name == 'og:image'

block.call name, value
end
end
end

def self.full_image_url(value)
return value if %r{^https?://}.match(value)

value = Middleman::Util.asset_url(app, value, app.config.images_dir, current_resource: current_resource)
return app.extensions[:asset_host].rewrite_url(value, '', nil) if app.extensions[:asset_host]

return URI.join(base_url, value) if base_url

value
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/middleman-ogp/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Middleman
module OGP
VERSION = '1.4.0'
VERSION = '1.5.0'
end
end
2 changes: 2 additions & 0 deletions spec/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require 'spec_helper'

describe 'Middleman::OGP::Helper' do # rubocop:disable Metrics/BlockLength
let(:app) { Middleman::Application.new }
subject do
Middleman::OGP::Helper.app = app
Middleman::OGP::Helper.namespaces = namespaces
Middleman::OGP::Helper.ogp_tags(options) do |name, value|
%(<meta property="#{name}" content="#{value}" />)
Expand Down