Skip to content

Commit a355e6e

Browse files
committed
Merge pull request #6 from benbalter/proper-escapes
Properly escape quotes in titles and descriptions
2 parents eb99fb0 + 1567a53 commit a355e6e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/jekyll-seo-tag.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ class SeoTag < Liquid::Tag
55

66
def render(context)
77
@context = context
8-
Liquid::Template.parse(template_contents).render!(payload, info).gsub(/[\n\s]{2,}/, "\n")
8+
output = Liquid::Template.parse(template_contents).render!(payload, info)
9+
10+
# Minify
11+
output.gsub!(/[\n\s]{2,}/, "\n")
12+
13+
# Encode smart quotes. See https://github.com/benbalter/jekyll-seo-tag/pull/6
14+
output.gsub!("\u201c", "&ldquo;")
15+
output.gsub!("\u201d", "&rdquo;")
16+
17+
output
918
end
1019

1120
private

spec/jekyll_seo_tag_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@
3535
it "escapes titles" do
3636
site = site({"title" => 'Jekyll & "Hyde"'})
3737
context = context({ :site => site })
38-
expect(subject.render(context)).to match(/<title>Jekyll &amp; “Hyde”<\/title>/)
38+
expect(subject.render(context)).to match(/<title>Jekyll &amp; &ldquo;Hyde&rdquo;<\/title>/)
39+
end
40+
41+
it "escapes descriptions" do
42+
site = site({"description" => 'Jekyll & "Hyde"'})
43+
context = context({ :site => site })
44+
expected = /<meta name="description" content="Jekyll &amp; &ldquo;Hyde&rdquo;" \/>/
45+
expect(subject.render(context)).to match(expected)
3946
end
4047

4148
it "uses the page description" do

0 commit comments

Comments
 (0)