8000 Inject `site.github` via `:pre_render` step rather than `:after_init`… · jekyll/github-metadata@a771d0d · GitHub
[go: up one dir, main page]

Skip to content

Commit a771d0d

Browse files
authored
Inject site.github via :pre_render step rather than :after_init (#238)
Merge pull request 238
1 parent c1ec41e commit a771d0d

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

lib/jekyll-github-metadata/site_github_munger.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module GitHubMetadata
66
class SiteGitHubMunger
77
extend Forwardable
88

9+
class << self
10+
attr_accessor :global_munger
11+
end
12+
913
def_delegators Jekyll::GitHubMetadata, :site, :repository
1014
private :repository
1115

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

19-
# This is the good stuff.
20-
site.config["github"] = github_namespace
21-
2223
add_title_and_description_fallbacks!
2324
add_url_and_baseurl_fallbacks! if should_add_url_fallbacks?
2425
end
2526

27+
def inject_metadata!(payload)
28+
payload.site["github"] = github_namespace
29+
end
30+
2631
private
2732

2833
def github_namespace
@@ -80,9 +85,14 @@ def should_warn_about_site_name?
8085
site.config["name"] && !site.config["title"]
8186
end
8287
end
83-
end
84-
end
8588

86-
Jekyll::Hooks.register :site, :after_init do |site|
87-
Jekyll::GitHubMetadata::SiteGitHubMunger.new(site).munge!
89+
Jekyll::Hooks.register :site, :after_init do |site|
90+
SiteGitHubMunger.global_munger = SiteGitHubMunger.new(site)
91+
SiteGitHubMunger.global_munger.munge!
92+
end
93+
94+
Jekyll::Hooks.register :site, :pre_render do |_site, payload|
95+
SiteGitHubMunger.global_munger.inject_metadata!(payload)
96+
end
97+
end
8898
end

spec/site_github_munger_spec.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,46 @@
1010
let(:site) { Jekyll::Site.new(Jekyll::Configuration.from(user_config)) }
1111
subject { described_class.new(site) }
1212
let!(:stubs) { stub_all_api_requests }
13+
let(:unified_payload) { site.site_payload }
1314

1415
context "generating" do
1516
before(:each) do
1617
ENV["JEKYLL_ENV"] = "production"
1718
subject.munge!
19+
subject.inject_metadata!(unified_payload)
1820
end
1921

2022
context "with site.github as nil" do
21-
it "replaces site.github with the drop" do
22-
expect(site.config["github"]).to be_a(Liquid::Drop)
23+
it "sets site.github to the drop" do
24+
expect(unified_payload.site["github"]).to be_a(Liquid::Drop)
2325
end
2426
end
2527

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

2931
it "replaces site.github with the drop" do
30-
expect(site.config["github"]).to be_a(Liquid::Drop)
32+
expect(unified_payload.site["github"]).to be_a(Liquid::Drop)
3133
end
3234
end
3335

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

3739
it "doesn't munge" do
38-
expect(site.config["github"]).to eql("foo")
40+
expect(unified_payload.site["github"]).to eql("foo")
3941
end
4042
end
4143

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

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

4951
it "still sets other values" do
50-
expect(site.config["github"].invoke_drop("source")["path"]).to eql("/")
52+
expect(unified_payload.site["github"].invoke_drop("source")["path"]).to eql("/")
5153
end
5254
end
5355

@@ -202,8 +204,8 @@
202204
end
203205

204206
it "sets the site.github config" do
205-
subject.munge!
206-
expect(site.config["github"]).to be_instance_of(Jekyll::GitHubMetadata::MetadataDrop)
207+
subject.inject_metadata!(unified_payload)
208+
expect(unified_payload.site["github"]).to be_instance_of(Jekyll::GitHubMetadata::MetadataDrop)
207209
end
208210
end
209211

@@ -223,8 +225,9 @@
223225

224226
it "fails loudly upon call to any drop method" do
225227
subject.munge!
228+
subject.inject_metadata!(unified_payload)
226229
expect do
227-
site.config["github"]["url"]
230+
unified_payload.site["github"]["url"]
228231
end.to raise_error(Jekyll::GitHubMetadata::Client::BadCredentialsError)
229232
end
230233
end

0 commit comments

Comments
 (0)
0