Skip to content

Commit

Permalink
Inject site.github via :pre_render step rather than :after_init (
Browse files Browse the repository at this point in the history
…#238)

Merge pull request 238
  • Loading branch information
parkr committed May 20, 2022
1 parent c1ec41e commit a771d0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
24 changes: 17 additions & 7 deletions lib/jekyll-github-metadata/site_github_munger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module GitHubMetadata
class SiteGitHubMunger
extend Forwardable

class << self
attr_accessor :global_munger
end

def_delegators Jekyll::GitHubMetadata, :site, :repository
private :repository

Expand All @@ -16,13 +20,14 @@ def initialize(site)
def munge!
Jekyll::GitHubMetadata.log :debug, "Initializing..."

# This is the good stuff.
site.config["github"] = github_namespace

add_title_and_description_fallbacks!
add_url_and_baseurl_fallbacks! if should_add_url_fallbacks?
end

def inject_metadata!(payload)
payload.site["github"] = github_namespace
end

private

def github_namespace
Expand Down Expand Up @@ -80,9 +85,14 @@ def should_warn_about_site_name?
site.config["name"] && !site.config["title"]
end
end
end
end

Jekyll::Hooks.register :site, :after_init do |site|
Jekyll::GitHubMetadata::SiteGitHubMunger.new(site).munge!
Jekyll::Hooks.register :site, :after_init do |site|
SiteGitHubMunger.global_munger = SiteGitHubMunger.new(site)
SiteGitHubMunger.global_munger.munge!
end

Jekyll::Hooks.register :site, :pre_render do |_site, payload|
SiteGitHubMunger.global_munger.inject_metadata!(payload)
end
end
end
21 changes: 12 additions & 9 deletions spec/site_github_munger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,46 @@
let(:site) { Jekyll::Site.new(Jekyll::Configuration.from(user_config)) }
subject { described_class.new(site) }
let!(:stubs) { stub_all_api_requests }
let(:unified_payload) { site.site_payload }

context "generating" do
before(:each) do
ENV["JEKYLL_ENV"] = "production"
subject.munge!
subject.inject_metadata!(unified_payload)
end

context "with site.github as nil" do
it "replaces site.github with the drop" do
expect(site.config["github"]).to be_a(Liquid::Drop)
it "sets site.github to the drop" do
expect(unified_payload.site["github"]).to be_a(Liquid::Drop)
end
end

context "without site.github" do
let(:user_config) { {} }

it "replaces site.github with the drop" do
expect(site.config["github"]).to be_a(Liquid::Drop)
expect(unified_payload.site["github"]).to be_a(Liquid::Drop)
end
end

context "with site.github as a non-hash" do
let(:github_namespace) { "foo" }

it "doesn't munge" do
expect(site.config["github"]).to eql("foo")
expect(unified_payload.site["github"]).to eql("foo")
end
end

context "with site.github as a hash" do
let(:github_namespace) { { "source" => { "branch" => "foo" } } }

it "lets user-specified values override the drop" do
expect(site.config["github"].invoke_drop("source")["branch"]).to eql("foo")
expect(unified_payload.site["github"].invoke_drop("source")["branch"]).to eql("foo")
end

it "still sets other values" do
expect(site.config["github"].invoke_drop("source")["path"]).to eql("/")
expect(unified_payload.site["github"].invoke_drop("source")["path"]).to eql("/")
end
end

Expand Down Expand Up @@ -202,8 +204,8 @@
end

it "sets the site.github config" do
subject.munge!
expect(site.config["github"]).to be_instance_of(Jekyll::GitHubMetadata::MetadataDrop)
subject.inject_metadata!(unified_payload)
expect(unified_payload.site["github"]).to be_instance_of(Jekyll::GitHubMetadata::MetadataDrop)
end
end

Expand All @@ -223,8 +225,9 @@

it "fails loudly upon call to any drop method" do
subject.munge!
subject.inject_metadata!(unified_payload)
expect do
site.config["github"]["url"]
unified_payload.site["github"]["url"]
end.to raise_error(Jekyll::GitHubMetadata::Client::BadCredentialsError)
end
end
Expand Down

0 comments on commit a771d0d

Please sign in to comment.