Skip to content

Commit 1567a53

Browse files
committed
properly escape smart quotes in title and description
1 parent c255b53 commit 1567a53

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
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

lib/template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{% endif %}
1919
{% endif %}
2020
{% if seo_title %}
21-
{% assign seo_title = seo_title | escape | markdownify | strip_html | strip_newlines | replace:'"','&quot;' | escape_once %}
21+
{% assign seo_title = seo_title | markdownify | strip_html | strip_newlines | escape_once %}
2222
{% endif %}
2323

2424
{% if page.description %}
@@ -27,7 +27,7 @@
2727
{% assign seo_description = site.description %}
2828
{% endif %}
2929
{% if seo_description %}
30-
{% assign seo_description = seo_description | escape | markdownify | strip_html | strip_newlines | replace:'"','&quot;' | escape_once %}
30+
{% assign seo_description = seo_description | markdownify | strip_html | strip_newlines | escape_once %}
3131
{% endif %}
3232

3333
{% if seo_title %}

spec/jekyll_seo_tag_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
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; &quot;Hyde&quot;<\/title>/)
38+
expect(subject.render(context)).to match(/<title>Jekyll &amp; &ldquo;Hyde&rdquo;<\/title>/)
3939
end
4040

4141
it "escapes descriptions" do
4242
site = site({"description" => 'Jekyll & "Hyde"'})
4343
context = context({ :site => site })
44-
expected = /<meta name="description" content="Jekyll &amp; &quot;Hyde&quot;" \/>/
44+
expected = /<meta name="description" content="Jekyll &amp; &ldquo;Hyde&rdquo;" \/>/
4545
expect(subject.render(context)).to match(expected)
4646
end
4747

0 commit comments

Comments
 (0)