Skip to content

Commit 56aef57

Browse files
committed
If an HTTPError occures the noscript-Tag will not be rendered, but the script-Tag
1 parent 4e7d870 commit 56aef57

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/jekyll-gist/gist_tag.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,24 @@ def gist_script_tag(gist_id, filename = nil)
4848
end
4949

5050
def gist_noscript_tag(gist_id, filename = nil)
51+
code = fetch_raw_code(gist_id, filename)
52+
if !code.nil?
53+
"<noscript><pre>#{CGI.escapeHTML(code)}</pre></noscript>"
54+
end
55+
end
56+
57+
def fetch_raw_code(gist_id, filename = nil)
5158
if filename.empty?
5259
uri = "https://gist.githubusercontent.com/#{gist_id}/raw"
5360
else
5461
uri = "https://gist.githubusercontent.com/#{gist_id}/raw/#{filename}"
5562
end
56-
code = open(uri).read.chomp
57-
"<noscript><pre>#{CGI.escapeHTML(code)}</pre></noscript>"
63+
begin
64+
open(uri).read.chomp
65+
rescue OpenURI::HTTPError => e
66+
nil
67+
end
5868
end
59-
6069
end
6170
end
6271
end

spec/gist_tag_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,30 @@
9494
end
9595

9696
it "produces the correct script tag" do
97-
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>\n\n/)
97+
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>/)
9898
end
9999

100100
it "produces the correct noscript tag" do
101101
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
102102
end
103103
end
104+
105+
context "with valid gist id and invalid filename" do
106+
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(status: 404) }
107+
let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" }
108+
let(:gist_filename) { "myfile.ext" }
109+
let(:content) { "{% gist #{gist_id} #{gist_filename} %}" }
110+
111+
it "produces the correct script tag" do
112+
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>/)
113+
end
114+
115+
it "doesn't produces the noscript tag" do
116+
expect(output).to_not match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
117+
end
118+
119+
end
120+
104121
end
105122

106123

0 commit comments

Comments
 (0)